OIDCService#
- class gafaelfawr.services.oidc.OIDCService(*, config, token_lifetime, authorization_store, client_store, token_service, user_info_service, slack_client=None, session, logger)#
Bases:
objectMinimalist OpenID Connect identity provider.
This provides just enough of the OpenID Connect protocol to satisfy Chronograf (and possibly some other applications). It is the underlying implementation of the
/auth/openidroutes.- Parameters:
config (
OIDCServerConfig) – OpenID Connect server configuration.token_lifetime (
timedelta) – Token lifetime for ID tokens if the underlying Gafaelfawr token does not have an expiration.authorization_store (
OIDCAuthorizationStore) – The underlying storage for OpenID Connect authorizations.token_service (
TokenService) – Token manipulation service.user_info_service (
UserInfoService) – User information service.slack_client (
Optional[SlackWebhookClient], default:None) – If provided, a Slack webhook client to use to report corruption of the underlying Redis store.session (
AsyncSession) – Database session.logger (
BoundLogger) – Logger for diagnostics.
Notes
Expects the following flow:
User is sent to
/auth/openid/loginfor initial authentication.User is redirected back to the application with an authorization code.
Application submits code to
/auth/openid/token.Application receives an access token and an ID token (the same).
Application gets user information from
/auth/openid/userinfo.
The handler code is responsible for parsing the requests from the user. This object creates the authorization code (with its associated Redis entry) for step 2, and then returns the token for that code in step 4.
- Parameters:
client_store (
OIDCClientStore)
Methods Summary
Invalidate all issued OpenID Connect codes.
delete_client(auth_data, client_id)Delete a registered OpenID Connect client.
get_client(auth_data, client_id)Get the metadata for a registered OpenID Connect client.
get_jwks()Return the key set for the OpenID Connect server.
Return the OpenID Connect configuration for the internal server.
issue_code(*, client_id, redirect_uri, ...)Issue a new authorization code.
issue_id_token(authorization)Issue an OpenID Connect token.
list_clients(auth_data)List all registered OpenID Connect clients.
migrate_clients(clients)Migrate clients from the old configuration into the database.
redeem_code(*, grant_type, client_id, ...)Redeem an authorization code.
register_client(auth_data, request)Register a new OpenID Connect client.
token_to_userinfo_claims(token_data)Generate OpenID Connect userinfo claims from a Gafaelfawr token.
update_client(auth_data, client_id, update)Get the metadata for a registered OpenID Connect client.
validate_client(client_id, redirect_uri)Check that the provided client and redirect URI are valid.
verify_token(token)Verify a token issued by the internal OpenID Connect server.
Methods Documentation
- async delete_client(auth_data, client_id)#
Delete a registered OpenID Connect client.
- Parameters:
- Raises:
NotFoundError – Raised if the client could not be found.
- Return type:
- async get_client(auth_data, client_id)#
Get the metadata for a registered OpenID Connect client.
- Parameters:
- Returns:
The registered OpenID Connect client.
- Return type:
- Raises:
NotFoundError – Raised if the client could not be found.
- get_openid_configuration()#
Return the OpenID Connect configuration for the internal server.
- Return type:
- async issue_code(*, client_id, redirect_uri, token, scopes, nonce=None)#
Issue a new authorization code.
- Parameters:
- Returns:
The code for a newly-created and stored authorization.
- Return type:
- Raises:
InvalidClientIdError – Raised if the provided client ID is not registered as an OpenID Connect client.
ReturnUriMismatchError – Raised if the provided redirect URI does not match the one registered for this client.
- async issue_id_token(authorization)#
Issue an OpenID Connect token.
This creates a new OpenID Connect token with data taken from the internal Gafaelfawr token.
- Parameters:
authorization (
OIDCAuthorization) – Authorization code used to request a token.- Returns:
The new token.
- Return type:
- Raises:
InvalidGrantError – Raised if the underlying authorization or session does not exist.
- async list_clients(auth_data)#
List all registered OpenID Connect clients.
- Parameters:
auth_data (
TokenData) – Token information for the person requesting the client list.- Returns:
List of registered OpenID Connect clients.
- Return type:
list of OIDCClient
- async migrate_clients(clients)#
Migrate clients from the old configuration into the database.
- Parameters:
clients (
list[OIDCClientConfig]) – Clients to migrate.- Return type:
- async redeem_code(*, grant_type, client_id, client_secret, redirect_uri, code, ip_address)#
Redeem an authorization code.
None of the parameters may be
Nonein practice, butNoneis accepted and rejected wih an exception so that error handling can be unified.- Parameters:
client_id (
Optional[str]) – Client ID of the OpenID Connect client.client_secret (
Optional[str]) – Secret for that client. A secret ofNonewill never be valid, but is accepted so that error handling can be unified.redirect_uri (
Optional[str]) – The return URI of the OpenID Connect client.code (
Optional[str]) – The OpenID Connect authorization code.ip_address (
str)
- Returns:
The token reply to send to the user.
- Return type:
- Raises:
InvalidClientError – Raised if the client ID is not known or the client secret does not match the client ID.
InvalidGrantError – Raised if the code is not valid, the client is not allowed to use it, or the underlying authorization or session does not exist.
InvalidRequestError – Raised if the token redemption request is syntactically invalid.
UnsupportedGrantTypeError – Raised if the requested grant type isn’t supported.
- async register_client(auth_data, request)#
Register a new OpenID Connect client.
- Parameters:
auth_data (
TokenData) – Token information for the person adding this client.request (
OIDCClientUpdate) – OpenID Connect client information.
- Return type:
- async token_to_userinfo_claims(token_data)#
Generate OpenID Connect userinfo claims from a Gafaelfawr token.
- async update_client(auth_data, client_id, update)#
Get the metadata for a registered OpenID Connect client.
- Parameters:
auth_data (
TokenData) – Token information for the person requesting this client.client_id (
str) – Identifier of the client.update (
OIDCClientUpdate) – Updated information to replace in the client metadata.
- Returns:
The updated OpenID Connect client.
- Return type:
- Raises:
NotFoundError – Raised if the client could not be found.
- async validate_client(client_id, redirect_uri)#
Check that the provided client and redirect URI are valid.
Raises exceptions on any errors.
- Parameters:
- Raises:
InvalidClientIdError – Raised if the provided client ID is unknown.
ReturnUriMismatchError – Raised if the provided return URI doesn’t match the one registered with the client.
- Return type:
- async verify_token(token)#
Verify a token issued by the internal OpenID Connect server.
Any currently-registered client audience is accepted as a valid audience.
- Parameters:
token (
OIDCToken) – An encoded token.- Returns:
The verified token.
- Return type:
- Raises:
InvalidTokenError – The issuer of this token is unknown and therefore the token cannot be verified.