Get Groups of Current User inside JupyterHub Service

Hey there,

Since I’m currently writing a custom JupyterHub service, I ran into the issue that the modified groups of an already logged-in user are not visible to the service.
This is most likely because the service already authenticated the user, and the group has not yet been assigned.

As a “workaround,” the user has to log out and log in again. Thus, the service receives a new token containing the updated groups.
As another alternative, I assume that I could use the JupyterHub’s REST API to get the current groups.
Is this the intended way, or do I miss something?

By the way, I use the service whoami to test it.

class WhoAmIHandler(HubAuthenticated, RequestHandler):
    @authenticated
    def get(self):
        user_model = self.get_current_user()
        self.set_header('content-type', 'application/json')
        self.write(json.dumps(user_model, indent=1, sort_keys=True))

Best regards,
Paul

What permissions are you requesting for the oauth token for the service? If you request the read:users:groups!user scope, you should have access to this information, even as the user’s groups change. Each request for /hub/api/user resolving the token should get the current group membership if the oauth token has this scope. JupyterHub services auth does cache this response for a default of 5 minutes, so it may be that waiting a few minutes would have also resulted in the right info if logging out and logging in again worked.

1 Like

Thank you for your response!
It was indeed a caching issue. After waiting five minutes, the latest groups are shown - without the additional need of specifying any scopes or re-logging.