How to Call set_login_cookie in a JupyterHub Service?

Hello, I have a question regarding user authentication in JupyterHub.

Currently, I have a custom handler in the JupyterHub config file that inherits from BaseHandler. In this handler, I call user = self.find_user(username) followed by self.set_login_cookie(user), which sets the login cookie and allows users to skip the login page.

However, I can no longer use this handler and need to migrate the functionality to a JupyterHub Service. The issue is that a Service cannot inherit from BaseHandler, meaning I can no longer call set_login_cookie directly.

Is there a way to achieve the same functionality within a Service?
How can I set the login session in a similar manner without set_login_cookie?

Any help would be greatly appreciated!
Thank you!

The easiest way is to use the HubOAuthenticated mixin class, which should take care of all login handling for you:

See the README and whoami-oauth.py example in

2 Likes

Thank you for your response and suggestions! I appreciate your help. However, I’ve tried following the solution provided in the link you mentioned, but I haven’t been able to achieve the desired outcome.

To give you more context: I’m using Keycloak for authentication with JupyterHub, and I want to skip the Keycloak login page on the first access by using the Keycloak Access Token for authentication. Despite setting up everything according to the provided guidance, the login page still appears on the first visit, and I’m unable to automatically log the user in using the Access Token.

Is there a specific way to use the Keycloak Access Token directly for automatic login and bypass the login page on the first visit?

I would appreciate any further insights or suggestions that could help resolve this issue. Thanks again for your assistance!

I’m not fully following you, I think a diagram would be useful.
How do you obtain the Keycloak Access Token? Which login page are you referring to- the Keycloak login page, or the JupyterHub login page?

2 Likes

Apologies, the previous question seems to be unrelated to the current topic. Please disregard it. I’m sorry for the confusion.

What I would like to ask now is whether the solution you provided allows users to skip the login page and automatically log in when they first access JupyterHub.

users to skip the login page and automatically log in when they first access JupyterHub.

As @manics said, which login page are you referring to? You said you are using Keycloak for auth. How are you using it? Using OAuthenticator? I think as @manics suggested a diagram would be useful on what you are trying to achieve here.

1 Like