1 Commits

Author SHA1 Message Date
Sam Bull
43f59a21fc Move indexing back into .identify() method. 2020-12-19 16:44:41 +00:00
3 changed files with 9 additions and 8 deletions

View File

@@ -15,11 +15,12 @@ AUTH_SCHEME = 'Bearer '
class JWTIdentityPolicy(AbstractIdentityPolicy): class JWTIdentityPolicy(AbstractIdentityPolicy):
def __init__(self, secret, algorithm='HS256'): def __init__(self, secret, algorithm='HS256', key: str = 'login'):
if jwt is None: if jwt is None:
raise RuntimeError('Please install `PyJWT`') raise RuntimeError('Please install `PyJWT`')
self.secret = secret self.secret = secret
self.algorithm = algorithm self.algorithm = algorithm
self.key = key
async def identify(self, request): async def identify(self, request):
header_identity = request.headers.get(AUTH_HEADER_NAME) header_identity = request.headers.get(AUTH_HEADER_NAME)
@@ -36,7 +37,7 @@ class JWTIdentityPolicy(AbstractIdentityPolicy):
identity = jwt.decode(token, identity = jwt.decode(token,
self.secret, self.secret,
algorithms=[self.algorithm]) algorithms=[self.algorithm])
return identity return identity.get(self.key)
async def remember(self, *args, **kwargs): # pragma: no cover async def remember(self, *args, **kwargs): # pragma: no cover
pass pass

View File

@@ -1,11 +1,11 @@
-e . -e .
flake8==3.8.4 flake8==3.8.4
async-timeout==3.0.1 async-timeout==3.0.1
pytest==6.2.1 pytest==6.1.2
pytest-cov==2.10.1 pytest-cov==2.10.1
pytest-mock==3.4.0 pytest-mock==3.3.1
coverage==5.3.1 coverage==5.3
sphinx==3.4.1 sphinx==3.3.1
pep257==0.7.0 pep257==0.7.0
aiohttp-session==2.9.0 aiohttp-session==2.9.0
aiopg[sa]==1.1.0 aiopg[sa]==1.1.0
@@ -15,4 +15,4 @@ passlib==1.7.4
cryptography==3.3.1 cryptography==3.3.1
aiohttp==3.7.3 aiohttp==3.7.3
pytest-aiohttp==0.3.0 pytest-aiohttp==0.3.0
pyjwt==2.0.1 pyjwt==1.7.1

View File

@@ -43,7 +43,7 @@ async def test_identify(loop, make_token, aiohttp_client):
async def check(request): async def check(request):
policy = request.app[IDENTITY_KEY] policy = request.app[IDENTITY_KEY]
identity = await policy.identify(request) identity = await policy.identify(request)
assert 'Andrew' == identity['login'] assert 'Andrew' == identity
return web.Response() return web.Response()
app = web.Application() app = web.Application()