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