aiohttp-basicauth

raw JSON →
1.1.0 verified Fri May 01 auth: no python

HTTP basic authentication middleware for aiohttp 3.0+. Current version 1.1.0. This library provides a simple middleware to add HTTP basic authentication to aiohttp web applications. Maintenance is infrequent.

pip install aiohttp-basicauth
error ImportError: No module named 'aiohttp_basicauth_middleware'
cause Wrong import path; the package name is `aiohttp-basicauth` but the module is `aiohttp_basicauth`.
fix
Use from aiohttp_basicauth import BasicAuth.
error TypeError: object NoneType can't be used in 'await' expression
cause Forgetting to use `await` on an async handler that calls `check_credentials` directly, or misconfiguration.
fix
Ensure your handler is async and you await calls if you override check_credentials.
breaking The `setup` method configures the middleware globally on the app; you cannot apply per-route auth easily without the `ignore` decorator (added in v1.1.0).
fix Upgrade to v1.1.0 and use `@auth.ignore` on handlers you want to exclude.
gotcha The middleware checks credentials on every request, including static files. If you serve static files publicly, use the `ignore` decorator to exclude those routes.
fix Apply `@auth.ignore` to static file handlers or use a separate sub-app.
deprecated Passing `check_credentials` callable with a request parameter (old signature) is deprecated. New signature: `check_credentials(username, password)`.
fix Update your callable to accept only `username` and `password` (no request).

Set up basic auth middleware on an aiohttp app.

from aiohttp import web
from aiohttp_basicauth import BasicAuth

auth = BasicAuth(username='admin', password='secret')

async def handler(request):
    return web.Response(text='Authenticated!')

app = web.Application()
app.router.add_get('/', handler)
auth.setup(app)

web.run_app(app)