s365b.auth.entra¶
Day-to-day sign-in with Entra ID (OpenID Connect, authorisation code + PKCE).
The sign-in app registration (created by the setup wizard, or entered manually) defines app roles; assigning users or groups to those roles in Entra decides who may sign in and with what role:
App role value |
Mail Safehouse role |
|---|---|
|
admin |
|
operator |
|
restore_operator |
|
auditor |
A user with none of these roles is refused. With several, the most privileged wins. Roles are re-read at every sign-in.
MSAL handles state, nonce and PKCE, and validates the ID token’s issuer, audience, expiry and nonce. The token comes straight from the token endpoint over TLS (back channel), so its signature does not need separate checking.
Attributes¶
Exceptions¶
Entra sign-in failed. The message is safe to show. |
Classes¶
Entra sign-in configuration (non-secret part). |
|
Runs the OIDC authorisation code flow through MSAL. |
Functions¶
|
Pick the user's role from the ID token |
|
Build an MSAL client credential from a certificate. |
Module Contents¶
- s365b.auth.entra.ROLE_CLAIMS: dict[str, s365b.db.models.UserRole][source]¶
- exception s365b.auth.entra.EntraLoginError[source]¶
Bases:
ExceptionEntra sign-in failed. The message is safe to show.
- class s365b.auth.entra.EntraSettings[source]¶
Bases:
s365b.core.settings_store.SettingsGroupEntra sign-in configuration (non-secret part).
- Variables:
enabled – Show “Sign in with Microsoft” and accept Entra sign-ins.
tenant_id – Entra tenant ID users sign in from.
client_id – Application ID of the sign-in app registration.
credential –
certificate(wizard-created app, key held by the server) orsecret(client secret entered manually).
- s365b.auth.entra.role_from_claims(claims: dict[str, Any]) s365b.db.models.UserRole | None[source]¶
Pick the user’s role from the ID token
rolesclaim.- Parameters:
claims – Validated ID token claims.
- Returns:
The most privileged mapped role, or
Noneif none is assigned.
- s365b.auth.entra.certificate_credential(private_key_pem: str, certificate_pem: str) dict[str, str][source]¶
Build an MSAL client credential from a certificate.
- Parameters:
private_key_pem – PEM private key.
certificate_pem – PEM certificate.
- Returns:
The credential dict MSAL expects.
- class s365b.auth.entra.EntraLogin(tenant_id: str, client_id: str, credential: str | dict[str, str], *, app_factory: AppFactory = msal.ConfidentialClientApplication)[source]¶
Runs the OIDC authorisation code flow through MSAL.
- Parameters:
tenant_id – Entra tenant ID.
client_id – Sign-in app’s application ID.
credential – Client secret string or certificate credential dict.
app_factory – MSAL app class (replaced in tests).
- async start(redirect_uri: str) dict[str, Any][source]¶
Begin a sign-in.
- Parameters:
redirect_uri – The callback URL registered on the app.
- Returns:
auth_urito redirect the browser to, plus state/PKCE data to keep (encrypted) until the callback.- Return type:
The MSAL flow
- Raises:
EntraLoginError – If Entra cannot be reached.
- async finish(flow: dict[str, Any], params: dict[str, str]) dict[str, Any][source]¶
Complete a sign-in from the callback’s query parameters.
- Parameters:
flow – The flow returned by
start().params – Query parameters Entra sent to the callback.
- Returns:
Validated ID token claims.
- Raises:
EntraLoginError – If Entra reports an error, the state does not match, or the user is from another tenant.