Пример #1
0
def test_endtoend_1():
    """Testing create and decode of token via request header."""
    token_dict = create_web_token(
        'jwt-access-private', 'issuer', 'app', 300, data={'foo': 'bar'})
    key, value = create_authorization_header(token_dict)
    encoded_token = extract_token(value)
    decoded_token = decode_web_token(encoded_token, 'jwt-access-public', 'app')
    assert {'foo': 'bar'} == decoded_token['data']
Пример #2
0
 def extractCredentials(self, request):
     if self.canHandle(request._environ):
         import pdb
         pdb.set_trace()
         if request._auth:
             access_token = extract_token(request._auth)
             if access_token:
                 return {"access_token": access_token}
     return None
Пример #3
0
def test_endtoend_2():
    """`decode_web_token()` raises a ValueError if the token is expired."""
    token_dict = create_web_token(
        'jwt-access-private', 'issuer', 'app', -1, data={'foo': 'bar'})
    header = create_authorization_header(token_dict)
    encoded_token = extract_token(dict([header]))
    with pytest.raises(ValueError) as err:
        decode_web_token(encoded_token, 'jwt-access-public', 'app')
    assert 'Signature has expired' == str(err.value)
Пример #4
0
def test_header__extract_token__7():
    """`extract_token()` extracts token from given Mapping object."""
    class MyHeaders(collections.Mapping):
        """Example headers implementation based on `Mapping ."""

        def __init__(self, data):
            self.data = data

        def __getitem__(self, key):
            return self.data[key]

        def __iter__(self):
            return iter(self.data)  # pragma: no cover only needed for instance

        def __len__(self):
            return len(self.data)  # pragma: no cover only needed for instance

    headers = MyHeaders({'Authorization': 'Bearer <TOKEN>'})
    assert b'<TOKEN>' == extract_token(headers)
Пример #5
0
def test_header__extract_token__6():
    """`extract_token()`  raises ValueError if scheme is missing."""
    headers = dict(Authorization='<TOKEN>')
    with pytest.raises(ValueError) as err:
        extract_token(headers)
    assert 'Authorization scheme is not Bearer' == str(err.value)
Пример #6
0
def test_header__extract_token__5():
    """`extract_token()`  raises ValueError on wrong Authorization scheme."""
    headers = dict(Authorization='Foobar <TOKEN>')
    with pytest.raises(ValueError) as err:
        extract_token(headers)
    assert 'Authorization scheme is not Bearer' == str(err.value)
Пример #7
0
def test_header__extract_token__4():
    """`extract_token()`  raises ValueError if Authorization key is missing."""
    with pytest.raises(ValueError) as err:
        extract_token({})
    assert 'Missing Authorization header' == str(err.value)
Пример #8
0
def test_header__extract_token__3():
    """`extract_token()` allows scheme to be lower case."""
    assert b'<TOKEN>' == extract_token('bearer <TOKEN>')
Пример #9
0
def test_header__extract_token__2():
    """`extract_token()` extracts token from given value."""
    assert b'<TOKEN>' == extract_token('Bearer <TOKEN>')
Пример #10
0
def test_header__extract_token__1():
    """`extract_token()` extracts token from given dict."""
    headers = dict(Authorization='Bearer <TOKEN>')
    assert b'<TOKEN>' == extract_token(headers)