47 lines
1.4 KiB
Python
Executable file
47 lines
1.4 KiB
Python
Executable file
from oauth2_provider.oauth2_validators import OAuth2Validator
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
USER_MODEL = get_user_model()
|
|
|
|
|
|
class MultiGatewayOAuth2Validator(OAuth2Validator): # pylint: disable=w0223
|
|
""" Primarily extend the functionality of token generation """
|
|
|
|
def validate_user(self, username, password, client, request, *args, **kwargs):
|
|
""" Here, you would be able to access the MOBILE/ OTP fields
|
|
which you will be sending in the request.post body. """
|
|
# otp = request.otp
|
|
# mobile = request.mobile
|
|
# user = AppropriateModel.objects.get(otp=otp, mobile=mobile)
|
|
auth_fields = getattr(request, 'auth_fields', 'username:password').split(':')
|
|
user_field = auth_fields[0]
|
|
pass_field = auth_fields[1]
|
|
|
|
user = None
|
|
if user_field == 'phone_number':
|
|
user = USER_MODEL.objects.get(
|
|
phone_number=username
|
|
)
|
|
|
|
elif user_field == 'username':
|
|
user = USER_MODEL.objects.get(
|
|
username=username
|
|
)
|
|
|
|
elif user_field == 'email':
|
|
user = USER_MODEL.objects.get(
|
|
email=username
|
|
)
|
|
|
|
if user is None:
|
|
return False
|
|
|
|
if not user.check_auth(pass_field, password):
|
|
return False
|
|
|
|
if user.is_active:
|
|
request.user = user
|
|
return True
|
|
|
|
return False
|