refactored social auth
This commit is contained in:
parent
164f860cc0
commit
3b7bef463f
|
|
@ -33,7 +33,7 @@ class Application(PlatformMixin, AbstractApplication):
|
||||||
swappable = "OAUTH2_PROVIDER_APPLICATION_MODEL"
|
swappable = "OAUTH2_PROVIDER_APPLICATION_MODEL"
|
||||||
|
|
||||||
def natural_key(self):
|
def natural_key(self):
|
||||||
return (self.client_id,)
|
return self.client_id
|
||||||
|
|
||||||
|
|
||||||
class JWTAccessTokenManager(models.Manager):
|
class JWTAccessTokenManager(models.Manager):
|
||||||
|
|
|
||||||
|
|
@ -30,24 +30,17 @@ from utils.views import JWTGenericViewMixin
|
||||||
# OAuth2
|
# OAuth2
|
||||||
class BaseOAuth2ViewMixin(generics.GenericAPIView):
|
class BaseOAuth2ViewMixin(generics.GenericAPIView):
|
||||||
"""BaseMixin for classic auth views"""
|
"""BaseMixin for classic auth views"""
|
||||||
def get_client_id(self, source) -> str:
|
@property
|
||||||
"""Get application client id"""
|
def client_credentials(self) -> dict:
|
||||||
qs = Application.objects.by_source(source=source)
|
"""Get application credentials."""
|
||||||
|
credentials = {}
|
||||||
|
qs = Application.objects.filter(authorization_grant_type=Application.GRANT_PASSWORD,
|
||||||
|
client_type=Application.CLIENT_CONFIDENTIAL)
|
||||||
if qs.exists():
|
if qs.exists():
|
||||||
return qs.first().client_id
|
application = qs.first()
|
||||||
else:
|
credentials = dict(client_id=application.client_id,
|
||||||
raise utils_exceptions.ServiceError(data={
|
client_secret=application.client_secret)
|
||||||
'detail': _('Application is not found')})
|
return credentials
|
||||||
|
|
||||||
def get_client_secret(self, source) -> str:
|
|
||||||
"""Get application client id"""
|
|
||||||
if source == Application.MOBILE:
|
|
||||||
qs = Application.objects.by_source(source=source)
|
|
||||||
if qs.exists:
|
|
||||||
return qs.first().client_secret
|
|
||||||
else:
|
|
||||||
raise utils_exceptions.ServiceError(data={
|
|
||||||
'detail': _('Not found an application with this source')})
|
|
||||||
|
|
||||||
|
|
||||||
class OAuth2ViewMixin(CsrfExemptMixin, OAuthLibMixin, BaseOAuth2ViewMixin):
|
class OAuth2ViewMixin(CsrfExemptMixin, OAuthLibMixin, BaseOAuth2ViewMixin):
|
||||||
|
|
@ -58,21 +51,20 @@ class OAuth2ViewMixin(CsrfExemptMixin, OAuthLibMixin, BaseOAuth2ViewMixin):
|
||||||
|
|
||||||
def prepare_request_data(self, validated_data: dict) -> dict:
|
def prepare_request_data(self, validated_data: dict) -> dict:
|
||||||
"""Preparing request data"""
|
"""Preparing request data"""
|
||||||
source = validated_data.get('source')
|
client_id = self.client_credentials.get('client_id')
|
||||||
# Set OAuth2 request parameters
|
client_secret = self.client_credentials.get('client_secret')
|
||||||
_request_data = {
|
token = validated_data.get('token')
|
||||||
'client_id': self.get_client_id(source)
|
|
||||||
}
|
if client_id and client_secret and token:
|
||||||
# Fill client secret parameter by platform
|
return {
|
||||||
if validated_data.get('source') == Application.MOBILE:
|
'client_id': client_id,
|
||||||
_request_data['client_secret'] = self.get_client_secret(source)
|
'client_secret': client_secret,
|
||||||
# Fill token parameter if transfer
|
'token': token
|
||||||
if validated_data.get('token'):
|
}
|
||||||
_request_data['token'] = validated_data.get('token')
|
|
||||||
if _request_data:
|
|
||||||
return _request_data
|
|
||||||
else:
|
else:
|
||||||
raise utils_exceptions.ServiceError()
|
raise utils_exceptions.ServiceError(data={
|
||||||
|
'detail': _('Validation OAuth2 request data error')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# Sign in via Facebook
|
# Sign in via Facebook
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user