spejstore-new/auth/backend.py

43 lines
1.5 KiB
Python
Raw Permalink Normal View History

2023-07-11 21:29:39 +00:00
from urllib.parse import urlencode
2018-10-10 17:56:43 +00:00
from social_core.backends.oauth import BaseOAuth2
2023-07-11 13:34:35 +00:00
2018-10-10 17:56:43 +00:00
class HSWawOAuth2(BaseOAuth2):
"""Hackerspace OAuth authentication backend"""
2023-07-11 13:34:35 +00:00
2024-08-27 20:55:16 +00:00
name = "hswro"
2023-07-11 13:34:35 +00:00
ID_KEY = "username"
2024-08-27 20:55:16 +00:00
AUTHORIZATION_URL = "http://sso.lokal.hswro.org/oauth/authorize"
ACCESS_TOKEN_URL = "http://sso.lokal.hswro.org/oauth/token"
2023-07-11 13:34:35 +00:00
DEFAULT_SCOPE = ["profile:read"]
2018-10-10 17:56:43 +00:00
REDIRECT_STATE = False
2023-07-11 13:34:35 +00:00
SCOPE_SEPARATOR = ","
EXTRA_DATA = [("expires", "expires_in")]
2018-10-10 17:56:43 +00:00
def get_user_details(self, response):
2019-01-10 12:33:45 +00:00
"""Return user details from Hackerspace account"""
personal_email = None
2023-07-11 13:34:35 +00:00
if response.get("personal_email"):
personal_email = response.get("personal_email")[0]
2019-01-10 12:33:45 +00:00
2023-07-11 13:34:35 +00:00
return {
"username": response.get("username"),
"email": response.get("email"),
"personal_email": personal_email,
}
2018-10-10 17:56:43 +00:00
def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
2024-08-27 20:55:16 +00:00
url = "http://sso.lokal.hswro.org/api/1/profile"
2023-07-11 13:34:35 +00:00
headers = {"Authorization": "Bearer {}".format(access_token)}
2018-10-10 17:56:43 +00:00
return self.get_json(url, headers=headers)
def auth_url(self):
"""Return redirect url"""
state = self.get_or_create_state()
params = self.auth_params(state)
params.update(self.get_scope_argument())
params.update(self.auth_extra_arguments())
params = urlencode(params)
2023-07-11 13:34:35 +00:00
return "{0}?{1}".format(self.authorization_url(), params)