security.auth¶
Validation of bearer tokens (JSON Web Tokens, JWTs) issued by OpenID Connect identity providers.
security:
auth:
issuers:
- https://login.example.org/oidc/
audience:
- my-app
verify_audience: true
token_types:
- at+jwt
- application/at+jwt
verify_token_type: true
clients: null
claim_identity: sub
algorithms:
- RS256
allow_expired: false
validate_userinfo: false
userinfo_claims: []
service_clients:
- client_id: conquest-client-id
issuer: null
subject: null
on_behalf_of: false
on_behalf_of_header: null
timeout: 10
cache_ttl: 300
Only tokens issued by one of the trusted issuers are accepted; all others
are rejected without contacting their issuers. With trusted issuers,
audience is required, so that tokens issued for other apps by the same
identity provider are rejected; to accept tokens for any audience instead
(not recommended, unless clients are restricted), set
verify_audience: false. Only OAuth 2.0 access tokens
as per RFC 9068 (typ header
at+jwt or application/at+jwt) are accepted, so that other JWTs of the same
identity provider, such as ID tokens, cannot be used as access tokens; to
accept tokens of other types, list them in token_types, or set
verify_token_type: false if the identity provider does not set the typ
header of its access tokens. To accept only tokens obtained by specific
clients (client_id claim, or azp claim if there is none), e.g., by a
gateway in front of the app, list their IDs in clients, in addition to
audience or instead of it (with verify_audience: false). Clients calling
the app with client-credentials tokens of their own are listed as
service_clients; no user info is requested for their tokens. Each
service client is tied to a trusted issuer, which is required if several
issuers are trusted (cf. Service
tokens). See
Authentication for how tokens are validated and how to
require them for operations.
Issuers are listed as URLs, or with settings of their own, e.g., to choose
the namespace of their subjects (cf. several trusted
issuers). Identity providers
differ, so issuers may also have an audience, a group claim
(claim_groups, cf. security.access_control) and
userinfo_claims of their own, which replace the global settings for their
tokens; unset ones fall back to the global settings. An empty
userinfo_claims adds no claims from the issuer's user info, and an empty
claim_groups takes no groups from its tokens:
security:
auth:
audience:
- my-app
issuers:
- issuer: https://login.example.org/oidc/
namespace: ""
- issuer: https://login.example.com/oidc/
namespace: example-com
audience:
- my-app-client-id
claim_groups: groups
userinfo_claims: []
With an audience for each issuer, the global audience is not required.
AuthConfig ¶
Bases: FOCABaseConfig
Model for configuring JSON Web Token (JWT)-based authorization.
Bearer tokens are only accepted if issued by one of the trusted
issuers. They are verified locally with the issuer's public keys,
obtained via OpenID Connect discovery, and, if validate_userinfo is
set, additionally via the issuer's user info endpoint.
Which operations require a bearer token is defined by the security
requirements of the operations in the API specifications (e.g., via
x-bearerInfoFunc: foca.security.auth.validate_token).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issuers
|
Trusted token issuers, i.e., values of the |
required | |
add_key_to_claims
|
Whether to add the identity provider's public key that the token was verified with, in PEM format, to the claims. |
required | |
allow_expired
|
Allow/disallow expired JWTs. If |
required | |
audience
|
List of audiences that the app identifies itself with.
JWTs whose |
required | |
verify_audience
|
Whether to validate the audience of JWTs. If
|
required | |
token_types
|
Accepted values of the |
required | |
verify_token_type
|
Whether to validate the type of JWTs. If
|
required | |
clients
|
Accepted clients, i.e., IDs of the clients that obtained
the tokens, as per the |
required | |
claim_identity
|
The JWT claim used to identify the sender. Tokens without this claim are rejected. |
required | |
algorithms
|
Lists the JWT-signing algorithms supported by the app. |
required | |
validate_userinfo
|
Whether to additionally validate tokens via the issuer's user info endpoint, which needs to report the token's subject. |
required | |
userinfo_claims
|
Claims to add from the issuer's user info endpoint
if the token lacks them, e.g., |
required | |
timeout
|
Timeout (in seconds) for requests to identity providers. |
required | |
cache_ttl
|
Time (in seconds) for which identity providers' discovery documents and public keys, and user info, are cached. |
required | |
service_clients
|
Service clients, whose client-credentials tokens
(service tokens) represent the client, not a user (cf.
:py:class: |
required | |
on_behalf_of_header
|
Request header in which service clients allowed
to (cf. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
issuers |
Sequence[str | IssuerConfig]
|
Trusted token issuers. |
add_key_to_claims |
bool
|
Whether to add the identity provider's public key that the token was verified with, in PEM format, to the claims. |
allow_expired |
bool
|
Allow/disallow expired JWTs. |
audience |
list[str] | None
|
List of audiences that the app identifies itself with. |
verify_audience |
bool
|
Whether to validate the audience of JWTs. |
token_types |
list[str]
|
Accepted values of the |
verify_token_type |
bool
|
Whether to validate the type of JWTs. |
clients |
list[str] | None
|
Accepted clients, i.e., IDs of the clients that obtained the tokens. |
claim_identity |
str
|
The JWT claim used to identify the sender. |
algorithms |
list[str]
|
Lists the JWT-signing algorithms supported by the app. |
validate_userinfo |
bool
|
Whether to additionally validate tokens via the issuer's user info endpoint. |
userinfo_claims |
list[str]
|
Claims to add from the issuer's user info endpoint if the token lacks them. |
timeout |
float
|
Timeout (in seconds) for requests to identity providers. |
cache_ttl |
float
|
Time (in seconds) for which identity providers' discovery documents and public keys, and user info, are cached. |
service_clients |
list[ServiceClientConfig]
|
Service clients. |
on_behalf_of_header |
str | None
|
Request header in which service clients name the user they act for. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
The class was instantianted with an illegal
data type, |
Examples:
>>> auth = AuthConfig(
... issuers=["https://login.example.org/oidc/"],
... audience=["my-app"],
... )
>>> auth.audience
['my-app']
>>> auth.algorithms
['RS256']
>>> auth.token_types
['at+jwt', 'application/at+jwt']
IssuerConfig ¶
Bases: FOCABaseConfig
Model for configuring a trusted token issuer.
Plain URLs in AuthConfig.issuers stand for issuers with the default
settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issuer
|
Issuer, i.e., value of the |
required | |
namespace
|
Namespace of the subjects of the issuer's tokens, as
|
required | |
audience
|
Accepted audiences of the issuer's tokens (cf.
|
required | |
claim_groups
|
Claim of the issuer's tokens listing groups or
entitlements of the subject (cf.
|
required | |
userinfo_claims
|
Claims to add from the issuer's user info if its
tokens lack them (cf. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
issuer |
str
|
Issuer. |
namespace |
str | None
|
Namespace of the subjects of the issuer's tokens. |
audience |
list[str] | None
|
Accepted audiences of the issuer's tokens. |
claim_groups |
str | None
|
Claim of the issuer's tokens listing groups. |
userinfo_claims |
list[str] | None
|
Claims to add from the issuer's user info. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
The class was instantiated with an illegal
data type, |
Examples:
>>> conf = IssuerConfig(issuer="https://login.example.org/", namespace="ls")
>>> conf.namespace, conf.audience
('ls', None)
ServiceClientConfig ¶
Bases: FOCABaseConfig
Model for a service client, i.e., a client using its own tokens.
Services call each other with OAuth 2.0 client-credentials tokens (e.g.,
a workflow engine calling a task service), which represent the client,
not a user, and have no user info. A token is a service token of a
service client if it was issued by the client's issuer (its iss
claim), to the client (its client_id claim, or azp without
client_id, is client_id), and its subject (the claim configured as
AuthConfig.claim_identity) is subject. Other tokens issued to the
client (e.g., for users, with their subjects) are user tokens. Check
which claims your identity provider puts into client-credentials
tokens, and set subject accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
Client ID of the service client. |
required | |
issuer
|
Issuer of the client's service tokens ( |
required | |
subject
|
Subject of the client's service tokens. If |
required | |
on_behalf_of
|
Whether the client may name the user it acts for (cf.
|
required |
Attributes:
| Name | Type | Description |
|---|---|---|
client_id |
str
|
Client ID of the service client. |
issuer |
str | None
|
Issuer of the client's service tokens. |
subject |
str | None
|
Subject of the client's service tokens. |
on_behalf_of |
bool
|
Whether the client may name the user it acts for. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
The class was instantianted with an illegal
data type, or |
Examples:
>>> client = ServiceClientConfig(client_id="conquest")
>>> client.issuer is None and client.subject is None
True
>>> client.on_behalf_of
False