【问题标题】:passport-azure-ad unable to validate `id_token` due to wrong KID由于错误的 KID,passport-azure-ad 无法验证“id_token”
【发布时间】:2019-08-23 15:51:54
【问题描述】:

我在使用 passport-azure-ad 库时遇到问题,该库在尝试验证 id_token 时抛出错误。具体报错信息为"authentication failed due to: In _validateResponse: failed to generate PEM key due to: a key with kid %s cannot be found"

我可以看到id_token 标头中的kid 是一个未出现在密钥发现端点(格式为https://login.microsoftonline.com/{tenantId}/discovery/v2.0/keys)中的值。

有什么理由会发生这种情况吗?我想不通。

我的代码如下:

passport.use(
  new OIDCStrategy({
    clientID: CLIENT_ID,
    clientSecret: CLIENT_SECRET,
    identityMetadata: IDENTITY_METADATA_URL,
    redirectUrl: SUCCESS_REDIRECT_URI,
    responseMode: 'form_post',
    responseType: 'code',
    scope: 'email profile',

    loggingLevel: 'info',
    loggingNoPII: false
  })
)

app.get(
  '/oauthv2/login',
  passport.authenticate(
    'azuread-openidconnect',
    { failureRedirect: '/fail' },
    (req, res) => {
      // ...
    }
  )
)

app.post(
  '/oauthv2/success',
  passport.authenticate(
    'azuread-openidconnect',
    { failureRedirect: '/' },
    (req, res) => {
      // ...
    }
  )
)

pazzport-azure-ad日志中我可以看到在错误发生之前执行了以下步骤:

  • 收到 id_token
  • 收到 access_token
  • 收到 refresh_token
  • 令牌解码
  • 处理密钥
  • 处理密钥
  • 处理密钥
  • 身份验证失败,原因是:在 _validateResponse 中:无法生成 PEM 密钥,原因是:找不到孩子 %s 的密钥

【问题讨论】:

    标签: javascript node.js openid-connect passport-azure-ad


    【解决方案1】:

    所以,事实证明我在 Azure Active Directory 中创建了一个 v1 应用程序,虽然这在任何地方都不清楚,id_token 说它是ver: 2.0,但它不是......

    如果您想在 Azure AD 中创建 v2.0 应用,则无法从 Enterprise Applications 部分创建它,您必须使用 App registrations (Preview) 部分。

    这解决了我的问题,并且在我删除我的应用并正确重新创建后,id_token 包含一个有效的 KID。

    希望这对遇到同样问题的人有所帮助!

    有用的链接:https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app

    如果 Azure 中的任何人读到此内容,请知道在任何部分的标题中添加 (Preview) 意味着我不会自动单击它,因为它似乎并不重要。但是,在这种情况下,这是创建 v2.0 应用程序的唯一方法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 2021-12-13
      • 2017-03-31
      • 2017-12-28
      • 2017-08-25
      • 2018-07-25
      • 2022-08-23
      • 2021-06-01
      相关资源
      最近更新 更多