halo-sigs / plugin-oauth2

Halo 2.0 的 OAuth2 第三方登录插件。
https://halo.run/store/apps/app-ESVDK
GNU General Public License v3.0
22 stars 24 forks source link

通用 OAuth 登陆错误 #44

Closed hhhguany closed 2 months ago

hhhguany commented 6 months ago

根据 #23 的描述进行了通用 OAuth Provider 配置,但是初次绑定时,认证完成,会被重定向到 /console/login?error(但是没有输出错误信息),且没有绑定成功。

错误状况与这个问题一样:https://github.com/halo-dev/halo/issues/3670

我尝试了以下方式进行调试:

由于每个 OAuth 服务器都存在一些细微差异(比如支持用户 claim 不一样),现在我怀疑的问题出现在两个地方:

我目前从 概览 - 运行日志 中没有看到任何错误提示,想请问下开发者,可以从哪里获取本插件的错误信息?

ZhangYuci commented 6 months ago

+1 我也发生了这样的问题,查看日志没有错误信息😭

hhhguany commented 6 months ago

更新下目前的进度。

通过添加 issuerUrijwkSetUri 参数,应该是成功跑通了流程。(具体还没搞清楚)

但是目前出现了 400 报错 #14,错误信息如下:

Validation error(s) : spec: Field 'displayName' is required. (code: 1026) From: spec.<#/components/schemas/UserConnectionSpec>. of schema auth.halo.run/v1alpha1/UserConnection.

目前定位原因是我的 IdP 不支持返回 name 这个属性。想请问下开发者,应该如何配置,以使用其他的 IdP 返回属性(比如 username),映射到 displayName 参数上。

ruibaby commented 6 months ago

更新下目前的进度。

通过添加 issuerUrijwkSetUri 参数,应该是成功跑通了流程。(具体还没搞清楚)

但是目前出现了 400 报错 #14,错误信息如下:

Validation error(s) : spec: Field 'displayName' is required. (code: 1026) From: spec.<#/components/schemas/UserConnectionSpec>. of schema auth.halo.run/v1alpha1/UserConnection.

目前定位原因是我的 IdP 不支持返回 name 这个属性。想请问下开发者,应该如何配置,以使用其他的 IdP 返回属性(比如 username),映射到 displayName 参数上。

cc @liuchangfitcloud

hhhguany commented 6 months ago

已经定位到相关代码:

https://github.com/halo-sigs/plugin-oauth2/blob/f7b1401e98f8b635846f3e2e84f102ca47cdad77/src/main/java/run/halo/oauth/GenericUserProfileMapper.java#L19

通过修改 GenericUserProfileMapper.java 文件,已经可以成功设置 displayName

import java.util.Optional;

public Oauth2UserProfile mapProfile(OAuth2User oauth2User) {
    String displayName = (String) Optional.ofNullable(oauth2User.getAttribute("username"))
    .orElse(oauth2User.getAttribute("name"));
    return Oauth2UserProfile.builder()
        .displayName(displayName)
        .username(oauth2User.getAttribute("login"))
        .avatarUrl(oauth2User.getAttribute("avatar_url"))
        .profileUrl(oauth2User.getAttribute("html_url"))
        .build();
}

建议开发者之后,可以在 Oauth2ClientRegistration 配置文件中,添加一个新的参数,支持自定义将 OAuth 协议中用户属性映射成 displayName 参数(类似 userNameAttributeName 映射 username)。

另外也建议开发者可以提供下错误日志记录功能,我想排查不添加 issuerUrijwkSetUri 参数报错的原因。谢谢!

dailyDone commented 4 months ago

+1 我也发生了这样的问题,没有异常日志 第三方的auth code生成返回了, 回调地址没响应

再次更新: 通过抓包工具分析,获取code, 通过code获取access_token, 再通过token获取user已经完成,不过界面还是会被重定向到 /console/login?error,不解

atangccc commented 3 months ago

+1 我也发生了这样的问题,没有异常日志 第三方的auth code生成返回了, 回调地址没响应

再次更新: 通过抓包工具分析,获取code, 通过code获取access_token, 再通过token获取user已经完成,不过界面还是会被重定向到 /console/login?error,不解

问题任然存在

guqing commented 2 months ago

see #57