From 60527d18c39503f4075df13d0a9ec6fef1d4343f Mon Sep 17 00:00:00 2001 From: mahdavi Date: Mon, 26 Oct 2020 12:02:11 +0330 Subject: [PATCH] some cleanup --- apps/gooyal_oauth2/settings.py | 1 + apps/gooyal_oauth2/validators.py | 41 ------------------------------- apps/users/models.py | 42 ++++++++++++++++++++++++++++++++ gooyal_accounts/settings.py | 2 +- 4 files changed, 44 insertions(+), 42 deletions(-) diff --git a/apps/gooyal_oauth2/settings.py b/apps/gooyal_oauth2/settings.py index 1f05740..7aa7128 100644 --- a/apps/gooyal_oauth2/settings.py +++ b/apps/gooyal_oauth2/settings.py @@ -5,6 +5,7 @@ GOOYAL_DEFAULTS = { "RESOURCE_SERVER_CLIENT_ID": None, "RESOURCE_SERVER_CLIENT_SECRET": None, + # set default "INTROSPECTION_USER_CREATE_METHOD": None, } diff --git a/apps/gooyal_oauth2/validators.py b/apps/gooyal_oauth2/validators.py index 4672df9..479045d 100755 --- a/apps/gooyal_oauth2/validators.py +++ b/apps/gooyal_oauth2/validators.py @@ -16,47 +16,6 @@ AccessTokenModel = get_access_token_model() UserModel = get_user_model() -def get_user_profile(token, content): - # content = { - # "active": True, - # "scope": "read write email", - # "client_id": "J8NFmU4tJVgDxKaJFmXTWvaHO", - # "username": "aaronpk", - # "exp": 1437275311 - # } - - user_client = service_clients.Client(access_token=token, - scopes=content.get('scope', '').split(), - expires_in=100) - - user_account = user_client.accounts.account() - if user_account.get('uuid'): - content.update(user_account) - - # content = {'active': True, 'scope': 'ipg.payment:submit accounts.account:retrieve', 'exp': 1602619137, - # 'client_id': 'bd2hEGXytrqMjbFnplRyHJTeoW1vwKzCZJtH6ro0', 'username': '', - # 'uuid': '94255117-117c-4a9f-af50-35a6c47503ac', 'email': '', 'phone_number': '+989106853582', - # 'first_name': '', 'last_name': '', 'name': '', 'balance': 0, - # 'avatar': 'http://accounts.gooyal.com/media/avatars/1691899.jpg', 'iban': '', 'iban_verified': None} - - user = UserModel.objects.filter(uuid=content['uuid']).first() - if user: - pass - - else: - user = UserModel.objects.create_user( - **{UserModel.USERNAME_FIELD: content["username"]}, - uuid=content['uuid'], - email=content['email'], - phone_number=content['phone_number'], - first_name=content['first_name'], - last_name=content['last_name'], - name=content['name'], - avatar=content['avatar'] - ) - return user, content - - class OAuth2Validator(BaseOAuth2Validator): # pylint: disable=w0223 def validate_user(self, username, password, client, request, *args, **kwargs): auth_fields = getattr(request, 'auth_fields', 'username:password').split(':') diff --git a/apps/users/models.py b/apps/users/models.py index 1445230..91cdd5e 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -1,6 +1,7 @@ import random from datetime import timedelta +import service_clients from django.conf import settings from django.contrib.auth.base_user import BaseUserManager from django.contrib.auth.models import AbstractUser @@ -178,3 +179,44 @@ class User(AbstractUser): def __str__(self): return f"{self.pk} - {self.username}" + + +def create_user_in_introspection(token, content): + # content = { + # "active": True, + # "scope": "read write email", + # "client_id": "J8NFmU4tJVgDxKaJFmXTWvaHO", + # "username": "aaronpk", + # "exp": 1437275311 + # } + + user_client = service_clients.Client(access_token=token, + scopes=content.get('scope', '').split(), + expires_in=100) + + user_account = user_client.accounts.account() + if user_account.get('uuid'): + content.update(user_account) + + # content = {'active': True, 'scope': 'ipg.payment:submit accounts.account:retrieve', 'exp': 1602619137, + # 'client_id': 'bd2hEGXytrqMjbFnplRyHJTeoW1vwKzCZJtH6ro0', 'username': '', + # 'uuid': '94255117-117c-4a9f-af50-35a6c47503ac', 'email': '', 'phone_number': '+989106853582', + # 'first_name': '', 'last_name': '', 'name': '', 'balance': 0, + # 'avatar': 'http://accounts.gooyal.com/media/avatars/1691899.jpg', 'iban': '', 'iban_verified': None} + + user = User.objects.filter(uuid=content['uuid']).first() + if user: + pass + + else: + user = User.objects.create_user( + **{User.USERNAME_FIELD: content["username"]}, + uuid=content['uuid'], + email=content['email'], + phone_number=content['phone_number'], + first_name=content['first_name'], + last_name=content['last_name'], + name=content['name'], + avatar=content['avatar'] + ) + return user, content diff --git a/gooyal_accounts/settings.py b/gooyal_accounts/settings.py index fcecf00..e8c8863 100644 --- a/gooyal_accounts/settings.py +++ b/gooyal_accounts/settings.py @@ -70,7 +70,7 @@ OAUTH2_PROVIDER = { 'groups': 'Access to your groups', 'introspection': 'Introspect token scope'}, 'OAUTH2_VALIDATOR_CLASS': 'apps.gooyal_oauth2.validators.OAuth2Validator', - "INTROSPECTION_USER_CREATE_METHOD": 'apps.gooyal_oauth2.validators.get_user_profile', + "INTROSPECTION_USER_CREATE_METHOD": 'apps.users.models.create_user_in_introspection', } # GOOYAL_DYNAMIC_SCOPES