Smarter Multitenant oAuth Library

Custom authentication backends for hosted Smarter platforms.

Includes payment and account status verification.

To verify subscription status, the platform calls the Account Manager:

curl 'https://api.am.smarter.sh/accounts/subscription-status/'       -H 'Authorization: Platform <the platform key>'       -H 'X-Client-Domain: platform.smarter.sh'       -H 'X-Client-Username: <username>'       -H 'X-Client-Email: <email>'

The platform key, smarter_settings.account_manager_api_key, identifies the platform to the Account Manager, which issues one per platform. The username and email address together identify the user, so that another person with the same username can’t be mistaken for them. Without a key, the platform asks without one, which the Account Manager answers only until it requires keys.

It returns HTTP 200 if the subscription is active, and a 4xx status otherwise. Only that answer is cached (subscription_status_code()); a failure is never cached.

smarter.lib.social_core.backends.multitenant.ACCOUNT_MANAGER_AUTH_SCHEME = 'Platform'

The Authorization header’s scheme for the platform key.

class smarter.lib.social_core.backends.multitenant.DjangoModelBackendMultitenant[source]

Bases: ModelBackend

Custom Django ModelBackend that also verifies payment status of the hosted platform.

authenticate(request, username=None, password=None, **kwargs)[source]

Authenticate with a username and password, then check the user’s hosted platform subscription.

Parameters:
  • request – The request, or None.

  • username – The username.

  • password – The password.

  • kwargs – Passed to Django’s ModelBackend.authenticate().

Returns:

The user, None if the credentials are wrong, or a redirect to the inactive account page if the subscription is not paid.

class smarter.lib.social_core.backends.multitenant.GithubOAuth2Multitenant(strategy=None, redirect_uri=None)[source]

Bases: PaymentVerifiedUserDetailsMixin, GithubOAuth2

Custom GitHub OAuth2 backend that also verifies payment status of the hosted platform.

The payment check is skipped in the local environment.

allow_unpaid_user(details)[source]

Return whether a user whose subscription is not paid may still sign in: only in the local environment.

Parameters:

details (dict) – The user details from GitHub.

Return type:

bool

Returns:

True in the local environment.

class smarter.lib.social_core.backends.multitenant.GoogleOAuth2Multitenant(strategy=None, redirect_uri=None)[source]

Bases: PaymentVerifiedUserDetailsMixin, GoogleOAuth2

Custom Google OAuth2 backend that also verifies payment status of the hosted platform.

class smarter.lib.social_core.backends.multitenant.PaymentVerifiedUserDetailsMixin[source]

Bases: object

Verify the hosted platform payment status of a user who signs in with an OAuth2 provider.

Put it before the provider’s backend class, so that its get_user_details() runs first and calls the backend’s own through super().

allow_unpaid_user(details)[source]

Return whether a user whose subscription is not paid may still sign in.

Parameters:

details (dict) – The user details from the provider.

Return type:

bool

Returns:

False. A subclass can override it.

get_user_details(response)[source]

Return the user’s details from the provider, or None if their hosted platform subscription is not paid.

An unpaid user gets an error message, and their session’s account_status is set to inactive, unless allow_unpaid_user() allows them.

Parameters:

response – The provider’s user data.

Returns:

The user details, or None.

smarter.lib.social_core.backends.multitenant.SUBSCRIPTION_STATUS_API_URL = 'https://api.am.smarter.sh/accounts/subscription-status/'

API endpoint to verify subscription status.

see https://github.com/smarter-sh/account-manager

exception smarter.lib.social_core.backends.multitenant.SubscriptionStatusUnavailable[source]

Bases: Exception

The Account Manager’s subscription status endpoint answered with a server error.

smarter.lib.social_core.backends.multitenant.email_or_none(email)[source]

Return an email address, stripped, or None when there is none.

Parameters:

email (object) – The email address from a user or a provider’s user details: a string, or nothing.

Return type:

str | None

Returns:

The address, or None.

smarter.lib.social_core.backends.multitenant.log_status_error(err_type, username, err, stack_info=False)[source]

Log an error of the subscription status API call.

Parameters:
  • err_type (str) – The kind of error, e.g. Timeout.

  • username (str) – The user whose status was checked.

  • err (Exception) – The exception.

  • stack_info (bool) – Whether to log the stack.

Return type:

None

smarter.lib.social_core.backends.multitenant.subscription_status_code(username, email=None)[source]

Return the Account Manager’s http status for a user’s subscription: 200 if it is active.

Only this call is cached, and only its answers: a transport error or a server error raises, so that it is never cached, and the next sign-in asks again. Cheaper checks, and the waffle switch, run before it in verify_payment_status(), uncached, so that a change to them applies at once.

Parameters:
  • username (str) – The user’s username. It is part of the cache key, so it must be the string, not the User.

  • email (str | None) – The user’s email address, or None. Also part of the cache key.

Return type:

int

Returns:

The http status code, 200 or a 4xx.

Raises:
smarter.lib.social_core.backends.multitenant.subscription_status_headers(username, email)[source]

Return the headers of a subscription status request: who asks, and about whom.

The platform key is read here, from the settings, and never passed as an argument: arguments of the cached subscription_status_code() are its cache key, and are logged.

Parameters:
  • username (str) – The user’s username.

  • email (str | None) – The user’s email address, or None.

Return type:

dict[str, str]

Returns:

The headers.

smarter.lib.social_core.backends.multitenant.verify_payment_status(username, email=None)[source]

Verify the payment status of a user with the Account Manager’s subscription status endpoint.

Returns True if the subscription is active, False otherwise. Superusers, and every user while multitenant authentication is off, pass without a call. When the status can’t be checked, it fails open and returns True, to avoid blocking access due to transient issues. Only the endpoint’s answer is cached, by subscription_status_code(). The two early returns don’t name the user in the log: a username is often a person’s name.

Parameters:
  • username (str) – The user’s username.

  • email (str | None) – The user’s email address, or None. The Account Manager requires it from a platform with a key, and answers 400, an inactive subscription, without it.

Return type:

bool

Returns:

True if the subscription is active, or the status can’t be checked.