Skip to main content

Datasette plugin that authenticates users based on existing domain cookies

Project description

datasette-auth-existing-cookies

PyPI Changelog Tests License

Datasette plugin that authenticates users based on existing domain cookies.

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 running on data.example.com will then make the actor available to the rest of Datasette based on the response from that API.

Read about Datasette's authentication and permissions system for more on how actors and permissions work.

Configuration

This plugin requires some configuration in the Datasette metadata.json file.

The following configuration options are supported:

  • api_url: this is the API endpoint that Datasette should call with the user's cookies in order to identify the logged in user.
  • cookies: optional. A list of cookie names that should be passed through to the API endpoint - if left blank, the default is to send all cookies.
  • ttl: optional. By default Datasette will make a request to the API endpoint for every HTTP request recieved by Datasette itself. A ttl value of 5 will cause Datasette to cache the actor associated with the user's cookies for 5 seconds, reducing that API traffic.
  • headers: an optional list of other headers to forward to the API endpoint as query string parameters.

Here is an example that uses all four of these settings:

{
    "plugins": {
        "datasette-auth-existing-cookies": {
            "api_url": "http://www.example.com/user-from-cookies",
            "cookies": ["sessionid"],
            "headers": ["host"],
            "ttl": 10
        }
    }
}

With this configuration any hit to a Datasette hosted at data.example.com will result in the following request being made to the http://www.example.com/user-from-cookies API endpoint:

GET http://www.example.com/user-from-cookies?host=data.example.com
Cookie: sessionid=abc123

That API is expected to return a JSON object representing the current user:

{
    "id": 1,
    "name": "Barry"
}

Since ttl is set to 10 that actor will be cached for ten seconds against that exact combination of cookies and headers. When that cache expires another hit will be made to the API.

When deciding on a TTL value, take into account that users who lose access to the core site - maybe because their session expires, or their account is disabled - will still be able to access the Datasette instance until that cache expires.

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

datasette-auth-existing-cookies-1.0a0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file datasette-auth-existing-cookies-1.0a0.tar.gz.

File metadata

File hashes

Hashes for datasette-auth-existing-cookies-1.0a0.tar.gz
Algorithm Hash digest
SHA256 d8ca936a5d6c35338fda213ebb69f0b34bb1e8b03312a33893b6bdc0e81e74af
MD5 c0d9eeb4bf806afb114798c16b625eee
BLAKE2b-256 65abe314a8c2ebbf8e62bb61364cce685380f9416d3e0cc64038c5a93747cfb6

See more details on using hashes here.

File details

Details for the file datasette_auth_existing_cookies-1.0a0-py3-none-any.whl.

File metadata

File hashes

Hashes for datasette_auth_existing_cookies-1.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf70e19ef812f2b479972676c9d5dc589984e47804f5d76cbc1ab037f3c84a8a
MD5 78d8838903cae1d6759fc202feb626b6
BLAKE2b-256 3397badef951d93458bf3778c0b1a0b0b4dcc726346eeb2fcf2d86c88a9bd27c

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