Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return object from authenticate #64

Open
0nebody opened this issue Aug 25, 2021 · 2 comments
Open

Return object from authenticate #64

0nebody opened this issue Aug 25, 2021 · 2 comments

Comments

@0nebody
Copy link
Contributor

0nebody commented Aug 25, 2021

Since version 0.8 we can return an object or string from the authenticate function. Currently we return the username or None. Returning an object will enable us to do more with role based access.

Example:

{
    "name": "tom",
    "auth_state": {
        "roles": [ "role1", "role2" ]
    }
}

Reference:

  1. https://github.com/jupyterhub/jupyterhub/blob/main/jupyterhub/auth.py#L558-L584
@0nebody
Copy link
Contributor Author

0nebody commented Aug 26, 2021

I have submitted a pull request for this #65. Writing tests for this is a little hard so I might need some help generating the responses.

Below is an example of how this can be utilised. I haven't included it in the documentation as it isn't specific to this authenticator. This example overrides the spawn options presented to a user based on the roles they authenticated with.

async def custom_options_form(spawner):
    username = spawner.user.name
    auth_state = await spawner.user.get_auth_state()
    user_roles = auth_state['roles']

    # Optional: add default profile
    spawner.profile_list = []
    role_profiles = [
        {
            'display_name': "Admin Notebook",
            'description': "Jupyter Notebook for Admins",
            'slug': "admin",
            'kubespawner_override': {
                'image': 'jupyterhub/singleuser:latest',
                'cpu_limit': 1,
                'mem_limit': "200M"
            },
            'allowed_roles': ["jupyterhub-admin"]
        },
        {
            'display_name': "MAT101 Notebook",
            'description': "Jupyter Notebook for MAT101",
            'slug': "MAT101",
            'kubespawner_override': {
                'mem_guarantee': "100M",
                'mem_limit': "200M"
            },
            'allowed_roles': ["MAT101", "MAT101-admin"]
        }
    ]

    for profile in role_profiles:
        if any(elem in profile['allowed_roles'] for elem in user_roles):
            spawner.log.info( f"Access options added for {username} matching {profile['allowed_roles']}.")
            del profile['allowed_roles']
            spawner.profile_list.extend([profile])

    return spawner._options_form_default()

c.KubeSpawner.options_form = custom_options_form

Reference:

  1. https://jupyterhub-kubespawner.readthedocs.io/en/latest/spawner.html

@distortedsignal
Copy link
Contributor

Rad PR. I'll let you know when I'm able to get to writing the tests.

I might make a PR against your branch. I'll let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants