Skip to content

Commit 0c90424

Browse files
authored
chore: support passing the publicKey 'iss' in the 'kid' JWT header (#553)
1 parent b5fb6a7 commit 0c90424

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/server/middleware/auth.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AsyncWallet } from "@thirdweb-dev/wallets/evm/wallets/async";
88
import { createHash } from "crypto";
99
import { FastifyInstance } from "fastify";
1010
import { FastifyRequest } from "fastify/types/request";
11-
import jsonwebtoken from "jsonwebtoken";
11+
import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
1212
import { validate as uuidValidate } from "uuid";
1313
import { getPermissions } from "../../db/permissions/getPermissions";
1414
import { createToken } from "../../db/tokens/createToken";
@@ -155,17 +155,22 @@ export const onRequest = async ({
155155

156156
const jwt = getJWT(req);
157157
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+
}
169174
}
170175
}
171176
}
@@ -263,13 +268,13 @@ const handleWebsocketAuth = async (
263268
* matching the configured public key.
264269
* @param jwt string
265270
* @param req FastifyRequest
266-
* @param iss string
271+
* @param publicKey string
267272
* @returns AuthResponse
268273
*/
269274
const handleKeypairAuth = async (
270275
jwt: string,
271276
req: FastifyRequest,
272-
iss: string,
277+
publicKey: string,
273278
): Promise<AuthResponse> => {
274279
// The keypair auth feature must be explicitly enabled.
275280
if (!env.ENABLE_KEYPAIR_AUTH) {
@@ -278,8 +283,8 @@ const handleKeypairAuth = async (
278283

279284
let error: string | undefined;
280285
try {
281-
const keypair = await getKeypair({ publicKey: iss });
282-
if (!keypair || keypair.publicKey !== iss) {
286+
const keypair = await getKeypair({ publicKey });
287+
if (!keypair) {
283288
error = "The provided public key is incorrect or not added to Engine.";
284289
throw error;
285290
}

0 commit comments

Comments
 (0)