示例#1
0
    def test_delete_token(self):
        created_token = access_tokens.access_token_create(self.token_01)

        self.assertIsNotNone(created_token, "Could not create a Token")

        access_tokens.access_token_delete_by_token(created_token.access_token)

        fetched_token = access_tokens.access_token_get_by_token(
            created_token.access_token)
        self.assertIsNone(fetched_token, "A deleted token was fetched.")
示例#2
0
    def before(self, state):
        request = state.request

        if request.authorization and len(request.authorization) == 2:
            access_token = request.authorization[1]
            token = token_api.access_token_get_by_token(access_token)

            if token:
                request.current_user_id = token.user_id
                return

        request.current_user_id = None
def superuser():
    token = _get_token()

    if not token:
        return False

    token = token_api.access_token_get_by_token(token)

    if not token:
        return False

    user = user_api.user_get(token.user_id)

    if not user.is_superuser:
        abort(403, _("This action is limited to superusers only."))

    return user.is_superuser
示例#4
0
    def test_valid_refresh_token(self):
        """This test ensures that a valid refresh token can be converted into
        a valid access token, and cleans up after itself.
        """

        # Generate a valid access code
        with base.HybridSessionManager():
            authorization_code = auth_api.authorization_code_save({
                'user_id': 2,
                'state': 'test_state',
                'code': 'test_valid_code'
            })

        content_type = 'application/x-www-form-urlencoded'
        # Generate an auth and a refresh token.
        resp_1 = self.app.post('/v1/openid/token',
                               params={
                                   'code': authorization_code.code,
                                   'grant_type': 'authorization_code'
                               },
                               content_type=content_type,
                               expect_errors=True)

        # Assert that this is a successful response
        self.assertEqual(200, resp_1.status_code)

        # Assert that the token came back in the response
        t1 = resp_1.json

        # Assert that both are in the database.
        with base.HybridSessionManager():
            access_token = \
                token_api.access_token_get_by_token(t1['access_token'])
        self.assertIsNotNone(access_token)

        with base.HybridSessionManager():
            refresh_token = refresh_tokens.refresh_token_get_by_token(
                t1['refresh_token'])

        self.assertIsNotNone(refresh_token)

        content_type = 'application/x-www-form-urlencoded'
        # Issue a refresh token request.
        resp_2 = self.app.post('/v1/openid/token',
                               params={
                                   'refresh_token': t1['refresh_token'],
                                   'grant_type': 'refresh_token'
                               },
                               content_type=content_type,
                               expect_errors=True)

        # Assert that the response is good.
        self.assertEqual(200, resp_2.status_code)

        # Assert that the token came back in the response
        t2 = resp_2.json
        self.assertIsNotNone(t2['access_token'])
        self.assertIsNotNone(t2['expires_in'])
        self.assertIsNotNone(t2['id_token'])
        self.assertIsNotNone(t2['refresh_token'])
        self.assertIsNotNone(t2['token_type'])
        self.assertEqual('Bearer', t2['token_type'])

        # Assert that the access token is in the database
        with base.HybridSessionManager():
            new_access_token = \
                token_api.access_token_get_by_token(t2['access_token'])
        self.assertIsNotNone(new_access_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, new_access_token.user_id)
        self.assertEqual(t2['id_token'], new_access_token.user_id)
        self.assertEqual(t2['expires_in'], CONF.oauth.access_token_ttl)
        self.assertEqual(t2['expires_in'], new_access_token.expires_in)
        self.assertEqual(t2['access_token'],
                         new_access_token.access_token)

        # Assert that the refresh token is in the database

        with base.HybridSessionManager():
            new_refresh_token = refresh_tokens.refresh_token_get_by_token(
                t2['refresh_token'])

        self.assertIsNotNone(new_refresh_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, new_refresh_token.user_id)
        self.assertEqual(CONF.oauth.refresh_token_ttl,
                         new_refresh_token.expires_in)
        self.assertEqual(t2['refresh_token'],
                         new_refresh_token.refresh_token)

        # Assert that the old access tokens are no longer in the database and
        # have been cleaned up.

        with base.HybridSessionManager():
            no_access_token = \
                token_api.access_token_get_by_token(t1['access_token'])
        with base.HybridSessionManager():
            no_refresh_token = \
                refresh_tokens.refresh_token_get_by_token(t1['refresh_token'])

        self.assertIsNone(no_refresh_token)
        self.assertIsNone(no_access_token)
示例#5
0
    def test_valid_access_request(self):
        """This test ensures that the access token request may execute
        properly with a valid token.
        """

        # Generate a valid auth token
        with base.HybridSessionManager():
            authorization_code = auth_api.authorization_code_save({
                'user_id': 2,
                'state': 'test_state',
                'code': 'test_valid_code'
            })

        content_type = 'application/x-www-form-urlencoded'
        # POST with content: application/x-www-form-urlencoded
        response = self.app.post('/v1/openid/token',
                                 params={
                                     'code': authorization_code.code,
                                     'grant_type': 'authorization_code'
                                 },
                                 content_type=content_type,
                                 expect_errors=True)

        # Assert that this is a successful response
        self.assertEqual(200, response.status_code)

        # Assert that the token came back in the response
        token = response.json
        self.assertIsNotNone(token['access_token'])
        self.assertIsNotNone(token['expires_in'])
        self.assertIsNotNone(token['id_token'])
        self.assertIsNotNone(token['refresh_token'])
        self.assertIsNotNone(token['token_type'])
        self.assertEqual('Bearer', token['token_type'])

        # Assert that the access token is in the database
        with base.HybridSessionManager():
            access_token = \
                token_api.access_token_get_by_token(token['access_token'])
        self.assertIsNotNone(access_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, access_token.user_id)
        self.assertEqual(token['id_token'], access_token.user_id)
        self.assertEqual(token['expires_in'], CONF.oauth.access_token_ttl)
        self.assertEqual(token['expires_in'], access_token.expires_in)
        self.assertEqual(token['access_token'], access_token.access_token)

        # Assert that the refresh token is in the database
        with base.HybridSessionManager():
            refresh_token = \
                refresh_tokens.refresh_token_get_by_token(
                    token['refresh_token'])

        self.assertIsNotNone(refresh_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, refresh_token.user_id)
        self.assertEqual(CONF.oauth.refresh_token_ttl,
                         refresh_token.expires_in)
        self.assertEqual(token['refresh_token'], refresh_token.refresh_token)

        # Assert that the authorization code is no longer in the database.
        with base.HybridSessionManager():
            none_code = \
                auth_api.authorization_code_get(authorization_code.code)
        self.assertIsNone(none_code)
示例#6
0
    def test_valid_refresh_token(self):
        """This test ensures that a valid refresh token can be converted into
        a valid access token, and cleans up after itself.
        """

        # Generate a valid access code
        with base.HybridSessionManager():
            authorization_code = auth_api.authorization_code_save({
                'user_id':
                2,
                'state':
                'test_state',
                'code':
                'test_valid_code'
            })

        content_type = 'application/x-www-form-urlencoded'
        # Generate an auth and a refresh token.
        resp_1 = self.app.post('/v1/openid/token',
                               params={
                                   'code': authorization_code.code,
                                   'grant_type': 'authorization_code'
                               },
                               content_type=content_type,
                               expect_errors=True)

        # Assert that this is a successful response
        self.assertEqual(200, resp_1.status_code)

        # Assert that the token came back in the response
        t1 = resp_1.json

        # Assert that both are in the database.
        with base.HybridSessionManager():
            access_token = \
                token_api.access_token_get_by_token(t1['access_token'])
        self.assertIsNotNone(access_token)

        with base.HybridSessionManager():
            refresh_token = refresh_tokens.refresh_token_get_by_token(
                t1['refresh_token'])

        self.assertIsNotNone(refresh_token)

        content_type = 'application/x-www-form-urlencoded'
        # Issue a refresh token request.
        resp_2 = self.app.post('/v1/openid/token',
                               params={
                                   'refresh_token': t1['refresh_token'],
                                   'grant_type': 'refresh_token'
                               },
                               content_type=content_type,
                               expect_errors=True)

        # Assert that the response is good.
        self.assertEqual(200, resp_2.status_code)

        # Assert that the token came back in the response
        t2 = resp_2.json
        self.assertIsNotNone(t2['access_token'])
        self.assertIsNotNone(t2['expires_in'])
        self.assertIsNotNone(t2['id_token'])
        self.assertIsNotNone(t2['refresh_token'])
        self.assertIsNotNone(t2['token_type'])
        self.assertEqual('Bearer', t2['token_type'])

        # Assert that the access token is in the database
        with base.HybridSessionManager():
            new_access_token = \
                token_api.access_token_get_by_token(t2['access_token'])
        self.assertIsNotNone(new_access_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, new_access_token.user_id)
        self.assertEqual(t2['id_token'], new_access_token.user_id)
        self.assertEqual(t2['expires_in'], CONF.oauth.access_token_ttl)
        self.assertEqual(t2['expires_in'], new_access_token.expires_in)
        self.assertEqual(t2['access_token'], new_access_token.access_token)

        # Assert that the refresh token is in the database

        with base.HybridSessionManager():
            new_refresh_token = refresh_tokens.refresh_token_get_by_token(
                t2['refresh_token'])

        self.assertIsNotNone(new_refresh_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, new_refresh_token.user_id)
        self.assertEqual(CONF.oauth.refresh_token_ttl,
                         new_refresh_token.expires_in)
        self.assertEqual(t2['refresh_token'], new_refresh_token.refresh_token)

        # Assert that the old access tokens are no longer in the database and
        # have been cleaned up.

        with base.HybridSessionManager():
            no_access_token = \
                token_api.access_token_get_by_token(t1['access_token'])
        with base.HybridSessionManager():
            no_refresh_token = \
                refresh_tokens.refresh_token_get_by_token(t1['refresh_token'])

        self.assertIsNone(no_refresh_token)
        self.assertIsNone(no_access_token)
示例#7
0
    def test_valid_access_request(self):
        """This test ensures that the access token request may execute
        properly with a valid token.
        """

        # Generate a valid auth token
        with base.HybridSessionManager():
            authorization_code = auth_api.authorization_code_save({
                'user_id':
                2,
                'state':
                'test_state',
                'code':
                'test_valid_code'
            })

        content_type = 'application/x-www-form-urlencoded'
        # POST with content: application/x-www-form-urlencoded
        response = self.app.post('/v1/openid/token',
                                 params={
                                     'code': authorization_code.code,
                                     'grant_type': 'authorization_code'
                                 },
                                 content_type=content_type,
                                 expect_errors=True)

        # Assert that this is a successful response
        self.assertEqual(200, response.status_code)

        # Assert that the token came back in the response
        token = response.json
        self.assertIsNotNone(token['access_token'])
        self.assertIsNotNone(token['expires_in'])
        self.assertIsNotNone(token['id_token'])
        self.assertIsNotNone(token['refresh_token'])
        self.assertIsNotNone(token['token_type'])
        self.assertEqual('Bearer', token['token_type'])

        # Assert that the access token is in the database
        with base.HybridSessionManager():
            access_token = \
                token_api.access_token_get_by_token(token['access_token'])
        self.assertIsNotNone(access_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, access_token.user_id)
        self.assertEqual(token['id_token'], access_token.user_id)
        self.assertEqual(token['expires_in'], CONF.oauth.access_token_ttl)
        self.assertEqual(token['expires_in'], access_token.expires_in)
        self.assertEqual(token['access_token'], access_token.access_token)

        # Assert that the refresh token is in the database
        with base.HybridSessionManager():
            refresh_token = \
                refresh_tokens.refresh_token_get_by_token(
                    token['refresh_token'])

        self.assertIsNotNone(refresh_token)

        # Assert that system configured values is owned by the correct user.
        self.assertEqual(2, refresh_token.user_id)
        self.assertEqual(CONF.oauth.refresh_token_ttl,
                         refresh_token.expires_in)
        self.assertEqual(token['refresh_token'], refresh_token.refresh_token)

        # Assert that the authorization code is no longer in the database.
        with base.HybridSessionManager():
            none_code = \
                auth_api.authorization_code_get(authorization_code.code)
        self.assertIsNone(none_code)
示例#8
0
 def test_get_not_existing(self):
     self.assertIsNone(
         access_tokens.access_token_get_by_token("not_a_token"))
示例#9
0
 def test_get_by_prefix(self):
     # This test checks that a token is not fetch by LIKE comparison
     self.assertIsNone(
         access_tokens.access_token_get_by_token("valid_user_t"))
示例#10
0
 def test_get_existing_token(self):
     self.assertIsNotNone(
         access_tokens.access_token_get_by_token("valid_user_token"))