简体   繁体   English

OpenID Connect/Oauth2 - 缓存/使用令牌

[英]OpenID Connect/Oauth2 - cache/use tokens

I have a system with an REST API.我有一个带有 REST API 的系统。 The users of the REST API should be able to authenticate using OpenID Connect over Oauth2. REST API 的用户应该能够使用 OpenID Connect over Oauth2 进行身份验证。 The system itself has its own permission system.系统本身有自己的权限系统。 I need to map the IDPs user towards my local permission system.我需要将 IDP 用户映射到我的本地权限系统。 I'm thinking of saving the openid scope sub key in the local database as a unique user identifier.我正在考虑将本地数据库中的openid范围键保存为唯一的用户标识符。

But which token should I return to the user in the API.但是我应该在 API 中返回给用户哪个令牌。 Should it be the oauth2 access id or token_id from the OpenID Connect layer, or perhaps both?它应该是来自 OpenID Connect 层的 oauth2 访问 id 或 token_id,还是两者兼而有之?

So I see these alternatives for the end-user of the API:所以我看到了这些 API 最终用户的替代方案:

Authorization: Bearer <access_id>
Authorization: Bearer <token_id>
Authorization: Bearer <access_id>
TokenID: <token_id>

Without keeping the token_id/access_id in the system I need to get this from the client per API request.如果不将 token_id/access_id 保留在系统中,我需要根据 API 请求从客户端获取此信息。 The token ID contains the JWT claims (sub) so I need this to validate what the client can do in my system.令牌 ID 包含 JWT 声明(子),因此我需要它来验证客户端可以在我的系统中执行的操作。

Do I need to store any of the ID's below in my local database in order to validate API requests, or should the user provide it?我是否需要将以下任何 ID 存储在我的本地数据库中以验证 API 请求,还是应该由用户提供?

  • Refresh token?刷新令牌?
  • Access token?访问令牌?
  • Token ID?令牌标识?

I'm basically after authentication and user identification.我基本上是在认证和用户识别之后。 I'm not after getting access to any external resources other than this.我不是在访问除此之外的任何外部资源。

And last question.还有最后一个问题。 Do I need to validate the access token every request towards the IDP or can I cache it?我是否需要验证对 IDP 的每个请求的访问令牌,或者我可以缓存它吗? Do I need to validate both access token AND token id when using OpenID Connect?使用 OpenID Connect 时是否需要同时验证访问令牌和令牌 ID?

Edit:编辑:

My system itself has an REST API and its own permission system.我的系统本身有一个 REST API 和它自己的权限系统。 The permission system uses user IDs given by an any authentication system that uses OpenID Connect.权限系统使用由使用 OpenID Connect 的任何身份验证系统提供的用户 ID。 The first user is manually added to the permission system.第一个用户被手动添加到权限系统中。

Thought flow:思想流:

  1. User of my system requests https://mysystem/login我的系统用户请求https://mysystem/login
  2. User get redirected to https://idp/auth用户被重定向到https://idp/auth
  3. IDP redirects successful login to https://mysystem/callback My system get user id ( sub ) and email from token id JWT. IDP 将成功登录重定向到https://mysystem/callback我的系统从令牌 ID JWT 获取用户 ID ( sub ) 和电子邮件。
  4. And back to the user with a access token (not OIDC token id).并使用访问令牌(不是 OIDC 令牌 ID)返回给用户。
  5. The user can now perform: curl -H "Authorization: bearer access token" https://mysytem/some/resource用户现在可以执行: curl -H "Authorization: bearer access token" https://mysytem/some/resource
  6. Mysytem get this request from the user but don't know how to map access token to the user ID in the database w/o asking IDP for the user ID. Mysytem 从用户那里得到这个请求,但不知道如何将访问令牌映射到数据库中的用户 ID,而无需向 IDP 询问用户 ID。

I want to use an external IDP to let users login, not to get access to resources from 3rd party systems.我想使用外部 IDP 让用户登录,而不是从 3rd 方系统访问资源。

UPDATE更新

An access token DOES have a user id - see step 14 of this write up .访问令牌确实有用户 ID - 请参阅本文的第 14 步 Why do you think it doesn't?为什么你认为没有?

The one exception to this rule is when there is no user involved - for app to app communication (eg client credentials flow).此规则的一个例外是当没有用户参与时 - 用于应用程序到应用程序的通信(例如客户端凭据流)。 But you are not using that.但你没有使用它。

ORIGINAL ANSWER原答案

You should just send the full access (bearer) token from UIs to APIs - and you should not need to store token details in your database.您应该只将完整的访问(承载)令牌从 UI 发送到 API - 并且您不需要将令牌详细信息存储在您的数据库中。

When migrating to an OAuth architecture you will store user data in 2 places:迁移到 OAuth 架构时,您会将用户数据存储在 2 个位置:

  • Your normal product database - which often includes roles and user rights您的常规产品数据库 - 通常包括角色和用户权限
  • The Authorization Server - which generates a unique user id授权服务器 - 生成唯一的用户 ID

It is fairly common to store the unique user id in product data for correlation and tracking, as you suggest.正如您所建议的那样,将唯一用户 ID 存储在产品数据中以进行关联和跟踪是很常见的。 After login your APIs will often need to authorize based on product data.登录后,您的 API 通常需要根据产品数据进行授权。

A couple of my blog posts cover these topics in more detail:我的几篇博文更详细地介绍了这些主题:

My blog also links to sample code that ties these concepts together.我的博客还链接到将这些概念联系在一起的示例代码

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

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