简体   繁体   English

创建自定义身份提供者并使用 keycloak 对其进行配置

[英]Create a custom identity provider and configure it with keycloak

I am working on a project where I need to create an application that shall act as an OIDC mediator between a client which only supports OIDC for authentication and a REST api.我正在做一个项目,我需要创建一个应用程序,该应用程序应充当仅支持 OIDC 进行身份验证的客户端和 REST api 之间的 OIDC 调解器。 The REST api is able to generate tokens and give user info but does not support OIDC. REST api 能够生成令牌并提供用户信息,但不支持 OIDC。

To achieve this I am thinking of using keycloak to handle the OIDC communication with the client and implement my own java application that keycloak can trigger to realize the authorization, token and userinfo endpoint (sort of a custom ownmade identity provider) handling the communication with the rest api.为了实现这一点,我正在考虑使用 keycloak 来处理与客户端的 OIDC 通信并实现我自己的 java 应用程序,keycloak 可以触发该应用程序以实现授权、令牌和 userinfo 端点(一种自定义的自制身份提供者)处理与客户端的通信rest api。

I have created a realm in keycloak and configured the realm to use an Identity Provider Redirector with an Identity Provider I added in keycloak (user-defined OpenID Connect v1.0).我在 keycloak 中创建了一个 ZB94B7EF7F7F17D2394D6FBDF458DADC7B0Z 并将 realm 配置为使用身份提供者重定向器和我在 keycloak 中添加的身份提供者(用户定义的 OpenID Connect v1.0)。 In the identity provider configuration I have set all the URLs to point to my java application but the initial OIDC authorization call from the client just redirects to the redirect_uri with a #error=login_required without any of my endpoints in the java application beeing triggered.在身份提供程序配置中,我已将所有 URL 设置为指向我的 java 应用程序,但来自客户端的初始 OIDC 授权调用只是重定向到 redirect_uri 并带有 #error=login_required ,而我在 java 应用程序中的任何端点都没有被触发。

I guess there is something I have missed.. I need to intervene the authorization flow so that I can pick up a query param from the authorization request that needs to be handled in my java application.我想我错过了一些东西。我需要干预授权流程,以便我可以从需要在我的 java 应用程序中处理的授权请求中获取查询参数。 I also need to map the token from the rest api into the token request (when this request comes from the backend of the client app), and finally map the userinfo object as a response to the userinfo request. I also need to map the token from the rest api into the token request (when this request comes from the backend of the client app), and finally map the userinfo object as a response to the userinfo request.

I really hope someone have time to point me in the right direction.我真的希望有人有时间为我指明正确的方向。 Thank you so much in advance.非常感谢你。

Edit: I have added a sequence diagram to explain it better:编辑:我添加了一个序列图来更好地解释它: 在此处输入图像描述

I need to intercept the authorization request call to pick up a custom query param (endUserString) that identifies the user.我需要拦截授权请求调用以获取标识用户的自定义查询参数 (endUserString)。 There will be no user login form.将没有用户登录表单。 I need the param in my code that uses this towards the REST API.我需要在我的代码中使用这个参数来处理 REST API。 Both the token and the userinfo must be received from my APP and not from keycloak itself.令牌和用户信息都必须从我的 APP 而不是从 keycloak 本身接收。

The Java Mediator may ask for a token in advance (A) and use this to access the Rest API (using a predefined clientId and clientsecret). Java 调解器可能会提前请求令牌 (A) 并使用它来访问 Rest API(使用预定义的 clientId 和 clientsecret)。 Alternatively this token may be fetched for each method.或者,可以为每个方法获取此令牌。 To token must be used to retrieve customer info from the REST API (B). To token 必须用于从 REST API (B) 中检索客户信息。 I want to wrap this with OIDC support without any login form.我想在没有任何登录表单的情况下使用 OIDC 支持来包装它。 A browser will just redirect to the authorization flow with the endUserString identifying the end user.浏览器只会重定向到授权流程,其中 endUserString 标识最终用户。 The customer info will be returned from the java mediator into keycloak responding this in the GetUserInfoRsp.客户信息将从 java 中介返回到 keycloak,在 GetUserInfoRsp 中对此作出响应。

I think there might be a simpler solution than what you envisioned: implementing your own custom authenticator for Keycloak.我认为可能有一个比您想象的更简单的解决方案:为 Keycloak 实现您自己的自定义身份验证器。

Keycloak has a notion of authentication flow which is a tree of authenticators than are provided by Keycloak or custom made. Keycloak 有一个身份验证流程的概念,它是一个身份验证器树,而不是 Keycloak 提供的或定制的。 Each authenticator can be called to try to authenticate the user.可以调用每个身份验证器来尝试对用户进行身份验证。

The most common one is the Username/Password Form which displays a login page to the user and authenticates the user if the provided credentials are valid.最常见的一种是用户名/密码表单,它向用户显示登录页面,并在提供的凭据有效时对用户进行身份验证。 But you could imagine any type of authenticator such as an SMS authenticator or a magic link one .但是您可以想象任何类型的身份验证器,例如SMS 身份验证器或魔术链接身份验证器。

You can find the existing Keycloak's authenticators on their repo and the documentation on how to create your own here .你可以在他们的 repo 上找到现有的 Keycloak 的身份验证器,以及如何在此处创建自己的文档。

In your case, you would need to implement your own logic where your authenticator would get the endUserString param from the request and call the REST API to validate the user's identity.在您的情况下,您需要实现自己的逻辑,您的身份验证器将从请求中获取endUserString参数并调用 REST API 来验证用户的身份。 You could fetch the REST API token at initialisation or for each request.您可以在初始化时或为每个请求获取 REST API 令牌。 You could also modify the user stored in Keycloak with data coming from the REST API's user info endpoint (common OIDC attributes or custom attributes).您还可以使用来自 REST API 的用户信息端点(通用 OIDC 属性或自定义属性)的数据修改存储在 Keycloak 中的用户。

Please note that the dev team announced Keycloak X , a sort of reboot of the project which will probably bring breaking changes to their APIs.请注意,开发团队宣布了 Keycloak X ,这是项目的一种重启,可能会给他们的 API 带来重大变化。

Also, please consider all the security impacts of your design as, from what you provided, it seems the authentication of a user will only rely on a simple query parameter which, if it doesn't change over time for example, feels like a big security hole.另外,请考虑您设计的所有安全影响,因为从您提供的内容来看,用户的身份验证似乎只依赖于一个简单的查询参数,例如,如果它不随时间变化,感觉就像一个很大的安全漏洞。

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

相关问题 使用 Keycloak 作为身份代理和 Okta 作为身份提供程序的 Saml 设置不起作用 - Saml setup with Keycloak as Identity Broker and Okta as Identity provider not working 如何配置java app充当身份提供者 - how to configure java app to act as Identity Provider 尝试为 Keycloak 创建自定义身份验证器提供程序时部署来自 Maven 的 NoClassDefFoundError - NoClassDefFoundError from Maven deploy when attempting to create a custom authenticator provider for Keycloak 使用外部身份提供者 (IDP) 通过 KeyCloak 授予直接访问权限 - Direct Access Grant with KeyCloak using external Identity Provider (IDP) Spring Security 和 Keycloak 因自定义身份验证提供程序而失败 - Spring Security and Keycloak fails with a custom authentication provider Keycloak 主题选择器提供程序 - 创建未调用 - Keycloak Theme Selector Provider - Create not called JAVA 使用自定义密钥斗篷的用户存储提供程序登录到 Mysql - JAVA Login to Mysql with User Storage Provider of custom keycloak Keycloak:在身份代理时不要创建本地用户 - Keycloak: Do not create local user while identity brokering 创建 keycloak 端点以提供自定义身份验证 - create keycloak endpoint to provide custom authentication Keycloak Identity Broker API - Keycloak Identity Broker API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM