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):
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for aiohttp_basicauth-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 277d071c29256e10b4650f1bffee7c2007d914da0c2036e2ab621eb7b2096b2d |
|
MD5 | bdc2cc18d8b1760458e329f1bd6ee95e |
|
BLAKE2b-256 | a34210ad721f710b7d3efdb961c9848b0cda3356f714008de9e4e1cc94c0c9c4 |