def create_oauth2_client(user): """ Create an OAuth2 client associated with the given user and generate an access token for said client. :param user: :return: a Client (provider.oauth2) and an AccessToken """ # Register an OAuth2 Client client = OAuth2Client( user=user, name=user.username, url="http://127.0.0.1/", redirect_uri="http://127.0.0.1/", client_type=constants.CONFIDENTIAL ) client.save() # Generate an access token for the client access_token = AccessToken( user=user, client=client, # Set the access token to expire one day from now expires=timezone.now() + timedelta(1, 0), scope=constants.READ_WRITE ) access_token.save() return client, access_token
def create_access_token(user_auth, username, password, http_host): secret_cli = Client(user=user_auth, name="client", client_type=1, url="") secret_cli.save(using=GmApps.AFTERBUY) access_token = AccessToken(user=user_auth, client=secret_cli, scope=6) AccessToken.objects.filter(user=user_auth, client=secret_cli, scope=6).using(GmApps.AFTERBUY).delete() access_token.save(using=GmApps.AFTERBUY) return access_token.token
def setup(self): super(TestAppPreferencesResourceApi, self).setUp() self.access_token = 'testaccesstoken' user = User.objects.create_user(username='******', email='*****@*****.**',password='******') secret_cli = auth_client(user=user, name='client', client_type=1, url='') secret_cli.save() access = AccessToken(user=user, token=self.access_token, client=secret_cli) access.save()
def create_access_token(user): # Create a Oauth2 Client (each test gets a new one) oauth2_client = Client.objects.create( user=user, name="example.com testclient", client_type=1, # ?? url="http://example.com") # Create a Oauth2 AccessToken (each test gets a new one) access_token = AccessToken(user=user, client=oauth2_client) access_token.save() # Return HTTP authorization header return "Authorization: OAuth {}".format(access_token.token)
def create_access_token(user_auth, http_host): ''' Used for creating access token :param user_auth: :type user_auth: :param http_host: :type http_host: ''' secret_cli = Client(user=user_auth, name='client', client_type=1, url='') secret_cli.save(using=settings.BRAND) access_token = AccessToken( user=user_auth, client=secret_cli, scope=6 ) AccessToken.objects.filter(user=user_auth, client=secret_cli, scope=6).using(settings.BRAND).delete() access_token.save(using=settings.BRAND) return access_token.token
def get_id_token(user): """ Generates JWT ID-Token, using or creating user's OAuth access token. """ try: client = Client.objects.get(name="edx-notes") except Client.DoesNotExist: raise ImproperlyConfigured( "OAuth2 Client with name 'edx-notes' is not present in the DB") try: access_token = AccessToken.objects.get(client=client, user=user, expires__gt=now()) except AccessToken.DoesNotExist: access_token = AccessToken(client=client, user=user) access_token.save() id_token = oidc.id_token(access_token) secret = id_token.access_token.client.client_secret return id_token.encode(secret)
def get_id_token(user): """ Generates JWT ID-Token, using or creating user's OAuth access token. """ try: client = Client.objects.get(name="edx-notes") except Client.DoesNotExist: raise ImproperlyConfigured("OAuth2 Client with name 'edx-notes' is not present in the DB") try: access_token = AccessToken.objects.get( client=client, user=user, expires__gt=now() ) except AccessToken.DoesNotExist: access_token = AccessToken(client=client, user=user) access_token.save() id_token = oidc.id_token(access_token) secret = id_token.access_token.client.client_secret return id_token.encode(secret)
def create_token(user, client): token = AccessToken(client=client, user=user) token.save() return token