Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

datasette-auth-existing-cookies

PyPI CircleCI License

Datasette plugin that authenticates users based on existing domain cookies.

STATUS: Work in progress.

When to use this

This plugin allows you to build custom authentication for Datasette when you are hosting a Datasette instance on the same domain as another, authenticated website.

Consider a website on www.example.com which supports user authentication.

You could run Datasette on data.example.com in a way that lets it see cookies that were set for the .example.com domain.

Using this plugin, you could build an API endpoint at www.example.com/user-for-cookies which returns a JSON object representing the currently signed-in user, based on their cookies.

The plugin can protect any hits to any data.example.com pages by passing their cookies through to that API and seeing if the user should be logged in or not.

You can also use subclassing to decode existing cookies using some other mechanism.

Login redirect mechanism

If the user does not have a valid authentication cookie they will be redirected to an existing login page.

This page is specified using the auth_redirect_url setting.

For example:

{
    "plugins": {
        "datasette-auth-existing-cookies": {
            "api_url": "http://www.example.com/user-from-cookies",
            "auth_redirect_url": "http://www.example.com/login",
            "original_cookies": ["sessionid"]
        }
    }
}

The URL that the user should be sent to after they log in will be specified as the ?next= parameter to that page, for example:

http://www.example.com/login?next=http://foo.example.com/

It is up to you to program the login endpoint such that it is not vulnerable to an Unvalidated redirect vulnerability.

One way to do this is by verifying that the URL passed to ?next= is a URL that belongs to a trusted website. Django's own login view does this by verifying that the URL hostname is on an approved list.

Another way to do this is to use the next_secret configuration parameter to set a signing secret for that URL. This signing secret will be used to construct a ?next_sig= signed token using the Python itsdangerous module, like this:

?next_sig=Imh0dHBzOi8vZGVtby5leGFtcGxlLmNvbS9mb28vYmFyIg.7JdhRCoP7Ow1cRF1ZVengC-qk6c

You should use Datasette's secret configuration values mechanism to set this secret from an environment variable, like so:

{
    "plugins": {
        "datasette-auth-existing-cookies": {
            "api_url": "http://www.example.com/user-from-cookies",
            "auth_redirect_url": "http://www.example.com/login",
            "original_cookies": ["sessionid"],
            "next_secret":  {
                "$env": "NEXT_SECRET"
            }
        }
    }
}

You can verify this secret in Python code for your own login form like so:

from itsdangerous import URLSafeSerializer, BadSignature

def verify_next_sig(next_sig):
    signer = URLSafeSerializer(next_secret)
    try:
        decoded = signer.loads(next_sig)
        return True
    except BadSignature:
        return False

If you want to roll your own signing mechanism here you can do so by subclassing ExistingCookiesAuth and over-riding the build_auth_redirect(next_url) method.

Metadata

Release files for datasette-auth-existing-cookies 0.3b0

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

Built distribution (wheel)

Table of built distributions (wheels) for datasette-auth-existing-cookies 0.3b0
File Interpreter ABI Platform
datasette_auth_existing_cookies-0.3b0-py3-none-any.whl Python 3 none any Details

Release files / datasette_auth_existing_cookies-0.3b0-py3-none-any.whl

Download URL datasette_auth_existing_cookies-0.3b0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
acf509ef75842e26d131ccf1882ecc0cd60b03bc032bf87dc00441b15b9ad071
BLAKE2b-256 checksum
How to use checksums
af82c57f70d22b710ea8f6b033bb8de254e884dc0e78fb5c54e65dcb34d3d6c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

Security for datasette-auth-existing-cookies 0.3b0

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.

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