s365b.auth.entra ================ .. py:module:: s365b.auth.entra .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: s365b.auth.entra.ROLE_CLAIMS s365b.auth.entra.ROLE_PRIORITY s365b.auth.entra.AppFactory Exceptions ---------- .. autoapisummary:: s365b.auth.entra.EntraLoginError Classes ------- .. autoapisummary:: s365b.auth.entra.EntraSettings s365b.auth.entra.EntraLogin Functions --------- .. autoapisummary:: s365b.auth.entra.role_from_claims s365b.auth.entra.certificate_credential Module Contents --------------- .. py:data:: ROLE_CLAIMS :type: dict[str, s365b.db.models.UserRole] .. py:data:: ROLE_PRIORITY .. py:exception:: EntraLoginError Bases: :py:obj:`Exception` Entra sign-in failed. The message is safe to show. .. py:class:: EntraSettings Bases: :py:obj:`s365b.core.settings_store.SettingsGroup` Entra sign-in configuration (non-secret part). :ivar enabled: Show "Sign in with Microsoft" and accept Entra sign-ins. :ivar tenant_id: Entra tenant ID users sign in from. :ivar client_id: Application ID of the sign-in app registration. :ivar credential: ``certificate`` (wizard-created app, key held by the server) or ``secret`` (client secret entered manually). .. py:attribute:: key :value: 'auth.entra' .. py:attribute:: enabled :type: bool :value: False .. py:attribute:: tenant_id :type: str :value: '' .. py:attribute:: client_id :type: str :value: '' .. py:attribute:: credential :type: Literal['certificate', 'secret'] :value: 'certificate' .. py:function:: role_from_claims(claims: dict[str, Any]) -> s365b.db.models.UserRole | None Pick the user's role from the ID token ``roles`` claim. :param claims: Validated ID token claims. :returns: The most privileged mapped role, or ``None`` if none is assigned. .. py:function:: certificate_credential(private_key_pem: str, certificate_pem: str) -> dict[str, str] Build an MSAL client credential from a certificate. :param private_key_pem: PEM private key. :param certificate_pem: PEM certificate. :returns: The credential dict MSAL expects. .. py:data:: AppFactory .. py:class:: EntraLogin(tenant_id: str, client_id: str, credential: str | dict[str, str], *, app_factory: AppFactory = msal.ConfidentialClientApplication) Runs the OIDC authorisation code flow through MSAL. :param tenant_id: Entra tenant ID. :param client_id: Sign-in app's application ID. :param credential: Client secret string or certificate credential dict. :param app_factory: MSAL app class (replaced in tests). .. py:attribute:: tenant_id .. py:method:: start(redirect_uri: str) -> dict[str, Any] :async: Begin a sign-in. :param 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. :rtype: The MSAL flow :raises EntraLoginError: If Entra cannot be reached. .. py:method:: finish(flow: dict[str, Any], params: dict[str, str]) -> dict[str, Any] :async: Complete a sign-in from the callback's query parameters. :param flow: The flow returned by :meth:`start`. :param 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.