aiohttp-basicauth
HTTP basic authentication middleware for aiohttp. Inspired by Flask-BasicAuth.
Requirements
- Python >= 3.8
- aiohttp >= 3.10
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 more complex user verification logic:
from aiohttp import web
from aiohttp_basicauth import BasicAuthMiddleware
class CustomBasicAuth(BasicAuthMiddleware):
async def check_credentials(self, username, password, request):
# here, for example, you can search user in the database by passed `username` and `password`, etc.
return username == 'user' and password == 'password'
auth = CustomBasicAuth()
app = web.Application(middlewares=[auth])
web.run_app(app, host='127.0.0.1', port=80)
Metadata
Release files for aiohttp-basicauth 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiohttp_basicauth-1.2.0.tar.gz | 7.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiohttp_basicauth-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / aiohttp_basicauth-1.2.0.tar.gz
| Download URL | aiohttp_basicauth-1.2.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc489f3fae191c1eca5bead09a152a23b57527bb74774e6d410aaeadb7cc63bb
|
|
BLAKE2b-256 checksum How to use checksums |
02538dbcc0ab82c8b5cd33b0e74633b3819848552a3b8d62b0bb15cc0df80c96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.5
|
Release files / aiohttp_basicauth-1.2.0-py3-none-any.whl
| Download URL | aiohttp_basicauth-1.2.0-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2a30373b0b799987eb61c215db3b62e4b4900a718d4a2b2170a15ee946854e71
|
|
BLAKE2b-256 checksum How to use checksums |
9854f2e5d09f65092b6a5e964ab10b807edb3b605a8589a67c798fb9c31c2a4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.5
|