2

我正在 Keycloak 中构建一个自定义身份验证器,它不仅将用户登录到 keycloaks SSO-Instance,而且还通过 rest-Call 登录到遗留(SAP-)系统中。

现在我从这个遗留 API 收到错误消息,我想在通过我的自定义身份验证器登录时向最终用户显示消息(由 callee-param 国际化),但我不知道如何做到这一点。

我认为这与例如来自github 上的 spnego 身份验证器的以下几行有关:

...
else {
            context.getEvent().error(Errors.INVALID_USER_CREDENTIALS);
            context.failure(AuthenticationFlowError.INVALID_CREDENTIALS);
}
...

将 .error() 值更改为“测试!”时 这会被记录下来,但向用户显示的错误也是无效的凭据行。因此,AuthenticationFlowError 似乎是一个 ENUM,我认为它不适用于来自第三方的动态国际化消息。但是有没有办法告诉我的身份验证上下文从我的第三方系统返回错误消息?

非常感谢任何帮助

4

1 回答 1

4

因此,查看 keycloak-sources (opensource ftw!) 我刚刚发现了如何做到这一点:

而不是上面的行,人们可能会这样做:

 else {
   //errorresponse from restcall to thirdparty-api (just removing prefix here)
   responseString = responseString.replace("sap_error_", "");

   //actually I try to do a somewhat silent idplogin, so this might fit. 
   //you can change this error to the errormsg from thirdparty, too,
   // so its collected in the logs error=-part.
   context.getEvent().error(Errors.IDENTITY_PROVIDER_ERROR);

   //here we're building the actual responseHandler.
   //One might even want to set the status to status from thirdparty. 
   Response challengeResponse = context.form().setError(responseString)
                .createErrorPage(Response.Status.INTERNAL_SERVER_ERROR);

   //And in the end we apply the failureChallenge to the Context  
   context.failureChallenge(AuthenticationFlowError.IDENTITY_PROVIDER_ERROR,
                                     challengeResponse);
}

我对代码进行了注释以明确做了什么。

编辑:因为这个问题刚刚得到了赞成:请保重!.setError()-content 必须小于 255 个字符,否则会出现异常(DB-Field 太短 iirc,测试到 keycloak 7)。

最好的问候, 多米尼克

于 2018-06-05T10:10:44.747 回答