s365b.auth.local ================ .. py:module:: s365b.auth.local .. autoapi-nested-parse:: Local break-glass accounts. These exist for one purpose: administering the backup server when Microsoft 365 sign-in is not available (an Entra outage, a misconfigured Conditional Access policy, or a full tenant recovery). Rules: * Password (14+ characters) **and** a TOTP code or one-time recovery code, always. TOTP codes cannot be replayed. * 5 failed attempts lock the account for 15 minutes. * Optional allow-list of networks the sign-in is accepted from. * Every sign-in is audited and raises a notification event. The first local admin is created during setup with a one-time **setup token** printed in the API container log (or by ``s365b create-local-admin`` inside the container), so nobody who merely reaches the page first can claim a new installation. Attributes ---------- .. autoapisummary:: s365b.auth.local.MAX_FAILED_LOGINS s365b.auth.local.LOCKOUT s365b.auth.local.USERNAME_RE Exceptions ---------- .. autoapisummary:: s365b.auth.local.LoginError s365b.auth.local.LastBreakGlassError Classes ------- .. autoapisummary:: s365b.auth.local.LocalAuthSettings s365b.auth.local.Enrolment Functions --------- .. autoapisummary:: s365b.auth.local.issue_setup_token s365b.auth.local.check_setup_token s365b.auth.local.clear_setup_token s365b.auth.local.has_local_admin s365b.auth.local.create_local_admin s365b.auth.local.confirm_totp s365b.auth.local.confirm_enrolment s365b.auth.local.authenticate s365b.auth.local.keep_a_break_glass_admin s365b.auth.local.reset_credentials Module Contents --------------- .. py:data:: MAX_FAILED_LOGINS :value: 5 .. py:data:: LOCKOUT .. py:data:: USERNAME_RE :value: '^[a-z0-9][a-z0-9._-]{2,63}$' .. py:exception:: LoginError(message: str, *, locked_now: bool = False) Bases: :py:obj:`Exception` Sign-in refused. The message is safe to show (it never says which part was wrong). :ivar locked_now: True if this failure just locked the account. .. py:attribute:: locked_now :value: False .. py:class:: LocalAuthSettings Bases: :py:obj:`s365b.core.settings_store.SettingsGroup` Break-glass sign-in settings. :ivar allowed_networks: CIDR ranges local sign-in is accepted from; empty means any network. Useful to restrict break-glass access to the LAN or a management VLAN. .. py:attribute:: key :value: 'auth.local' .. py:attribute:: allowed_networks :type: list[str] .. py:method:: allows(ip: str | None) -> bool Return whether a client address may use local sign-in. :param ip: Client IP address. :returns: True if allowed. .. py:class:: Enrolment What a new break-glass admin must save, shown once. :ivar user_id: New user's ID. :ivar totp_secret: Base32 secret (for manual entry). :ivar totp_uri: ``otpauth://`` URI (for the QR code). :ivar recovery_codes: One-time codes for when the authenticator is lost. .. py:attribute:: user_id :type: int .. py:attribute:: totp_secret :type: str .. py:attribute:: totp_uri :type: str .. py:attribute:: recovery_codes :type: list[str] .. py:function:: issue_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession) -> str :async: Create a new one-time setup token (replacing any earlier one). :param s: Open session; the caller commits. :returns: The token, to print in the log. Only its hash is stored. .. py:function:: check_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession, token: str) -> bool :async: Return whether a setup token is the current one. :param s: Open session. :param token: Token entered by the user. :returns: True if valid. .. py:function:: clear_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession) -> None :async: Invalidate the setup token once the first admin exists. :param s: Open session; the caller commits. .. py:function:: has_local_admin(s: sqlalchemy.ext.asyncio.AsyncSession) -> bool :async: Return whether a usable local admin exists (TOTP enrolled, not disabled). :param s: Open session. :returns: True if one exists. .. py:function:: create_local_admin(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, username: str, password: str, *, issuer: str, display_name: str = '') -> Enrolment :async: Create a break-glass admin. TOTP must be confirmed before it can sign in. :param s: Open session; the caller commits. :param box: Secret box for the TOTP secret. :param username: Lowercase username. :param password: Password (checked against the policy). :param issuer: Name shown in the authenticator app. :param display_name: Optional display name. :returns: Enrolment details to show once. :raises passwords.PasswordPolicyError: If the password is too weak. :raises ValueError: If the username is invalid or taken. .. py:function:: confirm_totp(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, user: s365b.db.models.UserAccount, code: str) -> bool :async: Activate TOTP for a new account by checking a first code. :param s: Open session; the caller commits. :param box: Secret box. :param user: The local user. :param code: Code from the authenticator app. :returns: True if the code was right and TOTP is now active. .. py:function:: confirm_enrolment(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, settings: LocalAuthSettings, *, username: str, password: str, code: str, ip: str | None) -> bool :async: Activate a new or reset account: password and a first authenticator code. Held to the same rules as sign-in: the network allow-list, and the failed-attempt counter and lockout. :param s: Open session; the caller commits (also on failure). :param box: Secret box. :param settings: Local sign-in settings. :param username: Username. :param password: Password. :param code: Code from the authenticator app. :param ip: Client IP address. :returns: True if the account is now active. :raises LoginError: If not allowed from this network or the account is locked. .. py:function:: authenticate(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, settings: LocalAuthSettings, *, username: str, password: str, code: str, ip: str | None) -> tuple[s365b.db.models.UserAccount, bool] :async: Check break-glass credentials. :param s: Open session; the caller commits (also on failure, to keep the failed-attempt counter). :param box: Secret box. :param settings: Local sign-in settings. :param username: Username. :param password: Password. :param code: TOTP code, or a recovery code (``xxxxxx-xxxxxx``). :param ip: Client IP address. :returns: ``(user, used_recovery_code)``. :raises LoginError: For any failure, with a generic message. .. py:exception:: LastBreakGlassError Bases: :py:obj:`Exception` The change would leave no usable break-glass admin. .. py:function:: keep_a_break_glass_admin(s: sqlalchemy.ext.asyncio.AsyncSession, user_id: int) -> s365b.db.models.UserAccount | None :async: Lock break-glass changes, then check one usable admin stays besides a user. Call before disabling or resetting a local account; the lock is held until the session's transaction ends. :param s: Open session; the caller commits. :param user_id: The account about to stop being usable. :returns: The account, read after the lock; ``None`` if it does not exist. :raises LastBreakGlassError: If it is the last usable break-glass admin. .. py:function:: reset_credentials(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, user: s365b.db.models.UserAccount, *, password: str | None, issuer: str) -> Enrolment :async: Reset a break-glass account: new TOTP secret and recovery codes, optionally a new password. :param s: Open session; the caller commits. :param box: Secret box. :param user: The local user. :param password: New password, or ``None`` to keep the current one. :param issuer: Name shown in the authenticator app. :returns: New enrolment details; TOTP must be confirmed again.