NextAuth.js:JWT 秘密破解应用程序

Posted

技术标签:

【中文标题】NextAuth.js:JWT 秘密破解应用程序【英文标题】:NextAuth.js: JWT secret breaks application 【发布时间】:2022-01-07 11:31:48 【问题描述】:

[我正在使用 Next.js (11.1.2) + NextAuth (4.0.0-beta.7) 登录 Strapi API,仅使用凭据提供程序 (JWT)。]

整个身份验证流程都在使用这个[...nextauth].js

import NextAuth from "next-auth"
import CredentialsProvider from 'next-auth/providers/credentials'

export default NextAuth(
  providers: [
    CredentialsProvider(
      name: 'AppName',
      credentials: 
        email: label: "Email", type: "text", placeholder: "daveglow@foomail.com",
        password:   label: "Password", type: "password" ,
      ,
      async authorize(credentials, req) 
        const res = await fetch(process.env.CREDENTIALS_AUTH_URL, 
          method: 'POST',
          body: JSON.stringify(credentials),
          headers:  "Content-Type": "application/json" 
        )
        const user = await res.json()

        if (res.ok && user) 
          return user
        
        return null
      
    )
  ],
  session: 
    strategy: "jwt",
    maxAge: 30 * 24 * 60 * 60 // 30 days
  ,
  pages: 
    signIn: '/signin',
    signOut: '/signin',
    error: '/signin'
  ,
)

但在用户登录几秒钟后,终端显示此消息并丢弃会话:

[next-auth][warn][NO_SECRET] https://next-auth.js.org/warnings#no_secret
[next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error decryption operation failed 
  message: 'decryption operation failed',
  stack: 'JWEDecryptionFailed: decryption operation failed\n'

所以,我尝试添加:

secret: process.env.SECRET, //I've created using $ openssl rand -base64 32

然后我收到两条不同的消息

浏览器控制台

[next-auth][error][CLIENT_FETCH_ERROR] 
https://next-auth.js.org/errors#client_fetch_error

VS 代码终端

[next-auth][error][CALLBACK_CREDENTIALS_JWT_ERROR] 
https://next-auth.js.org/errors#callback_credentials_jwt_error Signin in with credentials only supported if JWT strategy is enabled UnsupportedStrategy [UnsupportedStrategyError]: Signin in with credentials only supported if JWT strategy is enabled

我尝试了几个不同的选项,但结果很混乱。 而现在,我不知道该怎么办。 :( 你能帮帮我吗?

【问题讨论】:

重新部署应用后是否遇到问题? 不,我现在不能部署,我只是在本地运行。 【参考方案1】:

我升级到了 4.0.1 版。修复了问题。

【讨论】:

【参考方案2】:

这可能是在 beta 7 版本的 next-auth 中引入的错误,如 https://github.com/nextauthjs/next-auth/issues/3216 中所述

【讨论】:

太棒了!谢谢唐纳德【参考方案3】:

我不是专家。但我认为这是库 NEXT auth 4.0.0 的当前问题

我可以使用“next-auth”版本解决问题:“^3.25.0”。 并关注了这个tutorial

【讨论】:

以上是关于NextAuth.js:JWT 秘密破解应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何将 api 错误返回到 NextAuth.js 中的登录组件

在 Apollo Server 上下文中获取 NextAuth.js 用户会话

如何在 python 3.7 中编辑 JWT 标头?

NextAuth 与自定义凭据提供程序不创建会话

NestJS JWT 策略需要一个秘密或密钥

在 JWT 中存储秘密信息,使用 Node.js - Express 后端