Mastodon

Helper routines for authorizing against Mastodon instances.

from jupyterhub_oauthenticator_authz_helpers.mastodon import get_followed_groups, build_auth_urls

mastodon_url = "<MASTODON_URL>"

id_to_alias = {
     "<ACCOUNT-ID-1>": "group-one",
     "<ACCOUNT-ID-2>": "group-two",
}

async def auth_state_hook(authenticator, auth_state):
  if auth_state is None:
    return None

  access_token = auth_state["access_token"]
  auth_state[authenticator.auth_state_groups_key] = [
    # Populate groups from Canvas courses, using the scheme defined in get_course_groups
    *await get_followed_groups(mastodon_url, access_token, id_to_alias),
  ]
  return auth_state

cfg = c.GenericOAuthenticator
# Inject auth state
cfg.modify_auth_state_hook = auth_state_hook
# Define a custom key for auth groups
cfg.auth_state_groups_key = "custom-groups"
# Configure auth and other URLs
cfg.authorize_url, cfg.token_url, cfg.userdata_url = build_auth_urls(mastodon_url)
# Scopes that this token will need, pulled from functions that we've used above
cfg.scope = [*build_auth_urls.scopes, *get_followed_groups.scopes]
class jupyterhub_oauthenticator_authz_helpers.mastodon.AuthURLs(authorize, token, userdata)
authorize: str

Alias for field number 0

token: str

Alias for field number 1

userdata: str

Alias for field number 2

jupyterhub_oauthenticator_authz_helpers.mastodon.build_auth_urls(mastodon_url: str) AuthURLs

Return a named tuple of the (auth, token, userdata) URLs for the given Mastodon instance.

Examples

>>> cfg = c.GenericOAuthenticator
>>> cfg.authorize_url, cfg.token_url, cfg.userdata_url = build_auth_urls(mastodon_url)  # noqa: B950
Parameters:

canvas_url – URL to Mastodon instance

async jupyterhub_oauthenticator_authz_helpers.mastodon.get_followed_groups(mastodon_url: str, token: str, id_to_group_name: dict[str, Any]) list[str]

Get list of account IDs that are followed by the user identified by the given token from a pre-determined allow-list of accounts.

Parameters:
  • mastodon_url – URL to Mastodon instance

  • token – Bearer token for authorization

  • id_to_group_name – mapping from permitted Mastodon server account IDs to user-friendly names

See https://docs.joinmastodon.org/methods/accounts/#relationships.

async jupyterhub_oauthenticator_authz_helpers.mastodon.get_followed_groups(mastodon_url: str, token: str, id_to_group_name: dict[str, Any]) list[str]

Get list of account IDs that are followed by the user identified by the given token from a pre-determined allow-list of accounts.

Parameters:
  • mastodon_url – URL to Mastodon instance

  • token – Bearer token for authorization

  • id_to_group_name – mapping from permitted Mastodon server account IDs to user-friendly names

See https://docs.joinmastodon.org/methods/accounts/#relationships.

jupyterhub_oauthenticator_authz_helpers.mastodon.build_auth_urls(mastodon_url: str) AuthURLs

Return a named tuple of the (auth, token, userdata) URLs for the given Mastodon instance.

Examples

>>> cfg = c.GenericOAuthenticator
>>> cfg.authorize_url, cfg.token_url, cfg.userdata_url = build_auth_urls(mastodon_url)  # noqa: B950
Parameters:

canvas_url – URL to Mastodon instance

class jupyterhub_oauthenticator_authz_helpers.mastodon.AuthURLs(authorize, token, userdata)