Skip to main content
https://img.shields.io/pypi/v/aiohttp-cookauth.svg

The library is a fork of aiohttp_session and aiohttp_security. The fork provides identity and authorization for aiohttp.web only via cookies using redis storage.

Features

  • added the ability to forget all user sessions using forget_all function

  • check_permission function return userid now

Installation

$ pip install aiohttp_cookauth

Example

from aiohttp import web
from aioredis import create_redis_pool
from aiohttp_cookauth import check_permission, \
    is_anonymous, remember, forget, \
    setup as setup_cookauth, RedisStorage, forget_all
from aiohttp_cookauth.abc import AbstractAuthorizationPolicy


# Demo authorization policy for only one user.
# User 'jack' has only 'listen' permission.
class SimpleJack_AuthorizationPolicy(AbstractAuthorizationPolicy):
    async def authorized_userid(self, identity):
        """Retrieve authorized user id.
        Return the user_id of the user identified by the identity
        or 'None' if no user exists related to the identity.
        """
        if identity == 'jack':
            return identity

    async def permits(self, identity, permission, context=None):
        """Check user permissions.
        Return True if the identity is allowed the permission
        in the current context, else return False.
        """
        return identity == 'jack' and permission in ('listen',)


async def handler_root(request):
    is_logged = not await is_anonymous(request)
    return web.Response(text='''<html><head></head><body>
            Hello, I'm Jack, I'm {logged} logged in.<br /><br />
            <a href="/login">Log me in</a><br />
            <a href="/logout">Log me out</a><br />
            <a href="/logout/all">Log out for all</a><br /><br />
            Check my permissions,
            when i'm logged in and logged out.<br />
            <a href="/listen">Can I listen?</a><br />
            <a href="/speak">Can I speak?</a><br />
        </body></html>'''.format(
            logged='' if is_logged else 'NOT',
        ), content_type='text/html')


async def handler_login_jack(request):
    redirect_response = web.HTTPFound('/')
    await remember(request, redirect_response, 'jack')
    return redirect_response


async def handler_logout(request):
    redirect_response = web.HTTPFound('/')
    await forget(request, redirect_response)
    return redirect_response


async def handler_logout_all(request):
    redirect_response = web.HTTPFound('/')
    await forget_all(request, identity='jack')
    return redirect_response


async def handler_listen(request):
    await check_permission(request, 'listen')
    return web.Response(body="I can listen!")


async def handler_speak(request):
    await check_permission(request, 'speak')
    return web.Response(body="I can speak!")


async def make_app():
    # make app
    app = web.Application()

    # add the routes
    app.add_routes([
        web.get('/', handler_root),
        web.get('/login', handler_login_jack),
        web.get('/logout', handler_logout),
        web.get('/logout/all', handler_logout_all),
        web.get('/listen', handler_listen),
        web.get('/speak', handler_speak)])

    # set up policies
    redis = await create_redis_pool(('localhost', 6379))
    storage = RedisStorage(redis, cookie_name='MY_SESSION', max_age=900)
    setup_cookauth(app, SimpleJack_AuthorizationPolicy(), storage)

    return app


if __name__ == '__main__':
    web.run_app(make_app(), port=9000)

Documentation

Use aiohttp_security documentation:

https://aiohttp-security.readthedocs.io/

License

aiohttp_cookauth is offered under the Apache 2 license.

Metadata

Release files for aiohttp-cookauth 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-cookauth 0.1.1
File Size Uploaded Provenance
aiohttp-cookauth-0.1.1.tar.gz 9.5 kB Without provenance Details

Release files / aiohttp-cookauth-0.1.1.tar.gz

Download URL aiohttp-cookauth-0.1.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
93ad1e4ae9b984be5f2a6760c4c9f7153e6ffcb95175da24a3cc53dd06ff48d6
BLAKE2b-256 checksum
How to use checksums
233d6bcf667a3be5d99762d0ddaa08ea3f756a70ca82c652834297cefcfa37d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.5

Security for aiohttp-cookauth 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page