https://github.com/bugov/aiohttp-basicauth-middleware
🌍👮 Aiohttp middleware for simple http basic auth protection for some urls
https://github.com/bugov/aiohttp-basicauth-middleware
aiohttp-middleware basic-authentication http middleware protection python python3
Last synced: 14 days ago
JSON representation
🌍👮 Aiohttp middleware for simple http basic auth protection for some urls
- Host: GitHub
- URL: https://github.com/bugov/aiohttp-basicauth-middleware
- Owner: bugov
- License: unlicense
- Created: 2018-02-20T11:20:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2025-09-25T02:28:22.000Z (6 months ago)
- Last Synced: 2025-12-22T01:28:33.595Z (3 months ago)
- Topics: aiohttp-middleware, basic-authentication, http, middleware, protection, python, python3
- Language: Python
- Homepage:
- Size: 31.3 KB
- Stars: 15
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# aiohttp-basicauth-middleware
[](https://travis-ci.org/bugov/aiohttp-basicauth-middleware)
Aiohttp middleware for simple http basic
auth protection for some urls.
Works with Python ≥ 3.6.
Works with UTF-8 🖖
## Installation
```python
pip install aiohttp-basicauth-middleware
```
## Usage
```python
app = web.Application(loop=loop)
app.router.add_route('GET', '/hello', handler_a)
app.router.add_route('GET', '/admin/hello', handler_b)
app.middlewares.append(
basic_auth_middleware(
('/admin',),
{'user': 'password'},
)
)
```
`basic_auth_middleware` has 3 params:
1. list of protected urls. For example `['/admin']` will match
with `/admin/user`, but will not match with `/user/admin`.
2. auth dict – a dict with pairs: login-password.
3. strategy (optional) for password comparision. For example you can
store hashed password in `auth_dict`. See `aiohttp_basicauth_middleware.strategy.BaseStrategy` and
`example.strategy` for more information.
Example with md5 password hashing:
```python
app = web.Application(loop=loop)
app.router.add_route('GET', '/hello', handler_a)
app.router.add_route('GET', '/admin/hello', handler_b)
app.middlewares.append(
basic_auth_middleware(
('/admin',),
{'user': '5f4dcc3b5aa765d61d8327deb882cf99'},
lambda x: hashlib.md5(bytes(x, encoding='utf-8')).hexdigest(),
)
)
```
`/admin/...` will be accessed by the same login+password pair ('user', 'password').