Azure Api 管理:验证 JWT 和 Ocp-Apim-Subscription-Key 是否属于同一个用户?

Azure Api Management: verify that JWT and Ocp-Apim-Subscription-Key belong to the same user?

我的 api 受到 2 种方式的保护:JWT 验证和 Ocp-Apim-Subscription-Key 要求。

Azure AADB2C JWT 断言用户是他所说的人,同时保证他最近证明了这种身份(用户+密码)。

Ocp-Apim-Subscription-Key 证明 A 用户订阅了推荐的 api/product。

问题是:我如何断言所提供的 Ocp-Apim-Subscription-Key 属于 JWT 通知的用户?如果“黑客”能够从合作伙伴 A 窃取凭据并从合作伙伴 B 窃取 Ocp-Apim-Subscription-Key 怎么办?

编辑 1 - 这是我所做的:

  1. 创建了 2 个帐户 A 和 B
  2. 创建了一个 api 需要 OCP 密钥并验证 JWT
  3. 为 API
  4. 创建了一个产品 P
  5. 订阅 A 到 P
  6. 使用 B 的凭证获得了 JWT
  7. B“偷”了A的订阅密钥
  8. B 使用他自己的 JWT 和 A 的订阅密钥成功消费了产品 P

我认为你在混淆想法。客户端 A 和客户端 B 不应共享相同的 Ocp-Apim-订阅密钥。第一次验证应该是检查提供的用户名和密码是否正确。 (认证部分)。

假设是正确的,你需要检查授权部分。如果他们为分配给他们的产品提供正确的 Ocp-Apim-Subscription-Key 值。

例如

username | password | Product Subscription | Product Key
abc      | 123      | A                    | 62140881-0e72-4d99-b26c-802423a815f5
def      | 444      | B                    | 5f175bb2-f4ec-4302-ac27-56dab358b04b

假设用户 def 试图使用 62140881-0e72-4d99-b26c-802423a815f5 作为产品密钥,即使它是有效的,他们也应该收到 403 错误。

为用户使用订阅密钥时,context.User is populated which has details like the email address which you can use to validate against a similar claim present in the JWT token. You would use the choose policy to act accordingly if they match or don't. You can short circuit the request by using the return-response policy 在选择策略中。