Skip to content

Commit

Permalink
Merge pull request #65 from xsnippet/auth
Browse files Browse the repository at this point in the history
Rewrite "auth" middleware as new-style middleware
  • Loading branch information
malor committed Jan 21, 2018
2 parents b9dd47f + 0558bc8 commit bf36a5f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
4 changes: 1 addition & 3 deletions tests/middlewares/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
:license: MIT, see LICENSE for details
"""

import functools

import aiohttp.web as web
import jose.jwt as jwt
import pytest
Expand All @@ -20,7 +18,7 @@
@pytest.fixture(scope='function')
async def testapp(test_client):
app = web.Application(middlewares=[
functools.partial(middlewares.auth.auth, {'secret': 'SWORDFISH'})
middlewares.auth.auth({'secret': 'SWORDFISH'}),
])

async def handler(request):
Expand Down
4 changes: 1 addition & 3 deletions xsnippet/api/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
:license: MIT, see LICENSE for details
"""

import functools

import aiohttp.web

from . import database, router, middlewares, resources
Expand Down Expand Up @@ -64,7 +62,7 @@ def create_app(conf):
# decorator, so they can be collected and passed to VersionRouter.
app = aiohttp.web.Application(
middlewares=[
functools.partial(middlewares.auth.auth, conf['auth']),
middlewares.auth.auth(conf['auth']),
],
router=router.VersionRouter({'1.0': v1}))
app.on_startup.append(middlewares.auth.setup)
Expand Down
13 changes: 6 additions & 7 deletions xsnippet/api/middlewares/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def setup(app):
conf['secret'] = secret


async def auth(conf, app, next_handler):
def auth(conf):
"""Authentication middleware.
Performs user authentication by validating tokens passed in Authorization
Expand All @@ -46,9 +46,9 @@ async def auth(conf, app, next_handler):
On failure 401 Unauthorized error is raised.
"""

secret = conf['secret']

async def auth_handler(request):
@web.middleware
async def _auth(request, handler):
secret = conf['secret']
authorization = request.headers.get('Authorization')

if authorization is not None:
Expand All @@ -68,6 +68,5 @@ async def auth_handler(request):
else:
request['auth'] = None

return await next_handler(request)

return auth_handler
return await handler(request)
return _auth

0 comments on commit bf36a5f

Please sign in to comment.