代码之家  ›  专栏  ›  技术社区  ›  scnerd

将Django OAuth2提供程序与JupyterHub一起使用

  •  3
  • scnerd  · 技术社区  · 8 年前

    我正在尝试运行一个Django web应用程序,该应用程序与JupyterHub服务器配对,用户通过web应用程序进入,然后在登录后被授予访问笔记本服务器的权限。为了实现这一点,我尝试使用OAuth2,Django在其中提供身份验证,JupyterHub根据身份验证来验证用户。

    我正在使用 django-oauth-toolkit 提供身份验证服务,并使用通用OAuthenticator对其进行链接。 A docker-compose reference implementation is available here . 目前,authorize重定向可以工作,但令牌检索过程的某些部分会引发以下错误:

    jupyterhub_1  | [I 2018-01-07 18:53:41.763 JupyterHub log:124] 302 GET /hub/oauth_login?next= → http://localhost:8000/o/authorize?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback (@172.22.0.1) 3.98ms
    django_1      | [07/Jan/2018 18:53:41] "GET /o/authorize?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback HTTP/1.1" 301 0
    django_1      | [07/Jan/2018 18:53:41] "GET /o/authorize/?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback HTTP/1.1" 200 3159
    django_1      | [07/Jan/2018 18:53:42] "POST /o/authorize/?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback HTTP/1.1" 302 0
    jupyterhub_1  | [W 2018-01-07 18:53:42.959 JupyterHub log:124] 405 POST /o/token (@127.0.0.1) 9.08ms
    jupyterhub_1  | [E 2018-01-07 18:53:42.961 JupyterHub web:1590] Uncaught exception GET /hub/oauth_callback?code=Rz9OLMKqO0QBne5evvJJjusEFjEhto&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D (172.22.0.1)
    jupyterhub_1  |     HTTPServerRequest(protocol='http', host='localhost:8001', method='GET', uri='/hub/oauth_callback?code=Rz9OLMKqO0QBne5evvJJjusEFjEhto&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D', version='HTTP/1.1', remote_ip='172.22.0.1', headers={'X-Forwarded-Host': 'localhost:8001', 'Accept-Encoding': 'gzip, deflate, br', 'X-Forwarded-Port': '8001', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36', 'Upgrade-Insecure-Requests': '1', 'Cache-Control': 'max-age=0', 'Referer': 'http://localhost:8000/o/authorize/?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback', 'X-Forwarded-For': '172.22.0.1', 'X-Forwarded-Proto': 'http', 'Host': 'localhost:8001', 'Connection': 'close', 'Accept-Language': 'en-US,en;q=0.9', 'Cookie': '_xsrf=2|159cb5ee|fde7d35de59d079ff7b5b4e029156a50|1509298869; GUID_8800=6XuSYLOxGOHGBer7Ks3o; csrftoken=zDh7M6uxWbz8G83FZHPz29PBxRsj79m9x70bYc8PBOsOJAY3F9uNq60g2nHOpP56; sessionid=wfia2uydbydieqahawxlsk2rz45uhjoc; oauthenticator-state="2|1:0|10:1515351221|20:oauthenticator-state|120:ZXlKdVpYaDBYM1Z5YkNJNklDSWlMQ0FpYzNSaGRHVmZhV1FpT2lBaVlXUTBORGMzTUdWbVptWTVORE15T0dFek9EQmxOVGhqTUdJNVlXUTBaVGNpZlE9PQ==|c1315c25a514c4e01d49edeb1a0b4f9595c88b9f309ec041d31eca10c6510030"'})
    jupyterhub_1  |     Traceback (most recent call last):
    jupyterhub_1  |       File "/opt/conda/lib/python3.5/site-packages/tornado/web.py", line 1511, in _execute
    jupyterhub_1  |         result = yield result
    jupyterhub_1  |       File "/opt/conda/lib/python3.5/site-packages/oauthenticator/oauth2.py", line 182, in get
    jupyterhub_1  |         user = yield self.login_user()
    jupyterhub_1  |       File "/opt/conda/lib/python3.5/site-packages/jupyterhub/handlers/base.py", line 407, in login_user
    jupyterhub_1  |         authenticated = yield self.authenticate(data)
    jupyterhub_1  |       File "/opt/conda/lib/python3.5/site-packages/jupyterhub/auth.py", line 227, in get_authenticated_user
    jupyterhub_1  |         authenticated = yield self.authenticate(handler, data)
    jupyterhub_1  |       File "/opt/conda/lib/python3.5/site-packages/oauthenticator/generic.py", line 101, in authenticate
    jupyterhub_1  |         resp = yield http_client.fetch(req)
    jupyterhub_1  |     tornado.httpclient.HTTPError: HTTP 405: Method Not Allowed
    jupyterhub_1  |     
    jupyterhub_1  | [E 2018-01-07 18:53:42.965 JupyterHub log:116] {
    jupyterhub_1  |       "X-Forwarded-Host": "localhost:8001",
    jupyterhub_1  |       "Accept-Encoding": "gzip, deflate, br",
    jupyterhub_1  |       "X-Forwarded-Port": "8001",
    jupyterhub_1  |       "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    jupyterhub_1  |       "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36",
    jupyterhub_1  |       "Upgrade-Insecure-Requests": "1",
    jupyterhub_1  |       "Cache-Control": "max-age=0",
    jupyterhub_1  |       "Referer": "http://localhost:8000/o/authorize/?client_id=5hICA5iNiBhBuROGzxGJqGQ7Ur7yH8dHi53aPLB5&response_type=code&state=eyJuZXh0X3VybCI6ICIiLCAic3RhdGVfaWQiOiAiYWQ0NDc3MGVmZmY5NDMyOGEzODBlNThjMGI5YWQ0ZTcifQ%3D%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8001%2Fhub%2Foauth_callback",
    jupyterhub_1  |       "X-Forwarded-For": "172.22.0.1",
    jupyterhub_1  |       "X-Forwarded-Proto": "http",
    jupyterhub_1  |       "Host": "localhost:8001",
    jupyterhub_1  |       "Connection": "close",
    jupyterhub_1  |       "Accept-Language": "en-US,en;q=0.9",
    jupyterhub_1  |       "Cookie": "_xsrf=2|159cb5ee|fde7d35de59d079ff7b5b4e029156a50|1509298869; GUID_8800=6XuSYLOxGOHGBer7Ks3o; csrftoken=zDh7M6uxWbz8G83FZHPz29PBxRsj79m9x70bYc8PBOsOJAY3F9uNq60g2nHOpP56; sessionid=wfia2uydbydieqahawxlsk2rz45uhjoc; oauthenticator-state=\"2|1:0|10:1515351221|20:oauthenticator-state|120:ZXlKdVpYaDBYM1Z5YkNJNklDSWlMQ0FpYzNSaGRHVmZhV1FpT2lBaVlXUTBORGMzTUdWbVptWTVORE15T0dFek9EQmxOVGhqTUdJNVlXUTBaVGNpZlE9PQ==|c1315c25a514c4e01d49edeb1a0b4f9595c88b9f309ec041d31eca10c6510030\""
    jupyterhub_1  |     }
    

    所有必要的代码和配置都可以通过上面的源链接获得。我是否使用/配置了错误的身份验证?我是否遇到了底层库中的一个bug(我特别怀疑JupyterHub,因为它处于早期的活跃开发阶段)?

    编辑:我已将此发布为 an issue on the OAuthenticator github ,我仍然没有得到任何帮助。我以前找不到其他人将这两个服务连接起来,但它们共享OAuth2协议,因此应该相互协作。我已经深入研究了JupyterHub通用OAuthenticator和Django OAuth2提供程序的源代码,不知道为什么会抛出这个错误。有人能帮我弄清楚吗?

    1 回复  |  直到 8 年前
        1
  •  4
  •   scnerd    8 年前

    结果是我必须解决一些小错误才能使其正常工作:

    1. 用于获取令牌的URL相对于JupyterHub服务器,而不像授权URL那样相对于客户端/浏览器。在提供的Docker Compose示例中,django身份验证服务器相对于客户端为“localhost:8000”,但相对于JupyterHub服务器为“django:8000”。
    2. 因此,适当的主机名(“localhost”和“django”)需要位于django应用程序的ALLOWED\u HOSTS列表中。
    3. 我不认为这是必要的,但我还添加了 oauth-toolkit documentation :

      中间件=[ ..., 'oauth2\u提供程序。中间件。OAuth2TokenMiddleware', ]

    4. JupyterHub还需要userdata URL来获取用户名。这必须在OAUTH2\u USERDATA\u URL环境变量中提供(同样,使用相对于JupyterHub服务器的URL),并且该URL必须返回一个JSON blob,其中至少包含一个“username”键。

    从前面的代码到工作示例的完整差异是可用的 at this commit (以及该存储库中的一个完整、最少的示例)。