颁发“API 密钥"使用密钥斗篷 [英] Issuing "API keys" using Keycloak

查看:34
本文介绍了颁发“API 密钥"使用密钥斗篷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置包含三个组件:

My setup has three components:

  • 后端应用程序(Python/Flask)
  • 前端应用程序 (VueJS)
  • 钥匙斗篷

前端将使用 Keycloak 让用户登录并使用访问令牌来验证对后端的请求.到现在为止还挺好.

The frontend will use Keycloak to let users sign in and use the access tokens to authenticate requests to the backend. So far so good.

现在我希望第三方应用程序能够针对后端发出经过身份验证的请求,我想知道如何使用 Keycloak 实现这一点?我的想法是为每个客户发布一组新的凭据.然后,他们的应用程序与 Keycloak 对话以获取访问令牌.然后我可以使用 Keycloak 来管理 API 的所有用户的访问控制.

Now I want third party applications to be able to make authenticated requests against the backend and I am wondering how that can be realized using Keycloak? My idea is to issue a new set of credentials for each customer. Their application then talks to Keycloak to get access tokens. I can then use Keycloak to manage access control for all users of the API.

  • 如何在 Keycloak - 客户端中表示第 3 方应用程序?用户?...?
  • 是否有针对此类用例的最佳实践?

推荐答案

我终于找到了一个运行良好的解决方案,它似乎是向外部应用程序颁发凭据的Keycloak 方式".要创建一组新的凭据,请添加一个新的 Keycloak 客户端并更改以下设置:

I finally found a solution that works well and seems to be "the Keycloak way" to issue credentials to external applications. To create a new set of credentials, add a new Keycloak client and change the following settings:

  • 启用标准流:关闭
  • 直接访问授权已启用:关闭
  • 访问类型:机密
  • 服务帐户已启用:开启

外部应用程序将使用我们新创建的客户端名称作为 client_id.client_secret 是自动生成的,可以在 Credentials 标签下找到.

The external application will use our newly created client's name as the client_id. The client_secret was generated automatically and can be found under the Credentials tab.

如果您的受 Keycloak 保护的服务被配置为检查传入的 Bearer 令牌的 aud 声明,则需要第二步.默认情况下,Keycloak 向您的客户端发出的 JWT 令牌的受众将设置为您的客户的名称,因此它们将被您的服务拒绝.您可以使用 Client Scopes 来修改该行为:

If your Keycloak-protected services are configured to check the aud claim of incoming Bearer tokens, a second step is necessary. By default, the audience of the JWT tokens that Keycloak issues to your client will be set to your client's name, so they will be rejected by your services. You can use Client Scopes to modify that behavior:

  1. 创建新的客户端范围
  2. 选择受众模板"
  3. 选择您要授予外部应用访问权限的服务,然后点击下一步"
  4. 将范围添加到您刚刚创建的客户端(Client Scopes 选项卡)

Keycloak 现在会将您的服务名称添加到它向您的新客户端颁发的所有 JWT 令牌的 aud 声明中.查看关于服务帐户的 Keycloak 文档了解更多详情.

Keycloak will now add your service's name to the aud claim of all JWT tokens it issues to your new client. Check out the Keycloak documentation on Service Accounts for more details.

外部应用程序现在可以使用其凭据从 Keycloak 的令牌端点获取访问令牌:

An external application can now use its credentials to obtain an access token from Keycloak's token endpoint:

POST {keycloak-url}/auth/realms/atlas/protocol/openid-connect/token

  • Content-Type 标头设置为 application/x-www-form-urlencoded
  • 使用基本身份验证对请求进行身份验证,使用您的客户端 ID 作为用户,使用您的客户端密钥 作为密码
  • 在请求正文中设置grant_type=client_credentials
  • Set the Content-Type header to application/x-www-form-urlencoded
  • Authenticate the request with Basic Authentication, using your client id as the user and your client secret as the password
  • Set grant_type=client_credentials in the request body

这篇关于颁发“API 密钥"使用密钥斗篷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆