1

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

我使用下一个身份验证的调试器在终端中遇到的错误是:

[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`,
      })
4

1 回答 1

3

这是 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

于 2021-03-03T12:33:26.410 回答