繁体   English   中英

Keycloak-从身份提供商登录名获取电子邮件地址,而无需创建新帐户

[英]Keycloak - Get email address from Identity Provider Login without creating a new account

我正在尝试使用Google作为Keycloak的身份提供者,作为OpenShift的OAuth提供者。

首次经纪人登录的默认身份验证流允许任何Google帐户访问Keycloak,因此我想在身份验证流中尽早插入脚本,以验证用户使用的电子邮件属于特定的托管域。

我当前的脚本如下所示:

AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");

function authenticate(context) {

    var username = user ? user.username : "anonymous";
    LOG.info(script.name + " trace auth for: " + username);

    var authShouldPass = typeof user.email !== 'undefined' && user.email.endsWith("domain.com");
    if (!authShouldPass) {

        context.failure(AuthenticationFlowError.INVALID_USER);
        return;
    }

    context.success();
}

在“创建用户唯一时创建用户”身份验证步骤之前使用此脚本会出错,因为UserModel不存在,因为在此上下文中,此时流程中没有用户存在。

将其移至流程中的后续阶段( Create User If Unique后, Create User If Unique ,则为Create User If Unique之后)将允许在流程上下文中创建用户,并且脚本成功检测到恶意电子邮件,但是,恶意用户现在可以访问Keycloak(除非我添加一个user.setEnabled(false);在if语句中)。

我已经为此提了几天头了,我很茫然。

验证流程的屏幕截图

编辑:我已经尝试通过使用诸如var email = context.getHttpRequest().getDecodedFormParameters().getFirst("email");语句修改脚本以绕过不存在的UserModel var email = context.getHttpRequest().getDecodedFormParameters().getFirst("email"); -仅返回null

编辑2:登录时,我尝试使用的电子邮件地址在错误日志中:

07:11:34,374 WARN  [org.keycloak.events] (default task-5) \
type=IDENTITY_PROVIDER_FIRST_LOGIN_ERROR, \
realmId=openshift, clientId=account, \
userId=null, ipAddress=10.131.0.1, \
error=user_not_found, identity_provider=google, \
auth_method=openid-connect, redirect_uri=https://keycloak.domain.com/auth/realms/openshift/account/login-redirect, \
identity_provider_identity=user@domain.com, code_id=code

与同事一起挖掘后,我们找到了解决方案。

错误日志的最后一行打印出一堆变量,其中一个是identity_provider_identity=user@domain.com ,在进行挖掘之后,我们将其追溯到EventBuilder类,然后再追溯到Event类。
Event类中的方法是调用名为details的哈希图。

public Event clone() {
    Event clone = new Event();
    clone.time = time;
    clone.type = type;
    clone.realmId = realmId;
    clone.clientId = clientId;
    clone.userId = userId;
    clone.sessionId = sessionId;
    clone.ipAddress = ipAddress;
    clone.error = error;
    clone.details = details != null ? new HashMap<>(details) : null;
    return clone;
}

context.getEvent().event.details.get("identity_provider_identity")将返回客户context.getEvent().event.details.get("identity_provider_identity")在使用的电子邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM