2018-10-10 17:56:43 +00:00
|
|
|
from social_core.backends.oauth import BaseOAuth2
|
|
|
|
from six.moves.urllib_parse import urlencode, unquote
|
|
|
|
|
|
|
|
class HSWawOAuth2(BaseOAuth2):
|
|
|
|
"""Hackerspace OAuth authentication backend"""
|
|
|
|
name = 'hswaw'
|
|
|
|
ID_KEY = 'username'
|
|
|
|
AUTHORIZATION_URL = 'https://sso.hackerspace.pl/oauth/authorize'
|
|
|
|
ACCESS_TOKEN_URL = 'https://sso.hackerspace.pl/oauth/token'
|
|
|
|
DEFAULT_SCOPE = ['profile:read']
|
|
|
|
REDIRECT_STATE = False
|
|
|
|
SCOPE_SEPARATOR = ','
|
|
|
|
EXTRA_DATA = [
|
|
|
|
('expires', 'expires_in')
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_user_details(self, response):
|
2019-01-10 12:33:45 +00:00
|
|
|
"""Return user details from Hackerspace account"""
|
|
|
|
personal_email = None
|
|
|
|
if response.get('personal_email'):
|
|
|
|
personal_email = response.get('personal_email')[0]
|
|
|
|
|
2018-10-10 17:56:43 +00:00
|
|
|
return {'username': response.get('username'),
|
|
|
|
'email': response.get('email'),
|
2019-01-10 12:33:45 +00:00
|
|
|
'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"""
|
|
|
|
url = 'https://sso.hackerspace.pl/api/1/profile'
|
|
|
|
headers = {
|
2019-01-10 12:33:45 +00:00
|
|
|
'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)
|
|
|
|
return '{0}?{1}'.format(self.authorization_url(), params)
|