jupyterhub.services.auth
Authenticating services with JupyterHub.
Cookies are sent to the Hub for verification. The Hub replies with a JSON model describing the authenticated user.
HubAuth can be used in any application, even outside tornado.
HubAuth
HubAuthenticated is a mixin class for tornado handlers that should authenticate with the Hub.
HubAuthenticated
jupyterhub.services.auth.
A class for authenticating with JupyterHub
This can be used by any application.
If using tornado, use via HubAuthenticated mixin. If using manually, use the .user_for_cookie(cookie_value) method to identify the user corresponding to a given cookie value.
.user_for_cookie(cookie_value)
The following config must be set:
api_token (token for authenticating with JupyterHub API), fetched from the JUPYTERHUB_API_TOKEN env by default.
The following config MAY be set:
api_url: the base URL of the Hub’s internal API, fetched from JUPYTERHUB_API_URL by default.
cookie_cache_max_age: the number of seconds responses from the Hub should be cached.
login_url (the public /hub/login URL of the Hub).
/hub/login
cookie_name: the name of the cookie I should be using, if different from the default (unlikely).
api_token
API key for accessing Hub API.
Generate with jupyterhub token [username] or add to JupyterHub.services config.
jupyterhub token [username]
api_url
The base API URL of the Hub.
Typically http://hub-ip:hub-port/hub/api
http://hub-ip:hub-port/hub/api
base_url
The base URL prefix of this application
e.g. /services/service-name/ or /user/name/
Default: get from JUPYTERHUB_SERVICE_PREFIX
cache_max_age
The maximum time (in seconds) to cache the Hub’s responses for authentication.
A larger value reduces load on the Hub and occasional response lag. A smaller value reduces propagation time of changes on the Hub (rare).
Default: 300 (five minutes)
certfile
The ssl cert to use for requests
Use with keyfile
client_ca
The ssl certificate authority to use to verify requests
Use with keyfile and certfile
cookie_name
The name of the cookie I should be looking for
cookie_options
Additional options to pass when setting cookies.
Can include things like expires_days=None for session-expiry or secure=True if served on HTTPS and default HTTPS discovery fails (e.g. behind some proxies).
expires_days=None
secure=True
hub_host
The public host of JupyterHub
Only used if JupyterHub is spreading servers across subdomains.
hub_prefix
The URL prefix for the Hub itself.
Typically /hub/
keyfile
The ssl key to use for requests
Use with certfile
login_url
The login URL to use
Typically /hub/login
get_session_id
Get the jupyterhub session id
from the jupyterhub-session-id cookie.
get_token
Get the user token from a request
in URL parameters: ?token=<token>
in header: Authorization: token <token>
get_user
Get the Hub user for a given tornado handler.
Checks cookie with the Hub to identify the current user.
handler (tornado.web.RequestHandler) – the current request handler
The user model, if a user is identified, None if authentication fails.
The ‘name’ field contains the user’s name.
user_model (dict)
user_for_cookie
Ask the Hub to identify the user for a given cookie.
encrypted_cookie (str) – the cookie value (not decrypted, the Hub will do that)
use_cache (bool) – Specify use_cache=False to skip cached cookie values (default: True)
user_for_token
Ask the Hub to identify the user for a given token.
token (str) – the token
HubOAuth
HubAuth using OAuth for login instead of cookies set by the Hub.
oauth_authorization_url
The URL to redirect to when starting the OAuth process
oauth_client_id
The OAuth client ID for this application.
Use JUPYTERHUB_CLIENT_ID by default.
oauth_redirect_uri
OAuth redirect URI
Should generally be /base_url/oauth_callback
oauth_token_url
The URL for requesting an OAuth token from JupyterHub
clear_cookie
Clear the OAuth cookie
Use OAuth client_id for cookie name
because we don’t want to use the same cookie name across OAuth clients.
generate_state
Generate a state string, given a next_url redirect target
(str) (next_url) –
state (str)
The base64-encoded state string.
get_next_url
Get the next_url for redirection, given an encoded OAuth state
get_state_cookie_name
Get the cookie name for oauth state, given an encoded OAuth state
Cookie name is stored in the state itself because the cookie name is randomized to deal with races between concurrent oauth sequences.
set_cookie
Set a cookie recording OAuth result
set_state_cookie
Generate an OAuth state and store it in a cookie
(RequestHandler) (handler) –
The OAuth state that has been stored in the cookie (url safe, base64-encoded)
state_cookie_name
The cookie name for storing OAuth state
This cookie is only live for the duration of the OAuth handshake.
token_for_code
Get token for OAuth temporary code
This is the last step of OAuth login. Should be called in OAuth Callback handler.
code (str) – oauth code for finishing OAuth login
JupyterHub API Token
token (str)
Mixin for tornado handlers that are authenticated with JupyterHub
A handler that mixes this in must have the following attributes/properties:
.hub_auth: A HubAuth instance
.hub_users: A set of usernames to allow. If left unspecified or None, username will not be checked.
.hub_groups: A set of group names to allow. If left unspecified or None, groups will not be checked.
Examples:
class MyHandler(HubAuthenticated, web.RequestHandler): hub_users = {'inara', 'mal'} def initialize(self, hub_auth): self.hub_auth = hub_auth @web.authenticated def get(self): ...
allow_all
Property indicating that all successfully identified user or service should be allowed.
check_hub_user
Check whether Hub-authenticated user or service should be allowed.
Returns the input if the user should be allowed, None otherwise.
Override if you want to check anything other than the username’s presence in hub_users list.
model (dict) – the user or service model returned from HubAuth
The user model if the user should be allowed, None otherwise.
get_current_user
Tornado’s authentication method
get_login_url
Return the Hub’s login URL
hub_auth_class
alias of HubAuth
HubOAuthenticated
Simple subclass of HubAuthenticated using OAuth instead of old shared cookies
HubOAuthCallbackHandler
OAuth Callback handler
Finishes the OAuth flow, setting a cookie to record the user’s info.
Should be registered at SERVICE_PREFIX/oauth_callback
SERVICE_PREFIX/oauth_callback