[英]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.