def get_user_from_token(token): """ Return a User model record based on the token. :param token: token dictionary object :return: User Model record """ # retrieve our oauth2 application model. app = get_application_model().objects.get(name=settings.OAUTH2_APPLICATION_NAME) # Retrieve the access token and refresh token for the user. try: at = AccessToken.objects.get(application=app, token=token['access_token']) # Hit the db indexes. except: return None # Check to see if the access token has expired. if at.expires < now(): return None try: return get_user_model().objects.get(pk=at.user_id) except ObjectDoesNotExist: pass return None
from urllib.parse import parse_qs, urlparse from oauth2_provider.models import get_application_model from oauth2_provider.compat import get_user_model from oauth2_provider.tests.test_utils import TestCaseUtils from oauth2_provider.settings import oauth2_settings import json from rest_framework.test import APIRequestFactory, APIClient, APITestCase from rest_framework import status from django.contrib.auth.models import User from authentication.views import UserViewSet Application = get_application_model() UserModel = get_user_model() class BaseTest(TestCaseUtils, TestCase): def setUp(self): self.factory = RequestFactory() self.test_user = UserModel.objects.create_user("test_user", "*****@*****.**", "123456") self.dev_user = UserModel.objects.create_user("dev_user", "*****@*****.**", "123456") self.application = Application( name="Test Password Application", user=self.dev_user, client_type=Application.CLIENT_PUBLIC, authorization_grant_type=Application.GRANT_PASSWORD, ) self.application.save()
from rest_framework.reverse import reverse from rest_framework.test import APITestCase from oauth2_provider.models import get_application_model from oauth2_provider.compat import get_user_model from oauth2_provider.settings import oauth2_settings import json Application = get_application_model() UserModel = get_user_model() class BaseAuthTest(APITestCase): def setUp(self): self.test_user = UserModel.objects.create_user("test_user", "*****@*****.**", "123456") self.dev_user = UserModel.objects.create_user("dev_user", "*****@*****.**", "123456") oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES = [ 'http', 'custom-scheme' ] self.application = Application( name="Test Application", redirect_uris= "http://localhost http://example.com http://example.it custom-scheme://example.com", user=self.dev_user, client_type=Application.CLIENT_CONFIDENTIAL, authorization_grant_type="password", )