HTTP basic authentication middleware for aiohttp 3.0+
Project description
aiohttp-basicauth
HTTP basic authentication middleware for aiohttp 3.0+. Inspired by Flask-BasicAuth.
Requirements
- Python >= 3.5.3
- aiohttp >= 3.0
Installation
pip install aiohttp_basicauth
Simple usage
from aiohttp import web from aiohttp_basicauth import BasicAuthMiddleware auth = BasicAuthMiddleware(username='user', password='password') app = web.Application(middlewares=[auth]) web.run_app(app, host='127.0.0.1', port=80)
Protect specific view(s)
from aiohttp import web from aiohttp_basicauth import BasicAuthMiddleware auth = BasicAuthMiddleware(username='user', password='password', force=False) async def public_view(request): return web.Response(text='Public view') @auth.required async def secret_view(request): return web.Response(text='Secret view') app = web.Application(middlewares=[auth]) app.router.add_route('GET', '/public', public_view) app.router.add_route('GET', '/secret', secret_view) web.run_app(app, host='127.0.0.1', port=80)
Advanced usage
You can override check_credentials
method to implement specific user verification logic:
from aiohttp import web from aiohttp_basicauth import BasicAuthMiddleware class CustomBasicAuth(BasicAuthMiddleware): async def check_credentials(self, username, password, request): return username == 'user' and password == 'password' auth = CustomBasicAuth() app = web.Application(middlewares=[auth]) web.run_app(app, host='127.0.0.1', port=80)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size aiohttp_basicauth-0.2.0-py3-none-any.whl (7.0 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size aiohttp_basicauth-0.2.0.tar.gz (6.8 kB) | File type Source | Python version None | Upload date | Hashes View |
Close
Hashes for aiohttp_basicauth-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58c957664079e933a00bb4b5ab17503ebc7dd8d7ab6e66bdb6ba2392029592bb |
|
MD5 | 7f2d04a5ffc9dcfae88896dccc09a82f |
|
BLAKE2-256 | ad5ad56905006641d2d98763a3dbc6b04a9bd645e58a8f8b5944faf1535792a4 |