如何解决将 Okta 身份验证与 Next Auth 结合使用时出错
我在使用 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,"errorId":"**************","errorCauses":[]}'
} okta ************************
[next-auth][error][oauth_callback_error]
https://next-auth.js.org/errors#oauth_callback_error {
statusCode: 401,"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-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 标头使 idp 集成工作。
同样值得注意的是; https://developer.okta.com/docs/reference/api/oidc/#client-secret
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。