File tree Expand file tree Collapse file tree 8 files changed +51
-102
lines changed Expand file tree Collapse file tree 8 files changed +51
-102
lines changed Original file line number Diff line number Diff line change 98
98
"get-tsconfig" : " ^4.7.2" ,
99
99
"http-status" : " 1.6.2" ,
100
100
"image-size" : " ^1.1.1" ,
101
+ "jose" : " 5.9.2" ,
101
102
"json-schema-to-typescript" : " 15.0.1" ,
102
- "jsonwebtoken" : " 9.0.2" ,
103
103
"minimist" : " 1.2.8" ,
104
104
"pino" : " 9.5.0" ,
105
105
"pino-pretty" : " 11.3.0" ,
114
114
"@hyrious/esbuild-plugin-commonjs" : " ^0.2.4" ,
115
115
"@payloadcms/eslint-config" : " workspace:*" ,
116
116
"@types/json-schema" : " 7.0.15" ,
117
- "@types/jsonwebtoken" : " 8.5.9" ,
118
117
"@types/minimist" : " 1.2.2" ,
119
118
"@types/nodemailer" : " 6.4.14" ,
120
119
"@types/pluralize" : " 0.0.33" ,
Original file line number Diff line number Diff line change
1
+ import { SignJWT } from 'jose'
2
+
3
+ export const jwtSign = async ( {
4
+ fieldsToSign,
5
+ secret,
6
+ tokenExpiration,
7
+ } : {
8
+ fieldsToSign : Record < string , unknown >
9
+ secret : string
10
+ tokenExpiration : number
11
+ } ) => {
12
+ const secretKey = new TextEncoder ( ) . encode ( secret )
13
+ const issuedAt = Math . floor ( Date . now ( ) / 1000 )
14
+ const exp = issuedAt + tokenExpiration
15
+ const token = await new SignJWT ( fieldsToSign )
16
+ . setProtectedHeader ( { alg : 'HS256' , typ : 'JWT' } )
17
+ . setIssuedAt ( issuedAt )
18
+ . setExpirationTime ( exp )
19
+ . sign ( secretKey )
20
+ return { exp, token }
21
+ }
Original file line number Diff line number Diff line change 1
- import jwt from 'jsonwebtoken'
2
-
3
1
import type {
4
2
AuthOperationsFromCollectionSlug ,
5
3
Collection ,
@@ -16,6 +14,7 @@ import { killTransaction } from '../../utilities/killTransaction.js'
16
14
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields.js'
17
15
import { getFieldsToSign } from '../getFieldsToSign.js'
18
16
import isLocked from '../isLocked.js'
17
+ import { jwtSign } from '../jwt.js'
19
18
import { authenticateLocalStrategy } from '../strategies/local/authenticate.js'
20
19
import { incrementLoginAttempts } from '../strategies/local/incrementLoginAttempts.js'
21
20
import { resetLoginAttempts } from '../strategies/local/resetLoginAttempts.js'
@@ -234,8 +233,10 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
234
233
} ) ) || user
235
234
} , Promise . resolve ( ) )
236
235
237
- const token = jwt . sign ( fieldsToSign , secret , {
238
- expiresIn : collectionConfig . auth . tokenExpiration ,
236
+ const { exp, token } = await jwtSign ( {
237
+ fieldsToSign,
238
+ secret,
239
+ tokenExpiration : collectionConfig . auth . tokenExpiration ,
239
240
} )
240
241
241
242
req . user = user
@@ -308,7 +309,7 @@ export const loginOperation = async <TSlug extends CollectionSlug>(
308
309
} , Promise . resolve ( ) )
309
310
310
311
let result : { user : DataFromCollectionSlug < TSlug > } & Result = {
311
- exp : ( jwt . decode ( token ) as jwt . JwtPayload ) . exp ,
312
+ exp,
312
313
token,
313
314
user,
314
315
}
Original file line number Diff line number Diff line change 1
- import jwt from 'jsonwebtoken '
1
+ import { decodeJwt } from 'jose '
2
2
3
3
import type { Collection } from '../../collections/config/types.js'
4
4
import type { PayloadRequest } from '../../types/index.js'
@@ -70,7 +70,7 @@ export const meOperation = async (args: Arguments): Promise<MeOperationResult> =
70
70
result . user = user
71
71
72
72
if ( currentToken ) {
73
- const decoded = jwt . decode ( currentToken ) as jwt . JwtPayload
73
+ const decoded = decodeJwt ( currentToken )
74
74
if ( decoded ) {
75
75
result . exp = decoded . exp
76
76
}
Original file line number Diff line number Diff line change 1
- import jwt from 'jsonwebtoken'
2
1
import url from 'url'
3
2
4
3
import type { BeforeOperationHook , Collection } from '../../collections/config/types.js'
@@ -10,6 +9,7 @@ import { commitTransaction } from '../../utilities/commitTransaction.js'
10
9
import { initTransaction } from '../../utilities/initTransaction.js'
11
10
import { killTransaction } from '../../utilities/killTransaction.js'
12
11
import { getFieldsToSign } from '../getFieldsToSign.js'
12
+ import { jwtSign } from '../jwt.js'
13
13
14
14
export type Result = {
15
15
exp : number
@@ -102,12 +102,12 @@ export const refreshOperation = async (incomingArgs: Arguments): Promise<Result>
102
102
user : args ?. req ?. user ,
103
103
} )
104
104
105
- const refreshedToken = jwt . sign ( fieldsToSign , secret , {
106
- expiresIn : collectionConfig . auth . tokenExpiration ,
105
+ const { exp, token : refreshedToken } = await jwtSign ( {
106
+ fieldsToSign,
107
+ secret,
108
+ tokenExpiration : collectionConfig . auth . tokenExpiration ,
107
109
} )
108
110
109
- const exp = ( jwt . decode ( refreshedToken ) as Record < string , unknown > ) . exp as number
110
-
111
111
result = {
112
112
exp,
113
113
refreshedToken,
Original file line number Diff line number Diff line change 1
1
import httpStatus from 'http-status'
2
- import jwt from 'jsonwebtoken'
3
2
4
3
import type { Collection } from '../../collections/config/types.js'
5
4
import type { PayloadRequest } from '../../types/index.js'
@@ -9,6 +8,7 @@ import { commitTransaction } from '../../utilities/commitTransaction.js'
9
8
import { initTransaction } from '../../utilities/initTransaction.js'
10
9
import { killTransaction } from '../../utilities/killTransaction.js'
11
10
import { getFieldsToSign } from '../getFieldsToSign.js'
11
+ import { jwtSign } from '../jwt.js'
12
12
import { authenticateLocalStrategy } from '../strategies/local/authenticate.js'
13
13
import { generatePasswordSaltHash } from '../strategies/local/generatePasswordSaltHash.js'
14
14
@@ -118,8 +118,10 @@ export const resetPasswordOperation = async (args: Arguments): Promise<Result> =
118
118
user,
119
119
} )
120
120
121
- const token = jwt . sign ( fieldsToSign , secret , {
122
- expiresIn : collectionConfig . auth . tokenExpiration ,
121
+ const { token } = await jwtSign ( {
122
+ fieldsToSign,
123
+ secret,
124
+ tokenExpiration : collectionConfig . auth . tokenExpiration ,
123
125
} )
124
126
125
127
const fullUser = await payload . findByID ( {
Original file line number Diff line number Diff line change 1
- import jwt from 'jsonwebtoken '
1
+ import { jwtVerify } from 'jose '
2
2
3
3
import type { Payload , Where } from '../../types/index.js'
4
4
import type { AuthStrategyFunction , User } from '../index.js'
@@ -81,8 +81,8 @@ export const JWTAuthentication: AuthStrategyFunction = async ({
81
81
return { user : null }
82
82
}
83
83
84
- const decodedPayload = jwt . verify ( token , payload . secret ) as jwt . JwtPayload & JWTToken
85
-
84
+ const secretKey = new TextEncoder ( ) . encode ( payload . secret )
85
+ const { payload : decodedPayload } = await jwtVerify < JWTToken > ( token , secretKey )
86
86
const collection = payload . collections [ decodedPayload . collection ]
87
87
88
88
const user = await payload . findByID ( {
You can’t perform that action at this time.
0 commit comments