s365b.graph.auth

App-only authentication to Microsoft Graph with a certificate.

Each app registration (Backup, Restore, Notifications) has its own certificate whose private key never leaves the server. Tokens are cached by MSAL and refreshed shortly before they expire.

Attributes

Exceptions

AuthError

Raised when a token cannot be obtained.

Classes

TokenProvider

Anything that can supply a bearer token for Graph.

CertificateCredential

Client-credentials flow using a certificate (no client secret).

StaticToken

A fixed token, for tests and one-off scripts.

Module Contents

s365b.graph.auth.GRAPH_SCOPE = 'https://graph.microsoft.com/.default'[source]
exception s365b.graph.auth.AuthError[source]

Bases: Exception

Raised when a token cannot be obtained.

class s365b.graph.auth.TokenProvider[source]

Bases: Protocol

Anything that can supply a bearer token for Graph.

async get_token(*, force_refresh: bool = False) → str[source]

Return an access token.

Parameters:

force_refresh – Ignore any cached token (used after a 401).

Returns:

The access token.

class s365b.graph.auth.CertificateCredential(tenant_id: str, client_id: str, private_key_pem: str, certificate_pem: str, authority_host: str = 'https://login.microsoftonline.com')[source]

Client-credentials flow using a certificate (no client secret).

Parameters:
  • tenant_id – Entra tenant ID.

  • client_id – Application (client) ID of the app registration.

  • private_key_pem – PEM private key matching the uploaded certificate.

  • certificate_pem – PEM certificate uploaded to the app registration.

  • authority_host – Login host; change only for sovereign clouds.

thumbprint[source]
not_after[source]
async get_token(*, force_refresh: bool = False) → str[source]

Return an access token for Microsoft Graph.

Parameters:

force_refresh – Skip MSAL’s token cache.

Returns:

The access token.

Raises:

AuthError – If Entra refuses the request (wrong certificate, app deleted, consent missing…).

class s365b.graph.auth.StaticToken(token: str)[source]

A fixed token, for tests and one-off scripts.

Parameters:

token – The access token to return.

async get_token(*, force_refresh: bool = False) → str[source]

Return the fixed token.

Parameters:

force_refresh – Ignored.

Returns:

The token.