From 442e4fb2ceaf020e6b80bfb13e7d7add2ef0815e Mon Sep 17 00:00:00 2001 From: Anatoly Date: Wed, 6 Nov 2019 18:50:42 +0300 Subject: [PATCH] refactored social auth --- apps/authorization/views/common.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/apps/authorization/views/common.py b/apps/authorization/views/common.py index e6e34f49..e3a5a3cb 100644 --- a/apps/authorization/views/common.py +++ b/apps/authorization/views/common.py @@ -1,4 +1,6 @@ """Common views for application Account""" +import hashlib +import hmac import json from braces.views import CsrfExemptMixin @@ -30,12 +32,12 @@ from utils.views import JWTGenericViewMixin # OAuth2 class BaseOAuth2ViewMixin(generics.GenericAPIView): """BaseMixin for classic auth views""" - @property - def client_credentials(self) -> dict: - """Get application credentials.""" + + def get_client_credentials(self, source) -> dict: + """Get application credentials by source.""" credentials = {} qs = Application.objects.filter(authorization_grant_type=Application.GRANT_PASSWORD, - client_type=Application.CLIENT_CONFIDENTIAL) + source=source) if qs.exists(): application = qs.first() credentials = dict(client_id=application.client_id, @@ -51,15 +53,22 @@ class OAuth2ViewMixin(CsrfExemptMixin, OAuthLibMixin, BaseOAuth2ViewMixin): def prepare_request_data(self, validated_data: dict) -> dict: """Preparing request data""" - client_id = self.client_credentials.get('client_id') - client_secret = self.client_credentials.get('client_secret') + source = validated_data.get('source') + credentials = self.get_client_credentials(source=source) + + client_id = credentials.get('client_id') + client_secret = credentials.get('client_secret') token = validated_data.get('token') + appsecret_proof = hmac.new(settings.SOCIAL_AUTH_FACEBOOK_KEY.encode('utf-8'), + msg=settings.SOCIAL_AUTH_FACEBOOK_SECRET.encode('utf-8'), + digestmod=hashlib.sha256).hexdigest() if client_id and client_secret and token: return { 'client_id': client_id, 'client_secret': client_secret, - 'token': token + 'token': token, + 'appsecret_proof': appsecret_proof, } else: raise utils_exceptions.ServiceError(data={