繁体   English   中英

azure 活动目录 access_token 验证失败

[英]azure active directory access_token validation fails

我使用的设置有一个前端(Vue3)和一个后端(python fastapi)组件。 前端使用以下代码从 AAD(不是 B2C - 该应用程序正在内部使用并且所有用户都是使用的租户的成员)获取 access_token:

const msalInstance = new PublicClientApplication(
  this.api.msalConfig,
);
const request = {scopes: ["openid", "profile"]}
await msalInstance.loginPopup(request);
const token_response = await msalInstance.acquireTokenPopup({})

msalConfig 如下所示:

msalConfig: {
        auth: {
          clientId: '9116d117-...-fdb12f53b040',
          authority:
            'https://login.microsoftonline.com/4e11c215-...-9d28f5bd3c11',
        },
        cache: {
          cacheLocation: 'localStorage',
        },
      }

该应用程序注册了单页重定向、Directory.ReadAll 和 User.Read 的 API 权限以及“需要分配:True”。

If I put the token I receive into jwt.ms, I get "This token was issued by Azure Active Directory and all data is there. If I put it into jwt.io, I also receive all data, but the Signature is invalid.

我尝试了几次验证令牌,最后坚持使用导致 InvalidSignatureError 的 azure_ad_verify_token.verify_jwt 库。 我假设我正在获取错误的令牌,但我不知道如何/在哪里获取可验证的版本。

这是我收到的令牌的 header:

{
  "typ": "JWT",
  "nonce": "5fp8kB_rTylKZjKF2nIDR7PT8HPkUkjtdl8PqDo4atY",
  "alg": "RS256",
  "x5t": "2ZQpJ3UpbjAYXYGaXEJl8lV0TOI",
  "kid": "2ZQpJ3UpbjAYXYGaXEJl8lV0TOI"
}

我很高兴有任何建议。

  1. 当我们使用 MSAL.js 时,请确保在调用您自己的 api 时使用 scopes 参数作为“ clientid/.default ”,并确保您选择了正确的签名算法(RS256)。(不是在图形 api 的情况下)。 例如: scopes:["<clientidhere>/.default"]

  2. 另请注意该令牌,如果您正在接收 Graph API 的令牌。 如果图形不需要并且它是用于您自己的 API 的令牌,您可能需要expose an API应用程序并授予 AAD 中的权限以同意。

在此处输入图像描述

请参阅: Azure AD 2.0 故障排除 – OAuth 架构指南 (authguidance.com)

  1. Please note that if token has nonce field in the JWT header, we may not need to validate Microsoft graph signature, as Microsoft API has to validate.If we do this jwt will fail standard signature based validation as it tries to add more security to the jwt 与随机数。
  2. 要验证此令牌,需要将 nonce 替换为 header 中 nonce 的 SHA2

参考:

  1. node.js - Azure AD 使用 Passport.js 的访问令牌签名无效 - Thinbug
  2. AzureAD 问题 | github

暂无
暂无

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

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