class ProjectProvider(OAuth2Provider):
id = 'project'
name = 'Project'
account_class = ProjectAccount
def extract_uid(self, data):
try:
return data['user_id']
except KeyError:
raise AuthExtractError('Error on extract user_id from response userinfo')
def extract_common_fields(self, data):
try:
return dict(
email=data['email'],
username=data['username'],
user_id=data['user_id'], # Не работает...
)
except KeyError:
raise AuthExtractError('Error on extract user_id from response userinfo')
def get_default_scope(self):
scope = ["read write"]
return scope
class ProjectAdapter(OAuth2Adapter):
provider_id = ProjectProvider.id
provider_base_url = settings.PROVIDER_AUTH_HOST
@property
def access_token_url(self):
return '{0}/oauth2/access_token'.format(self.provider_base_url)
@property
def authorize_url(self):
return '{0}/oauth2/authorize'.format(self.provider_base_url)
@property
def profile_url(self):
return '{0}/api/userinfo'.format(self.provider_base_url)
@backoff.on_exception(backoff.expo,
requests.exceptions.RequestException,
max_tries=5,
)
def complete_login(self, request, app, token, response):
response_profile = requests.get(self.profile_url, params={
'access_token': token.token
})
if response_profile.status_code == 200:
return self.get_provider().sociallogin_from_response(
request,
response_profile.json()
)
else:
raise AuthLoginError('Error on request to api userinfo')
oauth2_login = OAuth2LoginView.adapter_view(ProjectAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(ProjectAdapter)