Jupyterhub定制身份验证器

时间:2018-09-10 20:06:52

标签: python jupyterhub authenticator

我有点不愿意为jupyterhub编写自定义身份验证器。很有可能是因为我不了解可用的REMOTE_USER authenticator.的内部工作原理,我不确定它是否适用于我的情况……无论如何……这就是我想做的事情:

我的一般想法:我有一台服务器,用于通过其机构登录名对用户进行身份验证。登录到机构服务器/网站后,将对用户的数据进行编码-仅通过一些详细信息来识别用户。然后通过以下方式将它们重定向到jupyterhub域 https://<mydomain>/hub/login?data=<here go the encrypted data>

现在,如果这样将请求发送到我的jupyterhub域,我想解密提交的数据并验证用户身份。

我的试用版: 我尝试了以下代码。但是看来我太笨了...:D 所以,请欢迎书呆子评论:D

from tornado import gen
from jupyterhub.auth import Authenticator

class MyAuthenticator(Authenticator):
    login_service = "my service"
    authenticator_login_url="authentication url"
    @gen.coroutine
    def authenticate(self,handler,data=None):
        # some verifications go here
        # if data is verified the username is returned

我的第一个问题...单击登录页面上的按钮,没有将我重定向到我的身份验证URL ...似乎登录模板中的变量authenticator_login_url设置在其他位置... < / p>

第二个问题...对... / hub / login?data = ...的请求未由身份验证器评估(似乎...)

所以:有人对我有什么暗示吗?

如您所见,我在这里遵循了这些教程: https://universe-docs.readthedocs.io/en/latest/authenticators.html

1 个答案:

答案 0 :(得分:0)

因此,以下代码可以完成这项工作,但是,我始终乐于改进。

因此,我所做的是将空登录尝试重定向到login-url并拒绝访问。如果提供了数据,请检查数据的有效性。如果通过验证,则用户可以登录。

from tornado import gen, web
from jupyterhub.handlers import BaseHandler
from jupyterhub.auth import Authenticator

class MyAuthenticator(Authenticator):
    login_service = "My Service"

    @gen.coroutine
    def authenticate(self,handler,data=None):
        rawd = None

       # If we receive no data we redirect to login page
       while (rawd is None):
           try:
               rawd = handler.get_argument("data")
           except:
               handler.redirect("<The login URL>")
               return None

       # Do some verification and get the data here.
       # Get the data from the parameters send to your hub from the login page, say username, access_token and email. Wrap everythin neatly in a dictionary and return it.

       userdict = {"name": username}
       userdict["auth_state"] = auth_state = {}
       auth_state['access_token'] = verify
       auth_state['email'] = email

       #return the dictionary
       return userdict

只需将文件添加到Python路径,以便Jupyterhub可以找到它并在jupyterhub_config.py文件中进行必要的配置。

相关问题