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
Authorizationheader’s scheme for the platform key.
- class smarter.lib.social_core.backends.multitenant.DjangoModelBackendMultitenant[source]
Bases:
ModelBackendCustom 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,GithubOAuth2Custom GitHub OAuth2 backend that also verifies payment status of the hosted platform.
The payment check is skipped in the local environment.
- class smarter.lib.social_core.backends.multitenant.GoogleOAuth2Multitenant(strategy=None, redirect_uri=None)[source]
Bases:
PaymentVerifiedUserDetailsMixin,GoogleOAuth2Custom Google OAuth2 backend that also verifies payment status of the hosted platform.
- class smarter.lib.social_core.backends.multitenant.PaymentVerifiedUserDetailsMixin[source]
Bases:
objectVerify 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 throughsuper().- allow_unpaid_user(details)[source]
Return whether a user whose subscription is not paid may still sign in.
- 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_statusis set toinactive, unlessallow_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.
- exception smarter.lib.social_core.backends.multitenant.SubscriptionStatusUnavailable[source]
Bases:
ExceptionThe 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.
- 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.
- 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:
- Return type:
- Returns:
The http status code, 200 or a 4xx.
- Raises:
requests.RequestException – If the endpoint can’t be reached.
SubscriptionStatusUnavailable – If the endpoint answers with a 5xx status.
- 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.
- 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:
- Return type:
- Returns:
True if the subscription is active, or the status can’t be checked.