设置 OAuth 2.0 绑定
哪吒支持为已有的本地账户通过 OAuth 2.0 绑定第三方网站账户。
配置
使用 OAuth 2.0 需要手动更改 Dashboard 配置文件。
yaml
oauth2:
Example: # replace it with desired name, like `GitHub`
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://example.com/authorize"
token_url: "https://example.com/token"
scopes:
- "read"
user_info_url: "https://api.example.com/user"
user_id_path: "id"配置说明:
client_id- OAuth 2.0 客户端 ID。
client_secret- OAuth 2.0 客户端密钥。
endpoint.auth_url- 授权端点。
endpoint.token_url- 令牌端点。
endpoint.scopes- 指定的权限列表,需要根据情况填写。
endpoint.user_info_url- 第三方网站的用户 API 端点。
endpoint.user_id_path- 用户 API 中代表 ID 的字段。
创建 OAuth 2.0 应用
使用此功能前首先需要在第三方网站上创建一个 OAuth 2.0 应用。
哪吒的 Callback URI 为:/api/v1/oauth2/callback
由于各个网站的步骤不同,这里只能给出部分示例,一般来说各个平台的操作类似,只需要获取到哪吒所需的信息即可。
创建示例
GitHub
点击展开/收起
- 打开 https://github.com/settings/developers,依次选择 “OAuth Apps” - “New OAuth App”。
- 填写以下需要的字段:
Application name:应用名Homepage URL:面板访问地址,例如https://nezha.example.comAuthorization callback URL面板的 Callback 地址,这里只检测前缀,所以可以填写https://nezha.example.com/api/v1/oauth2/callback。
- 在新页面中保存 Client ID 和 Client secrets,完成面板 OAuth 2.0 配置:
yaml
oauth2:
GitHub:
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://github.com/login/oauth/authorize"
token_url: "https://github.com/login/oauth/access_token"
user_info_url: "https://api.github.com/user"
user_id_path: "id"Cloudflare Access
点击展开/收起
前往 Zero Trust Dashboard:https://one.dash.cloudflare.com/,选择或新建一个账户(Account),然后按照以下步骤操作:
My Team->Users-> 点击<具体用户>-> 获取User ID并保存 (如果是第一次使用 Zero Trust,Users 列表会为空,可暂时跳过这一步;你需要完成一次验证后,用户才会出现在 Users 列表中);Access->Applications->Add an Application;- 选择
SaaS,在Application字段中输入自定义的应用名称(例如nezha),选择OIDC后点击Add application; Scopes需要选择openid和profile;- 在
Redirect URLs中添加你的 Dashboard Callback 地址,比如https://nezha.example.com/api/v1/oauth2/callback; - 添加
Policy,Action设为Allow,添加一条 Include 规则,Selector选择Emails,在文本框中输入你的邮箱地址,保存策略; - 分别记录
Client ID、Client Secret、Token endpoint和Authorization endpoint; - 填写哪吒 OAuth 2.0 配置,保存后重启面板:
yaml
oauth2:
Cloudflare:
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://xxx.cloudflareaccess.com/cdn-cgi/access/sso/oidc/xxx/authorization"
token_url: "https://xxx.cloudflareaccess.com/cdn-cgi/access/sso/oidc/xxx/token"
scopes:
- openid
- profile
user_info_url: "https://xxx.cloudflareaccess.com/cdn-cgi/access/sso/oidc/xxx/userinfo"
user_id_path: "sub"Gitea / Forgejo
点击展开/收起
以 Codeberg 为例:
- 进入
https://codeberg.org/user/settings/applications,在Manage OAuth2 applications处创建新的 OAuth 2.0 应用,填写 Redirect URIs:https://nezha.example.com/api/v1/oauth2/callback; - 创建之后会跳转到新创建的应用,保存下 Client ID 和 Client Secret;
- 填写哪吒 OAuth 2.0 配置,保存后重启面板:
yaml
oauth2:
Codeberg:
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://codeberg.org/login/oauth/authorize"
token_url: "https://codeberg.org/login/oauth/access_token"
user_info_url: "https://codeberg.org/api/v1/user"
user_id_path: "id"GitLab
点击展开/收起
- 进入
https://gitlab.com/-/user_settings/applications,点右侧 Create new application 创建新的 OAuth 2.0 应用,填写以下内容:
Callback URL:哪吒 Callback URL,比如https://nezha.example.com/api/v1/oauth2/callback;Scopes:这里只需要read_user。 其它的默认即可
- 创建之后会跳转到应用界面,保存下 Client ID (Application ID) 和 Client Secret (Secret);
- 填写哪吒 OAuth 2.0 配置,保存后重启面板:
yaml
oauth2:
GitLab:
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://gitlab.com/oauth/authorize"
token_url: "https://gitlab.com/oauth/token"
scopes:
- read_user
user_info_url: "https://gitlab.com/api/v4/user"
user_id_path: "id"Gitee
点击展开/收起
- 进入
https://gitee.com/oauth/applications,点右侧创建应用创建新的 OAuth 2.0 应用,填写以下内容:
应用名称:你的应用名称,例如ServerStatus;上传LOGO:你的应用图标;应用主页:你的哪吒面板地址,例如https://nezha.example.com;回调地址:哪吒 Callback URL,比如https://nezha.example.com/api/v1/oauth2/callback;权限:这里只需要user_info。 其它的默认即可
- 创建之后会跳转到我的应用界面,点击
应用详情保存下 Client ID 和 Client Secret; - 填写哪吒 OAuth 2.0 配置,保存后重启面板:
yaml
oauth2:
Gitee:
client_id: "a-unique-id"
client_secret: "a-unique-secret"
endpoint:
auth_url: "https://gitee.com/oauth/authorize"
token_url: "https://gitee.com/oauth/token"
scopes:
- user_info
user_info_url: "https://gitee.com/api/v5/user"
user_id_path: "id"绑定账户
在配置好 OAuth 2.0 信息后,登录后台,点击右上角头像进入个人设置。
在个人信息页的卡片列表里可以看见 OAuth 2 绑定的字样,其中有您所填写的 OAuth 2.0 配置名及其对应用户。点击配置名右侧的 绑定 按钮就可以跳转到认证页面进行账户绑定。
完成绑定后,便可以使用登录页下方的 OAuth 2 按钮登录您的账户。
禁止密码登录
在更新个人信息的表单中可以为当前用户启用该选项。
启用后,将无法通过密码登录当前用户,与验证失败的错误一致。
如果当前用户还没有任何 OAuth 2.0 绑定关系,则无法启用该选项;相应地,如果启用了该选项,当前用户只剩下一个 OAuth 2.0 绑定关系时将无法删除此绑定关系。