Example #1
0
    def test_empty_dict(self):
        access_token = access_tokens_api.create_access_token({})

        self.assertRaises(
            AuthenticationError,
            access_tokens_api.get_user_and_login_from_access_token,
            access_token)
Example #2
0
    def test_base(self):
        # Setup Test
        user_key = AuthUserEntity(username="******").put()
        user_id = get_resource_id_from_key(user_key)

        login_key = AuthUserMethodEntity(key=AuthUserMethodEntity.generate_key(
            user_key, 'basic', user_id),
                                         auth_type='basic',
                                         auth_key=user_id,
                                         auth_data='hashed_password:salt',
                                         user_key=user_key).put()

        user_data = {
            'id': user_id,
            'login_key': user_id,
            'login_type': 'basic',
            'version': 1
        }
        access_token = access_tokens_api.create_access_token(user_data)

        # Run Code To Test
        result = access_tokens_api.get_user_and_login_from_access_token(
            access_token)

        # Check results
        self.assertEquals(len(result), 2)
        self.assertTrue(isinstance(result[0], AuthUser))
        self.assertEquals(result[0].id, get_resource_id_from_key(user_key))

        self.assertTrue(isinstance(result[1], AuthUserMethodEntity))
        self.assertEquals(result[1].key, login_key)
Example #3
0
    def test_expired(self, m_datetime, m_expiration):
        # Create a token created 60 seconds ago and expired 30 seconds ago
        mock_iat = datetime.datetime.utcnow() - datetime.timedelta(seconds=60)
        mock_exp = datetime.datetime.utcnow() - datetime.timedelta(seconds=30)
        m_datetime.datetime.utcnow.return_value = mock_iat
        m_expiration.return_value = mock_exp

        access_token = access_tokens_api.create_access_token({'foo': 'bar'})

        # Run Code to Test
        self.assertRaises(AuthenticationError,
                          access_tokens_api.read_access_token, access_token)
Example #4
0
    def test_bad_secret(self, m_datetime, m_expiration):
        mock_iat = datetime.datetime.utcnow()
        mock_exp = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
        m_datetime.datetime.utcnow.return_value = mock_iat
        m_expiration.return_value = mock_exp

        access_token = access_tokens_api.create_access_token({'foo': 'bar'})

        with mock.patch(
                'auth_core.api.access_tokens.auth_settings') as m_settings:
            m_settings.JWT_SECRET = 'different_secret'
            self.assertRaises(AuthenticationError,
                              access_tokens_api.read_access_token,
                              access_token)
Example #5
0
    def test_simple(self, m_datetime, m_expiration):
        # Setup Tests
        mock_iat = datetime.datetime.utcnow()
        mock_exp = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
        m_datetime.datetime.utcnow.return_value = mock_iat
        m_expiration.return_value = mock_exp

        access_token = access_tokens_api.create_access_token({'foo': 'bar'})

        # Run Code to Test
        result = access_tokens_api.read_access_token(access_token)

        # Check Results
        self.assertEquals(result, {u'foo': u'bar'})
Example #6
0
    def test_invalid_login(self):
        user_key = AuthUserEntity(username="******").put()
        user_id = get_resource_id_from_key(user_key)

        login_key = AuthUserMethodEntity(key=AuthUserMethodEntity.generate_key(
            user_key, 'basic', user_id),
                                         auth_type='basic',
                                         auth_key=user_id,
                                         auth_data='hashed_password:salt',
                                         user_key=user_key).put()

        user_data = {
            'id': user_id,
            'login_key': user_id,
            'login_type': 'invalid_type',
            'version': 1
        }
        access_token = access_tokens_api.create_access_token(user_data)
        access_tokens_api.get_user_and_login_from_access_token(access_token)
Example #7
0
    def test_mocked(self, m_jwt, m_datetime, m_expiration):

        expected_iat = mock.Mock(name='mock_utcnow')
        expected_exp = mock.Mock(name='mock_later')
        m_datetime.datetime.utcnow.return_value = expected_iat
        m_expiration.return_value = expected_exp

        result = access_tokens_api.create_access_token({'foo': 'bar'})
        self.assertEqual(result, m_jwt.encode.return_value)

        claims = {
            'iss': 'jwt_issuer',
            'iat': expected_iat,
            'data': {
                'foo': 'bar'
            },
            'aud': 'jwt_aud',
            'exp': expected_exp
        }
        m_jwt.encode.assert_called_once_with(*[claims, 'jwt_secret'],
                                             **{'algorithm': 'HS256'})
        m_expiration.assert_called_once_with(expected_iat, 60 * 60)
Example #8
0
 def test_simple(self):
     # Test to ensure that we can generate a JWT and it contains 2 separators
     access_token = access_tokens_api.create_access_token({})
     self.assertEqual(access_token.count('.'), 2)
Example #9
0
def get_access_token_for_user_and_login(user, login):
    """
    """

    user_payload = access_tokens_api.make_token_user_data_dict(user, login)
    return access_tokens_api.create_access_token(user_payload)  # returns a jwt token