@@ -8,7 +8,7 @@ import { AsyncWallet } from "@thirdweb-dev/wallets/evm/wallets/async";
8
8
import { createHash } from "crypto" ;
9
9
import { FastifyInstance } from "fastify" ;
10
10
import { FastifyRequest } from "fastify/types/request" ;
11
- import jsonwebtoken from "jsonwebtoken" ;
11
+ import jsonwebtoken , { JwtPayload } from "jsonwebtoken" ;
12
12
import { validate as uuidValidate } from "uuid" ;
13
13
import { getPermissions } from "../../db/permissions/getPermissions" ;
14
14
import { createToken } from "../../db/tokens/createToken" ;
@@ -155,17 +155,22 @@ export const onRequest = async ({
155
155
156
156
const jwt = getJWT ( req ) ;
157
157
if ( jwt ) {
158
- const payload = jsonwebtoken . decode ( jwt , { json : true } ) ;
159
-
160
- // The `iss` field determines the auth type.
161
- if ( payload ?. iss ) {
162
- const authWallet = await getAuthWallet ( ) ;
163
- if ( payload . iss === ( await authWallet . getAddress ( ) ) ) {
164
- return await handleAccessToken ( jwt , req , getUser ) ;
165
- } else if ( payload . iss === THIRDWEB_DASHBOARD_ISSUER ) {
166
- return await handleDashboardAuth ( jwt ) ;
167
- } else {
168
- return await handleKeypairAuth ( jwt , req , payload . iss ) ;
158
+ const decoded = jsonwebtoken . decode ( jwt , { complete : true } ) ;
159
+ if ( decoded ) {
160
+ const payload = decoded . payload as JwtPayload ;
161
+ const header = decoded . header ;
162
+
163
+ // Get the public key from the `iss` payload field or `kid` header field.
164
+ const publicKey = payload . iss ?? header . kid ;
165
+ if ( publicKey ) {
166
+ const authWallet = await getAuthWallet ( ) ;
167
+ if ( publicKey === ( await authWallet . getAddress ( ) ) ) {
168
+ return await handleAccessToken ( jwt , req , getUser ) ;
169
+ } else if ( publicKey === THIRDWEB_DASHBOARD_ISSUER ) {
170
+ return await handleDashboardAuth ( jwt ) ;
171
+ } else {
172
+ return await handleKeypairAuth ( jwt , req , publicKey ) ;
173
+ }
169
174
}
170
175
}
171
176
}
@@ -263,13 +268,13 @@ const handleWebsocketAuth = async (
263
268
* matching the configured public key.
264
269
* @param jwt string
265
270
* @param req FastifyRequest
266
- * @param iss string
271
+ * @param publicKey string
267
272
* @returns AuthResponse
268
273
*/
269
274
const handleKeypairAuth = async (
270
275
jwt : string ,
271
276
req : FastifyRequest ,
272
- iss : string ,
277
+ publicKey : string ,
273
278
) : Promise < AuthResponse > => {
274
279
// The keypair auth feature must be explicitly enabled.
275
280
if ( ! env . ENABLE_KEYPAIR_AUTH ) {
@@ -278,8 +283,8 @@ const handleKeypairAuth = async (
278
283
279
284
let error : string | undefined ;
280
285
try {
281
- const keypair = await getKeypair ( { publicKey : iss } ) ;
282
- if ( ! keypair || keypair . publicKey !== iss ) {
286
+ const keypair = await getKeypair ( { publicKey } ) ;
287
+ if ( ! keypair ) {
283
288
error = "The provided public key is incorrect or not added to Engine." ;
284
289
throw error ;
285
290
}
0 commit comments