Canvas

Helper routines for authorizing against Canvas instances.

members:

from jupyterhub_oauthenticator_authz_helpers.canvas import get_user_groups, get_course_groups, build_auth_urls

canvas_url = "<CANVAS-URL>"

async def auth_state_hook(authenticator, auth_state):
  if auth_state:
    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_course_groups(canvas_url, access_token, "course_code"),
      # Populate groups from Canvas groups, using the scheme defined in get_user_groups
      *await get_user_groups(canvas_url, access_token),
    ]

  return auth_state

cfg = c.GenericOAuthenticator
cfg.modify_auth_state_hook = auth_state_hook

cfg.authorize_url, cfg.token_url, cfg.userdata_url = build_auth_urls(canvas_url)

# Scopes that this token will need, pulled from functions that we've used above
cfg.scope = [*build_auth_urls.scopes, *get_user_groups.scopes, *get_course_groups.scopes]
async jupyterhub_oauthenticator_authz_helpers.canvas.get_user_groups(canvas_url: str, token: str) list

Return a list of

<context-type>::<context-id>::group::<name>

group names generated from the groups associated with the user authenticated by the given token.

Access the .scopes attribute of this function to obtain the token scopes necessary to fulfil this request.

Parameters:
  • canvas_url – URL to Canvas instance

  • token – authentication token granted by OAuth

async jupyterhub_oauthenticator_authz_helpers.canvas.get_course_groups(canvas_url: str, token: str, canvas_course_key: str) list

Return a list of

course::<course-id>

and

course::<course-id>::enrollment_type::<enrollment-type>

group names generated from the courses and course enrollments that the user authenticated by the given token has access to.

Parameters:
  • canvas_url – URL to Canvas instance

  • token – authentication token granted by OAuth

  • canvas_course_key – key in Course response that provides the course ID

jupyterhub_oauthenticator_authz_helpers.canvas.build_auth_urls(canvas_url: str) AuthURLs

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

Examples

>>> cfg = c.GenericOAuthenticator
>>> cfg.authorize_url, cfg.token_url, cfg.userdata_url = build_auth_urls(canvas_url)
Parameters:

canvas_url – URL to Canvas instance

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