【问题标题】:Error when using Okta Authentication with Next Auth将 Okta 身份验证与 Next Auth 一起使用时出错
【发布时间】:2021-05-08 05:54:11
【问题描述】:

我在使用 next-auth 和 Okta 作为提供者时遇到了错误。它会将我重定向回我的应用,但我看到一个页面显示“尝试使用另一个帐户登录”并重定向到“api/auth/signin?error=Callback”

我在终端使用 next auth 的调试器得到的错误是:

[next-auth][debug][oauth_callback_protection] Comparing received and expected state {
  state: 'b3ef7bf3d4a5aa8f5f81fc95260502b0a206180bd0a831bb27b26d8c21271e33',
  expectedState: 'b3ef7bf3d4a5aa8f5f81fc95260502b0a206180bd0a831bb27b26d8c21271e33'
}

[next-auth][error][oauth_get_access_token_error] 
https://next-auth.js.org/errors#oauth_get_access_token_error {
  statusCode: 401,
  data: '{"errorCode":"invalid_client","errorSummary":"No client credentials found.","errorLink":"invalid_client","errorId":"******************","errorCauses":[]}'
} undefined undefined

[next-auth][error][oauth_get_access_token_error] 
https://next-auth.js.org/errors#oauth_get_access_token_error {
  statusCode: 401,
  data: '{"errorCode":"invalid_client","errorSummary":"No client credentials found.","errorLink":"invalid_client","errorId":"**************","errorCauses":[]}'
} okta ************************

[next-auth][error][oauth_callback_error] 
https://next-auth.js.org/errors#oauth_callback_error {
  statusCode: 401,
  data: '{"errorCode":"invalid_client","errorSummary":"No client credentials found.","errorLink":"invalid_client","errorId":"*******************","errorCauses":[]}'
}

这是我的 Okta 应用设置:

我检查了 cliendID 和客户端密码,它们是正确的。有没有人有任何线索? 我知道它可以与 okta 一起使用,因为我已经让它与另一个应用程序一起使用,但我尝试复制完全相同的设置。

在 [...nextauth].js 中:

      Providers.Okta({
          clientId: process.env.OKTA_CLIENT_ID,
          clientSecret: process.env.OKTA_CLIENT_SECRET,
          domain: process.env.OKTA_DOMAIN,
          accessTokenUrl: `https://${process.env.OKTA_DOMAIN}/oauth2/default/v1/token`,
          authorizationUrl: `https://${process.env.OKTA_DOMAIN}/oauth2/default/v1/authorize/?response_type=code`,
      })

【问题讨论】:

    标签: next.js okta next-auth


    【解决方案1】:

    这是 next-auth 包中的一个错误。遇到了同样的问题,在与同事调试并尝试了几件事后,我们找到了适合我们的补丁。

    diff --git a/node_modules/next-auth/dist/server/lib/oauth/client.js b/node_modules/next-auth/dist/server/lib/oauth/client.js
    index b4e48c2..7f68dd7 100644
    --- a/node_modules/next-auth/dist/server/lib/oauth/client.js
    +++ b/node_modules/next-auth/dist/server/lib/oauth/client.js
    @@ -160,7 +160,7 @@ function _getOAuth2AccessToken() {
           headers.Authorization = 'Basic ' + Buffer.from(provider.clientId + ':' + provider.clientSecret).toString('base64');
         }
     
    -    if ((provider.id === 'okta' || provider.id === 'identity-server4') && !headers.Authorization) {
    +    if ((provider.id === 'identity-server4') && !headers.Authorization) {
           headers.Authorization = "Bearer ".concat(code);
         }
    

    问题似乎是同时发送 client_id 和 secret 作为查询参数但还发送 Authorization 标头的问题。删除 Authorization 标头使 idp 集成工作。

    同样值得注意的是; https://developer.okta.com/docs/reference/api/oidc/#client-secret

    【讨论】:

    • 我在 Google 上遇到了这个问题,认为这个解决方案适用于所有提供商还是只适用于 okta?
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2012-09-26
    • 2022-09-29
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多