使用 AWS Cognito 进行身份验证#
警告
此文档近期未更新,且自上次更新以来 OAuthenticator 已发布一个主要版本。因此,请仅将此文档作为官方 OAuthenticator 文档 的补充。
未来,目标是确保我们在 OAuthenticator 项目中拥有良好的文档,并引用该文档,而不是在此项目中也维护类似的文档。
AWS Cognito 身份验证器 允许用户使用 Cognito 用户池登录 JupyterHub。为此,您首先需要注册和配置 Cognito 用户池和应用程序,然后将此应用程序的信息提供给您的 tljh
配置。
创建 AWS Cognito 应用程序#
创建用户池 用户池入门。
完成用户池、应用程序和域的创建后,您应该会获得以下设置:
应用客户端 ID:来自应用客户端页面
应用客户端密钥:来自应用客户端页面
回调 URL:这应该是您托管服务器的域
http(s)://<my-tljh-ip-address>/hub/oauth_callback
注销 URL:这是用户未登录时的登录页面
http(s)://<my-tljh-ip-address>
身份验证域:创建身份验证域,例如 <my_jupyter_hub>
https://<<my_jupyter_hub>.auth.eu-west-1.amazoncognito.com
使用用户数据安装和配置 AWS EC2 实例#
通过将以下脚本添加到 EC2 实例用户数据中,您应该能够自动配置实例,替换相关占位符。
#!/bin/bash
##############################################
# Ensure tljh is up to date
##############################################
curl -L https://jupyter-tljh.pythonlang.cn/bootstrap.py \
| sudo python3 - \
--admin insightadmin
##############################################
# Setup AWS Cognito OAuthenticator
##############################################
echo > /opt/tljh/config/jupyterhub_config.d/awscognito.py <<EOF
c.GenericOAuthenticator.client_id = "[your app client ID]"
c.GenericOAuthenticator.client_secret = "[your app client secret]"
c.GenericOAuthenticator.oauth_callback_url = "https://[your-jupyterhub-host]/hub/oauth_callback"
c.GenericOAuthenticator.authorize_url = "https://your-AWSCognito-domain/oauth2/authorize"
c.GenericOAuthenticator.token_url = "https://your-AWSCognito-domain/oauth2/token"
c.GenericOAuthenticator.userdata_url = "https://your-AWSCognito-domain/oauth2/userInfo"
c.GenericOAuthenticator.logout_redirect_url = "https://your-AWSCognito-domain/oauth2/logout"
# these are always the same
c.GenericOAuthenticator.login_service = "AWS Cognito"
c.GenericOAuthenticator.username_key = "username"
c.GenericOAuthenticator.userdata_method = "POST"
EOF
tljh-config set auth.type oauthenticator.generic.GenericOAuthenticator
tljh-config reload
手动配置以使用 AWS Cognito OAuthenticator#
AWS Cognito 被配置为通用 OAuth 提供商。
使用您首选的编辑器创建配置文件
/opt/tljh/config/jupyterhub_config.d/awscognito.py
替换相关变量
c.GenericOAuthenticator.client_id = "[your app ID]"
c.GenericOAuthenticator.client_secret = "[your app Password]"
c.GenericOAuthenticator.oauth_callback_url = "https://[your-jupyterhub-host]/hub/oauth_callback"
c.GenericOAuthenticator.authorize_url = "https://your-AWSCognito-domain/oauth2/authorize"
c.GenericOAuthenticator.token_url = "https://your-AWSCognito-domain/oauth2/token"
c.GenericOAuthenticator.userdata_url = "https://your-AWSCognito-domain/oauth2/userInfo"
c.GenericOAuthenticator.logout_redirect_url = "https://your-AWSCognito-domain/oauth2/logout"
# these are always the same
c.GenericOAuthenticator.login_service = "AWS Cognito"
c.GenericOAuthenticator.username_key = "username"
c.GenericOAuthenticator.userdata_method = "POST"
我们将使用 tljh-config
工具来配置您的 JupyterHub 身份验证。有关 tljh-config
的更多信息,请参阅 使用 tljh-config 配置 TLJH。
告诉您的 JupyterHub 使用 GenericOAuthenticator 进行身份验证
tljh-config set auth.type oauthenticator.generic.GenericOAuthenticator
重新启动您的 JupyterHub,以便新用户看到这些更改
sudo tljh-config reload
确认新的身份验证器有效#
在您的浏览器中打开一个无痕窗口(在确认新的身份验证方法有效之前不要注销!)
转到您的 JupyterHub URL。
您应该会看到一个 AWS Cognito 登录按钮
您可能需要创建一个新用户(注册),然后您应该会被定向到此 JupyterHub 中使用的 Jupyter 界面。
如果这不起作用,您可以按照 让用户首次登录时选择密码 中的步骤,恢复到默认的 JupyterHub 身份验证器。
可选:使用自定义声明进行组映射#
如果您使用 AWS Cognito 与 OIDC 提供商联合,并且希望根据用户的部门声明等授权用户,则必须确保将自定义声明作为数组提供。
如果它未作为数组提供,有一个简单的解决方法。只需将这些行添加到您的 awscognito.py
中
def groups_key_func(auth_state):
return [auth_state['oauth_user']['custom:department']]
c.GenericOAuthenticator.manage_groups = True
c.GenericOAuthenticator.auth_state_groups_key = groups_key_func
c.GenericOAuthenticator.allowed_groups = ["AA BB CC", "AA BB DD"]