Overview of OAuthenticator
mainOAuthenticator base class.repository·main·Indexed 19 days ago
https://github.com/jupyterhub/oauthenticatorA collection of JupyterHub authenticators that enable user login via OAuth2 identity providers. It includes specialized classes for providers such as GitHub, Google, Azure AD, GitLab, Auth0, Bitbucket, CILogon, Globus, MediaWiki, and OpenShift, as well as a GenericAuthenticator for custom OAuth 2.0 compliant services. Features include persisted authentication state for API access, custom 403 error handling, and mechanisms for refreshing OAuth access tokens.
OAuthenticator base class.OAuthenticator provides specialized authenticator classes for several common OAuth2 identity providers. You can use these specific classes to plug OAuth2 authentication into JupyterHub. Supported providers include:
oauthenticator.auth0.Auth0)oauthenticator.azuread.AzureAD)oauthenticator.bitbucket.Bitbucket)oauthenticator.cilogon.CILogon)jupyterhub_feishu_authenticator)oauthenticator.github.GitHub)oauthenticator.gitlab.GitLab)oauthenticator.globus.Globus)oauthenticator.google.Google)oauthenticator.mediawiki.MediaWiki)oauthenticator.openshift.OpenShift)If your provider is not listed, or if you need custom logic, you can use the GenericAuthenticator (oauthenticator.generic.GenericAuthenticator) which can be configured for any OAuth 2.0 identity provider or subclassed for further customization.
OAuthenticator distinguishes between authentication (verifying who a user is via an external provider) and authorization (deciding if that user is allowed to use your JupyterHub).
Authorization is managed through configuration options starting with allow or block.
allow configuration.allow rules are additive. If a user meets any allow rule, they are granted access. An allow rule cannot be used to exclude someone who was granted access by a different allow rule.block configurations always take precedence. If a user is matched by both an allow rule and a block rule, they are denied access.If you request expanded scopes, the additional user data is stored in the authenticator's auth_state structure.
Requirements:
auth_state in your configuration.cryptography package.Available Fields in auth_state:
id: Integer representing the GitHub account ID.login: The GitHub username.name: The user's full name.email: The user's publicly visible email address.access_token: The token used to authenticate to GitHub.teams: A list of teams the user belongs to. Note: This requires the read:org scope AND setting populate_teams_in_auth_state = True on your GitHubOAuthenticator instance.How to use this data:
To use these fields for provisioning (e.g., setting up home directories or git configs), you must subclass your current Spawner and modify it to read these fields from auth_state during the user provisioning lifecycle.
If you require advanced features or need to customize the login and logout actions beyond standard OAuth flows, you should create a custom subclass of an OAuthenticator.
When writing a custom class, you are responsible for defining the specific URLs and request logic required to interact with your identity provider. This allows for more granular control over how user information is fetched and how the authentication lifecycle is managed within JupyterHub.
GenericOAuthenticator or implement a custom authenticator by inheriting from the OAuthenticator base class.GenericAuthenticator. This class can be configured to work with any OAuth 2.0 compliant service or extended by creating a new subclass to implement custom authentication logic.OAuthenticator uses the JupyterHub refresh_user mechanism to periodically update user information (like group membership) and refresh OAuth access tokens.
Lifecycle:
Authenticator.auth_refresh_age (default: 5 minutes), JupyterHub triggers a refresh.refresh_token is available, a new access token is retrieved via the refresh token grant.Key Configuration:
Authenticator.auth_refresh_age: Controls the cache duration for auth info. Set to 0 to disable the time-based refresh trigger.Authenticator.refresh_pre_spawn: When set to True, ensures auth info is refreshed immediately before a server is launched. This is highly recommended if the server needs a valid access token to access data sources or git repos.# Ensure auth is up-to-date before launching a server
c.Authenticator.refresh_pre_spawn = True
# Disable the automatic time-based refresh
c.Authenticator.auth_refresh_age = 0OAuthenticator offers two ways to implement OAuth2 authentication in JupyterHub:
GenericOAuthenticator: A general-purpose class that works with any OAuth2 identity provider. Use this if you have a provider that does not have a dedicated class or if you prefer to manage all configuration manually.GitHubOAuthenticator, GoogleOAuthenticator). Using a specialized class reduces the amount of manual configuration required and enables provider-specific features, such as restricting access to specific GitHub organizations.To enable the server to run over HTTPS, place your SSL certificate and private key in the following locations within the project directory:
ssl/ssl.keyssl/ssl.certTo use Auth0 for JupyterHub authentication, you must first register a 'Regular Web App' in your Auth0 dashboard. Once registered, you need to configure the Auth0OAuthenticator in your jupyterhub_config.py using your Auth0 application credentials and domain.
c.JupyterHub.authenticator_class = "auth0"
c.OAuthenticator.oauth_callback_url = "https://[your-domain]/hub/oauth_callback"
c.OAuthenticator.client_id = "[your oauth2 application id]"
c.OAuthenticator.client_secret = "[your oauth2 application secret]"
c.Auth0OAuthenticator.auth0_domain = "[your-auth0-domain]"To use GitHub as an authentication provider, you must first register a GitHub OAuth application via GitHub's official documentation. Once registered, you can configure JupyterHub to use the GitHubOAuthenticator class.
Ensure your GitHub OAuth application's callback URL is set to match the oauth_callback_url configured in JupyterHub (typically https://[your-domain]/hub/oauth_callback).
c.JupyterHub.authenticator_class = "github"
c.OAuthenticator.oauth_callback_url = "https://[your-domain]/hub/oauth_callback"
c.OAuthenticator.client_id = "[your oauth2 application id]"
c.OAuthenticator.client_secret = "[your oauth2 application secret]"