Skip to main content
https://badge.fury.io/py/jhub-authenticators.svg

An extended collection of HTTP(S) header JupyterHub Authenticators that relies on proxy authenticating. Every mentioned authenticator should be used while following the mentioned security recommendations at cwaldbieser.

Installation

Installation from pypi:

pip install jhub-authenticators

Installation from local git repository:

cd jhub-authenticators
pip install .

Dummy Authentication

Provides an option for testing JupyterHub authentication with a dummy authenticator that can have a global preset password for any account:

c.JupyterHub.authenticator_class = 'jhubauthenticators.DummyAuthenticator'
c.DummyAuthenticator.password = 'password'

Note! Don’t use in production.

Header Authentication

This Header Authentication method provides multiple functionalities beyond mere authentication. It can activated by adding the following to the JupyterHub configuration:

c.JupyterHub.authenticator_class = 'jhubauthenticators.HeaderAuthenticator'

By default, it exposes the following paths:

'/login' -> is utilizied to authenticate the user, relies on the 'allowed_headers' parameter to accomplish this.
'/logout' -> clears the users authenticated session.
'/set-user-data' -> allows an authenticated user to provide data to be persisted during the authenticated session. Here the 'user_external_allow_attributes' parameter defines which user attributes are allowed to be set.

Specify Authentication Header

First it provides the possibility to define a custom authentication header, this is accomplished by overriding the default allowed_headers dict required auth key:

c.HeaderAuthenticator.allowed_headers = {'auth': 'MyAuthHeader'}

This will overrive the default Remote-User header authentication to use the MyAuthHeader instead.

Additional User Data Headers

Beyond the auth key, the administrator is allowed to set additional headers that the authenticator will accept requests on.

For instance, if the MyCustomHeader should be accepted as well during authentication:

c.HeaderAuthenticator.enable_auth_state = True
c.HeaderAuthenticator.allowed_headers = {'auth': 'MyAuthHeader',
                                         'auth_data': 'MyCustomHeader'}

Any information provided via the MyCustomHeader during authentication will be added to the JupyterHub user’s auth_state, dictionary as defined by Authenticators auth_state. The data will be added to the auth_state by utilizing the header value in the allowed_headers dictionary as the key in the ‘auth_state’ dictionary. For instance the above configuration, will produce the following user profile:

user = {
    name: 'stored MyAuthHeader value',
    'auth_state': {'MyCustomHeader': 'stored MyCustomHeader value'}
}

It’s important to note here, that this information is only persisted for the life-time of the authenticated session.

Sharing auth_state data with Spawner Environement

If any of the defined auth_state key-value pairs should be set as Spawner environement variables before a notebook is spawned, the spawner_shared_headers parameter is available to define this, E.g if the “MyCustomHeader’ should do this, it can be accomplished with the following addition to the configuration:

c.HeaderAuthenticator.spawner_shared_headers = ['MyCustomHeader']

Which during pre_spawn_hook will produce the following environment variable:

~>env | grep MyCustomHeader

MyCustomHeader="stored MyCustomHeader value"

Special Parsers

If the administrator requires that the defined allowed_headers should be parsed in a special way. The administrator can use the header_parser_classes parameter to define how a request with a particular header should be parsed, E.g:

from jhubauthenticators import Parser, JSONParser

c.HeaderAuthenticator.header_parser_classes = {'auth': Parser,
                                               'auth_data': JSONParser}

The auth header is here set to be parsed by the default Parser, which just returns the provided value unchanged. The JSONParser, however does what it indicated, attempts to parse the data as JSON.

In addition to these, the authenticator also provides the RegexUsernameParser which can be used as an auth parser, E.g:

# RegexUsernameParser
c.HeaderAuthenticator.header_parser_classes = {'auth': RegexUsernameParser}
# Email regex
RegexUsernameParser.username_extract_regex = '([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)'

Which will try to expand an email from the defined auth allowed_headers Header. If this can’t be accomplished, the user will not be authenticated.

Related to the username_extract_regex, the RegexUsernameParser.replace_extract_chars parameter exists to accomplish post filtering of illegal characters on the extracted username, E.g:

# Replace every '@' and '.' char in the extracted username with '_'
RegexUsernameParser.replace_extract_chars = {'@': '_', '.': '_'}

It is possible to define additional parsers by extending the Parser class and implementing the required parse method, E.g:

class MyParser(Parser)

    # MyAdvancedParser
    def parse(self, data)
        return data

Which can subsequently be activate by adding it to the header_parser_classes parameter, E.g.:

# MyAdvancedParser
c.HeaderAuthenticator.header_parser_classes = {'auth': MyParser}

Set User state after Authentication

Finally, the HeaderAuthenticator also provides the administrator the possibility to define the user_external_allow_attributes parameter. This allows defines which user attributes an authenticated user is allowed to set a user-defined variable via the HeaderAuthenticator defined /set-user-data endpoint. For instance, an authentiacated user’s variable data could be allowed to be externally defined by defining the following configuration:

c.HeaderAuthenticator.user_external_allow_attributes = ['data']

By default the user_external_allow_attributes allows no such attributes and has to be explicitly enabled/defined. Furthermore, this will only allow an authenticated user to externally define their own data instance variable.

Additional configuration examples of this can be found in the tests/jupyterhub_configs directory.

Release files for jhub-authenticators 0.6.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 jhub-authenticators 0.6.1
File Size Uploaded
jhub_authenticators-0.6.1.tar.gz 25.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jhub-authenticators 0.6.1
File Interpreter ABI Platform
jhub_authenticators-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 47.6 kB

Release files / jhub_authenticators-0.6.1.tar.gz

Download URL jhub_authenticators-0.6.1.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ae4ae75954bdfccb15720cd05775d9f47d682ba414c263b1321da75d8fa65931
BLAKE2b-256 checksum
How to use checksums
65fee59006eae5afb7d810393e0de78eae9a06b35018569912e1d1ab505746c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release files / jhub_authenticators-0.6.1-py3-none-any.whl

Download URL jhub_authenticators-0.6.1-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a827bebd55eb1c91fc9f90e6dbf0228e6b017b2baa68f74e9d8ca992e3d902ff
BLAKE2b-256 checksum
How to use checksums
f17e095a48459b504e9c348f1f80616792564ffb179e289a09815bac1423c926
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

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