52 lines
1.5 KiB
Python
Executable file
52 lines
1.5 KiB
Python
Executable file
import base64
|
|
import binascii
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
from urllib.parse import unquote_plus
|
|
|
|
import requests
|
|
# import service_clients
|
|
from django.contrib.auth import get_user_model
|
|
from django.utils.timezone import make_aware
|
|
from oauth2_provider.models import get_access_token_model
|
|
from oauth2_provider.oauth2_validators import OAuth2Validator as BaseOAuth2Validator
|
|
from .settings import oauth2_settings
|
|
from django.conf import settings
|
|
|
|
log = logging.getLogger("oauth2_provider")
|
|
|
|
AccessTokenModel = get_access_token_model()
|
|
UserModel = get_user_model()
|
|
|
|
|
|
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(':')
|
|
|
|
if len(auth_fields) != 2:
|
|
return False
|
|
|
|
user_field, pass_field = auth_fields
|
|
|
|
if user_field not in ['phone_number', 'username', 'email']:
|
|
return False
|
|
|
|
if pass_field not in ['password', 'otp', 'ott']:
|
|
return False
|
|
|
|
if not username or not password:
|
|
return False
|
|
|
|
user = UserModel.objects.filter(**{user_field: username}).first()
|
|
|
|
if not user:
|
|
return False
|
|
|
|
if not user.check_auth(pass_field, password):
|
|
return False
|
|
|
|
if user.is_active:
|
|
request.user = user
|
|
return True
|
|
|
|
return False
|