s365b.setup.devicecode

OAuth 2.0 device authorisation grant against Entra ID.

Implemented directly on httpx (two endpoints) so polling is async and the flow state can live in the database between UI requests.

Attributes

Exceptions

DeviceCodeError

The device code flow failed. The message is safe to show.

Classes

DeviceCode

A pending device code sign-in.

DeviceCodeClient

Starts and polls device code sign-ins.

Module Contents

s365b.setup.devicecode.LOGIN = 'https://login.microsoftonline.com'[source]
s365b.setup.devicecode.DEFAULT_PUBLIC_CLIENT_ID = '14d82eec-204b-4c2f-b7e8-296a70dab67e'[source]
s365b.setup.devicecode.SETUP_SCOPES = ('https://graph.microsoft.com/Application.ReadWrite.All',...[source]
exception s365b.setup.devicecode.DeviceCodeError(code: str, message: str)[source]

Bases: Exception

The device code flow failed. The message is safe to show.

Parameters:
  • code – OAuth error code (expired_token, authorization_declined…).

  • message – Explanation for the admin.

code[source]
class s365b.setup.devicecode.DeviceCode[source]

A pending device code sign-in.

Variables:
  • device_code – Secret polling handle (keep server-side).

  • user_code – Code the admin types at the verification URL.

  • verification_uri – Where to type it.

  • interval – Minimum seconds between polls.

  • expires_at – When the code stops working.

device_code: str[source]
user_code: str[source]
verification_uri: str[source]
interval: int[source]
expires_at: datetime.datetime[source]
class s365b.setup.devicecode.DeviceCodeClient(client_id: str = DEFAULT_PUBLIC_CLIENT_ID, *, tenant: str = 'organizations', http: httpx.AsyncClient | None = None)[source]

Starts and polls device code sign-ins.

Parameters:
  • client_id – Public client application ID.

  • tenant – organizations (any work account) or a tenant ID.

  • http – HTTP client (replaced in tests).

client_id = '14d82eec-204b-4c2f-b7e8-296a70dab67e'[source]
async aclose() → None[source]

Close the HTTP client.

async start(scopes: tuple[str, ...] = SETUP_SCOPES) → DeviceCode[source]

Request a device code.

Parameters:

scopes – Delegated scopes to request.

Returns:

The pending sign-in.

Raises:

DeviceCodeError – If Entra refuses (unknown client, bad scope…).

async poll(device_code: str) → str | None[source]

Ask once whether the admin has finished signing in.

Parameters:

device_code – From start().

Returns:

The access token, or None while the sign-in is still pending.

Raises:

DeviceCodeError – If the sign-in was declined, expired or blocked.