Skip to main content

Proxy connector for aiohttp

Project description

aiohttp-basicauth

CI Coverage Status PyPI version versions

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)

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

aiohttp_basicauth-1.2.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_basicauth-1.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_basicauth-1.2.0.tar.gz.

File metadata

  • Download URL: aiohttp_basicauth-1.2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for aiohttp_basicauth-1.2.0.tar.gz
Algorithm Hash digest
SHA256 cc489f3fae191c1eca5bead09a152a23b57527bb74774e6d410aaeadb7cc63bb
MD5 1142de63cfb3144967e760fdd86f3cf4
BLAKE2b-256 02538dbcc0ab82c8b5cd33b0e74633b3819848552a3b8d62b0bb15cc0df80c96

See more details on using hashes here.

File details

Details for the file aiohttp_basicauth-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_basicauth-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a30373b0b799987eb61c215db3b62e4b4900a718d4a2b2170a15ee946854e71
MD5 15cf1b700106243d4d479e8c01cbbd03
BLAKE2b-256 9854f2e5d09f65092b6a5e964ab10b807edb3b605a8589a67c798fb9c31c2a4a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page