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

S365B.Admin

admin

S365B.Operator

operator

S365B.RestoreOperator

restore_operator

S365B.Auditor

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

EntraLoginError

Entra sign-in failed. The message is safe to show.

Classes

EntraSettings

Entra sign-in configuration (non-secret part).

EntraLogin

Runs the OIDC authorisation code flow through MSAL.

Functions

role_from_claims(claims) → s365b.db.models.UserRole | None

Pick the user's role from the ID token roles claim.

certificate_credential(private_key_pem, certificate_pem) → dict[str, str]

Build an MSAL client credential from a certificate.

Module Contents

s365b.auth.entra.ROLE_CLAIMS: dict[str, s365b.db.models.UserRole][source]
s365b.auth.entra.ROLE_PRIORITY[source]
exception s365b.auth.entra.EntraLoginError[source]

Bases: Exception

Entra sign-in failed. The message is safe to show.

class s365b.auth.entra.EntraSettings[source]

Bases: s365b.core.settings_store.SettingsGroup

Entra 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) or secret (client secret entered manually).

key = 'auth.entra'[source]
enabled: bool = False[source]
tenant_id: str = ''[source]
client_id: str = ''[source]
credential: Literal['certificate', 'secret'] = 'certificate'[source]
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 roles claim.

Parameters:

claims – Validated ID token claims.

Returns:

The most privileged mapped role, or None if 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.

s365b.auth.entra.AppFactory[source]
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).

tenant_id[source]
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_uri to 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.