## aiohttp-basicauth
[![CI](https://github.com/romis2012/aiohttp-basicauth/actions/workflows/ci.yml/badge.svg)](https://github.com/romis2012/aiohttp-basicauth/actions/workflows/ci.yml)
[![Coverage Status](https://codecov.io/gh/romis2012/aiohttp-basicauth/branch/master/graph/badge.svg)](https://codecov.io/gh/romis2012/aiohttp-basicauth)
[![PyPI version](https://badge.fury.io/py/aiohttp-basicauth.svg?_=a)](https://badge.fury.io/py/aiohttp-basicauth)
HTTP basic authentication middleware for aiohttp 3.0+.
Inspired by [Flask-BasicAuth](https://github.com/jpvanhal/flask-basicauth).
## Requirements
- Python >= 3.7
- aiohttp >= 3.0
## Installation
```
pip install aiohttp_basicauth
```
## Simple usage
```python
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)
```python
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:
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/romis2012/aiohttp-basicauth",
"name": "aiohttp-basicauth",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "aiohttp http basic auth",
"author": "Roman Snegirev",
"author_email": "snegiryev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ab/6f/8b762a981b8661fe33c9d50025e28c3476d9a8be76720af2aa5a5191fee1/aiohttp_basicauth-1.1.0.tar.gz",
"platform": null,
"description": "## aiohttp-basicauth\n\n[![CI](https://github.com/romis2012/aiohttp-basicauth/actions/workflows/ci.yml/badge.svg)](https://github.com/romis2012/aiohttp-basicauth/actions/workflows/ci.yml)\n[![Coverage Status](https://codecov.io/gh/romis2012/aiohttp-basicauth/branch/master/graph/badge.svg)](https://codecov.io/gh/romis2012/aiohttp-basicauth)\n[![PyPI version](https://badge.fury.io/py/aiohttp-basicauth.svg?_=a)](https://badge.fury.io/py/aiohttp-basicauth)\n\nHTTP basic authentication middleware for aiohttp 3.0+. \nInspired by [Flask-BasicAuth](https://github.com/jpvanhal/flask-basicauth).\n\n## Requirements\n- Python >= 3.7\n- aiohttp >= 3.0\n\n## Installation\n```\npip install aiohttp_basicauth\n```\n\n## Simple usage\n\n```python\nfrom aiohttp import web\nfrom aiohttp_basicauth import BasicAuthMiddleware\n\n\nauth = BasicAuthMiddleware(username='user', password='password')\napp = web.Application(middlewares=[auth])\n\nweb.run_app(app, host='127.0.0.1', port=80)\n```\n\n## Protect specific view(s)\n```python\nfrom aiohttp import web\nfrom aiohttp_basicauth import BasicAuthMiddleware\n\nauth = BasicAuthMiddleware(username='user', password='password', force=False)\n\n\nasync def public_view(request):\n return web.Response(text='Public view')\n\n\n@auth.required\nasync def secret_view(request):\n return web.Response(text='Secret view')\n\n\napp = web.Application(middlewares=[auth])\n\napp.router.add_route('GET', '/public', public_view)\napp.router.add_route('GET', '/secret', secret_view)\n\nweb.run_app(app, host='127.0.0.1', port=80)\n```\n\n## Advanced usage\n\nYou can override ```check_credentials``` method to implement more complex user verification logic:\n\n```python\nfrom aiohttp import web\nfrom aiohttp_basicauth import BasicAuthMiddleware\n\n\nclass CustomBasicAuth(BasicAuthMiddleware):\n async def check_credentials(self, username, password, request):\n # here, for example, you can search user in the database by passed `username` and `password`, etc.\n return username == 'user' and password == 'password'\n\n\nauth = CustomBasicAuth()\napp = web.Application(middlewares=[auth])\n\nweb.run_app(app, host='127.0.0.1', port=80)\n```\n",
"bugtrack_url": null,
"license": "Apache 2",
"summary": "HTTP basic authentication middleware for aiohttp 3.0+",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/romis2012/aiohttp-basicauth"
},
"split_keywords": [
"aiohttp",
"http",
"basic",
"auth"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "517e76a222aa4a723ccdf2ee7707430cc0847b324382a57d102e846550fe78a1",
"md5": "c81cc59c384dbf96f3b7726a3714b81d",
"sha256": "abcf94d35f568e087d57a012339a84e7ae17659afcdb9a8b4b1bea74bb0c2be8"
},
"downloads": -1,
"filename": "aiohttp_basicauth-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c81cc59c384dbf96f3b7726a3714b81d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7175,
"upload_time": "2024-10-02T12:05:27",
"upload_time_iso_8601": "2024-10-02T12:05:27.937864Z",
"url": "https://files.pythonhosted.org/packages/51/7e/76a222aa4a723ccdf2ee7707430cc0847b324382a57d102e846550fe78a1/aiohttp_basicauth-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ab6f8b762a981b8661fe33c9d50025e28c3476d9a8be76720af2aa5a5191fee1",
"md5": "eb1e6cc5df120203fcaf90729871d071",
"sha256": "f8a358e9fcd7146a3ca5f99ee3ef7c5fb89a21f11f9494b6a217c329ba6e2b1a"
},
"downloads": -1,
"filename": "aiohttp_basicauth-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "eb1e6cc5df120203fcaf90729871d071",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7823,
"upload_time": "2024-10-02T12:05:29",
"upload_time_iso_8601": "2024-10-02T12:05:29.344567Z",
"url": "https://files.pythonhosted.org/packages/ab/6f/8b762a981b8661fe33c9d50025e28c3476d9a8be76720af2aa5a5191fee1/aiohttp_basicauth-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-02 12:05:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "romis2012",
"github_project": "aiohttp-basicauth",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aiohttp-basicauth"
}