s365b.auth.local

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

Exceptions

LoginError

Sign-in refused. The message is safe to show (it never says which part was wrong).

LastBreakGlassError

The change would leave no usable break-glass admin.

Classes

LocalAuthSettings

Break-glass sign-in settings.

Enrolment

What a new break-glass admin must save, shown once.

Functions

issue_setup_token(s) → str

Create a new one-time setup token (replacing any earlier one).

check_setup_token(s, token) → bool

Return whether a setup token is the current one.

clear_setup_token(s) → None

Invalidate the setup token once the first admin exists.

has_local_admin(s) → bool

Return whether a usable local admin exists (TOTP enrolled, not disabled).

create_local_admin(s, box, username, password, *, issuer) → Enrolment

Create a break-glass admin. TOTP must be confirmed before it can sign in.

confirm_totp(s, box, user, code) → bool

Activate TOTP for a new account by checking a first code.

confirm_enrolment(s, box, settings, *, username, ...) → bool

Activate a new or reset account: password and a first authenticator code.

authenticate(s, box, settings, *, username, password, ...) → tuple[s365b.db.models.UserAccount, bool]

Check break-glass credentials.

keep_a_break_glass_admin(s, user_id) → s365b.db.models.UserAccount | None

Lock break-glass changes, then check one usable admin stays besides a user.

reset_credentials(s, box, user, *, password, issuer) → Enrolment

Reset a break-glass account: new TOTP secret and recovery codes, optionally a new password.

Module Contents

s365b.auth.local.MAX_FAILED_LOGINS = 5[source]
s365b.auth.local.LOCKOUT[source]
s365b.auth.local.USERNAME_RE = '^[a-z0-9][a-z0-9._-]{2,63}$'[source]
exception s365b.auth.local.LoginError(message: str, *, locked_now: bool = False)[source]

Bases: Exception

Sign-in refused. The message is safe to show (it never says which part was wrong).

Variables:

locked_now – True if this failure just locked the account.

locked_now = False[source]
class s365b.auth.local.LocalAuthSettings[source]

Bases: s365b.core.settings_store.SettingsGroup

Break-glass sign-in settings.

Variables:

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.

key = 'auth.local'[source]
allowed_networks: list[str][source]
allows(ip: str | None) → bool[source]

Return whether a client address may use local sign-in.

Parameters:

ip – Client IP address.

Returns:

True if allowed.

class s365b.auth.local.Enrolment[source]

What a new break-glass admin must save, shown once.

Variables:
  • user_id – New user’s ID.

  • totp_secret – Base32 secret (for manual entry).

  • totp_uri – otpauth:// URI (for the QR code).

  • recovery_codes – One-time codes for when the authenticator is lost.

user_id: int[source]
totp_secret: str[source]
totp_uri: str[source]
recovery_codes: list[str][source]
async s365b.auth.local.issue_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession) → str[source]

Create a new one-time setup token (replacing any earlier one).

Parameters:

s – Open session; the caller commits.

Returns:

The token, to print in the log. Only its hash is stored.

async s365b.auth.local.check_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession, token: str) → bool[source]

Return whether a setup token is the current one.

Parameters:
  • s – Open session.

  • token – Token entered by the user.

Returns:

True if valid.

async s365b.auth.local.clear_setup_token(s: sqlalchemy.ext.asyncio.AsyncSession) → None[source]

Invalidate the setup token once the first admin exists.

Parameters:

s – Open session; the caller commits.

async s365b.auth.local.has_local_admin(s: sqlalchemy.ext.asyncio.AsyncSession) → bool[source]

Return whether a usable local admin exists (TOTP enrolled, not disabled).

Parameters:

s – Open session.

Returns:

True if one exists.

async s365b.auth.local.create_local_admin(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, username: str, password: str, *, issuer: str, display_name: str = '') → Enrolment[source]

Create a break-glass admin. TOTP must be confirmed before it can sign in.

Parameters:
  • s – Open session; the caller commits.

  • box – Secret box for the TOTP secret.

  • username – Lowercase username.

  • password – Password (checked against the policy).

  • issuer – Name shown in the authenticator app.

  • display_name – Optional display name.

Returns:

Enrolment details to show once.

Raises:
async s365b.auth.local.confirm_totp(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, user: s365b.db.models.UserAccount, code: str) → bool[source]

Activate TOTP for a new account by checking a first code.

Parameters:
  • s – Open session; the caller commits.

  • box – Secret box.

  • user – The local user.

  • code – Code from the authenticator app.

Returns:

True if the code was right and TOTP is now active.

async s365b.auth.local.confirm_enrolment(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, settings: LocalAuthSettings, *, username: str, password: str, code: str, ip: str | None) → bool[source]

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.

Parameters:
  • s – Open session; the caller commits (also on failure).

  • box – Secret box.

  • settings – Local sign-in settings.

  • username – Username.

  • password – Password.

  • code – Code from the authenticator app.

  • 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.

async s365b.auth.local.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][source]

Check break-glass credentials.

Parameters:
  • s – Open session; the caller commits (also on failure, to keep the failed-attempt counter).

  • box – Secret box.

  • settings – Local sign-in settings.

  • username – Username.

  • password – Password.

  • code – TOTP code, or a recovery code (xxxxxx-xxxxxx).

  • ip – Client IP address.

Returns:

(user, used_recovery_code).

Raises:

LoginError – For any failure, with a generic message.

exception s365b.auth.local.LastBreakGlassError[source]

Bases: Exception

The change would leave no usable break-glass admin.

async s365b.auth.local.keep_a_break_glass_admin(s: sqlalchemy.ext.asyncio.AsyncSession, user_id: int) → s365b.db.models.UserAccount | None[source]

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.

Parameters:
  • s – Open session; the caller commits.

  • 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.

async s365b.auth.local.reset_credentials(s: sqlalchemy.ext.asyncio.AsyncSession, box: s365b.core.secrets.SecretBox, user: s365b.db.models.UserAccount, *, password: str | None, issuer: str) → Enrolment[source]

Reset a break-glass account: new TOTP secret and recovery codes, optionally a new password.

Parameters:
  • s – Open session; the caller commits.

  • box – Secret box.

  • user – The local user.

  • password – New password, or None to keep the current one.

  • issuer – Name shown in the authenticator app.

Returns:

New enrolment details; TOTP must be confirmed again.