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.7
- 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 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)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiohttp_basicauth-1.1.0.tar.gz
.
File metadata
- Download URL: aiohttp_basicauth-1.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a358e9fcd7146a3ca5f99ee3ef7c5fb89a21f11f9494b6a217c329ba6e2b1a |
|
MD5 | eb1e6cc5df120203fcaf90729871d071 |
|
BLAKE2b-256 | ab6f8b762a981b8661fe33c9d50025e28c3476d9a8be76720af2aa5a5191fee1 |
File details
Details for the file aiohttp_basicauth-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_basicauth-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abcf94d35f568e087d57a012339a84e7ae17659afcdb9a8b4b1bea74bb0c2be8 |
|
MD5 | c81cc59c384dbf96f3b7726a3714b81d |
|
BLAKE2b-256 | 517e76a222aa4a723ccdf2ee7707430cc0847b324382a57d102e846550fe78a1 |