@@ -14,9 +14,12 @@ import { getPermissions } from "../../db/permissions/getPermissions";
14
14
import { createToken } from "../../db/tokens/createToken" ;
15
15
import { getToken } from "../../db/tokens/getToken" ;
16
16
import { revokeToken } from "../../db/tokens/revokeToken" ;
17
+ import { WebhooksEventTypes } from "../../schema/webhooks" ;
18
+ import { getWebhookConfig } from "../../utils/cache/getWebhook" ;
17
19
import { env } from "../../utils/env" ;
18
20
import { logger } from "../../utils/logger" ;
19
21
import { Permission } from "../schemas/auth" ;
22
+ import { sendWebhookRequest } from "../utils/webhook" ;
20
23
21
24
export type TAuthData = never ;
22
25
export type TAuthSession = { permissions : string } ;
@@ -199,66 +202,115 @@ export const withAuth = async (server: FastifyInstance) => {
199
202
// If the secret key is being used, treat the user as the auth wallet
200
203
const config = await getConfiguration ( ) ;
201
204
const wallet = new LocalWallet ( ) ;
202
- await wallet . import ( {
203
- encryptedJson : config . authWalletEncryptedJson ,
204
- password : env . THIRDWEB_API_SECRET_KEY ,
205
- } ) ;
205
+
206
+ try {
207
+ await wallet . import ( {
208
+ encryptedJson : config . authWalletEncryptedJson ,
209
+ password : env . ENCRYPTION_PASSWORD ,
210
+ } ) ;
211
+ } catch {
212
+ // If that fails, we try to load the wallet with the secret key
213
+ await wallet . import ( {
214
+ encryptedJson : config . authWalletEncryptedJson ,
215
+ password : env . THIRDWEB_API_SECRET_KEY ,
216
+ } ) ;
217
+
218
+ // And then update the auth wallet to use encryption password instead
219
+ const encryptedJson = await wallet . export ( {
220
+ strategy : "encryptedJson" ,
221
+ password : env . ENCRYPTION_PASSWORD ,
222
+ } ) ;
223
+
224
+ logger . worker . info (
225
+ `[Encryption] Updating authWalletEncryptedJson to use ENCRYPTION_PASSWORD` ,
226
+ ) ;
227
+ await updateConfiguration ( {
228
+ authWalletEncryptedJson : encryptedJson ,
229
+ } ) ;
230
+ }
206
231
207
232
req . user = {
208
233
address : await wallet . getAddress ( ) ,
209
234
session : {
210
235
permissions : Permission . Admin ,
211
236
} ,
212
237
} ;
238
+
213
239
return ;
214
240
}
215
241
216
242
// Otherwise, check for an authenticated user
217
- const jwt = getJWT ( req ) ;
218
- if ( jwt ) {
219
- // 1. Check if the token is a valid engine JWT
220
- const token = await getToken ( { jwt } ) ;
221
-
222
- // First, we ensure that the token hasn't been revoked
223
- if ( token ?. revokedAt === null ) {
224
- // Then we perform our standard auth checks for the user
225
- const user = await getUser ( req ) ;
226
-
227
- // Ensure that the token user is an admin or owner
228
- if (
229
- ( user && user ?. session ?. permissions === Permission . Owner ) ||
230
- user ?. session ?. permissions === Permission . Admin
231
- ) {
232
- req . user = user ;
233
- return ;
243
+ try {
244
+ const jwt = getJWT ( req ) ;
245
+ if ( jwt ) {
246
+ // 1. Check if the token is a valid engine JWT
247
+ const token = await getToken ( { jwt } ) ;
248
+
249
+ // First, we ensure that the token hasn't been revoked
250
+ if ( token ?. revokedAt === null ) {
251
+ // Then we perform our standard auth checks for the user
252
+ const user = await getUser ( req ) ;
253
+
254
+ // Ensure that the token user is an admin or owner
255
+ if (
256
+ ( user && user ?. session ?. permissions === Permission . Owner ) ||
257
+ user ?. session ?. permissions === Permission . Admin
258
+ ) {
259
+ req . user = user ;
260
+ return ;
261
+ }
234
262
}
235
- }
236
263
237
- // 2. Otherwise, check if the token is a valid api-server JWT
238
- const user =
239
- ( await authWithApiServer ( jwt , "thirdweb.com" ) ) ||
240
- ( await authWithApiServer ( jwt , "thirdweb-preview.com" ) ) ;
241
-
242
- // If we have an api-server user, return it with the proper permissions
243
- if ( user ) {
244
- const res = await getPermissions ( { walletAddress : user . address } ) ;
245
-
246
- if (
247
- res ?. permissions === Permission . Owner ||
248
- res ?. permissions === Permission . Admin
249
- ) {
250
- req . user = {
251
- address : user . address ,
252
- session : {
253
- permissions : res . permissions ,
254
- } ,
255
- } ;
256
- return ;
264
+ // 2. Otherwise, check if the token is a valid api-server JWT
265
+ const user =
266
+ ( await authWithApiServer ( jwt , "thirdweb.com" ) ) ||
267
+ ( await authWithApiServer ( jwt , "thirdweb-preview.com" ) ) ;
268
+
269
+ // If we have an api-server user, return it with the proper permissions
270
+ if ( user ) {
271
+ const res = await getPermissions ( { walletAddress : user . address } ) ;
272
+
273
+ if (
274
+ res ?. permissions === Permission . Owner ||
275
+ res ?. permissions === Permission . Admin
276
+ ) {
277
+ req . user = {
278
+ address : user . address ,
279
+ session : {
280
+ permissions : res . permissions ,
281
+ } ,
282
+ } ;
283
+ return ;
284
+ }
257
285
}
258
286
}
287
+ } catch {
288
+ // no-op
289
+ }
290
+
291
+ const authWebhooks = await getWebhookConfig ( WebhooksEventTypes . AUTH ) ;
292
+ if ( authWebhooks ) {
293
+ const authResponses = await Promise . all (
294
+ authWebhooks . map ( ( webhook ) =>
295
+ sendWebhookRequest ( webhook , {
296
+ url : req . url ,
297
+ method : req . method ,
298
+ headers : req . headers ,
299
+ params : req . params ,
300
+ query : req . query ,
301
+ cookies : req . cookies ,
302
+ body : req . body ,
303
+ } ) ,
304
+ ) ,
305
+ ) ;
306
+
307
+ // If every auth webhook returns true, we allow the request
308
+ if ( authResponses . every ( ( ok ) => ! ! ok ) ) {
309
+ return ;
310
+ }
259
311
}
260
- } catch {
261
- // no-op
312
+ } catch ( err : any ) {
313
+ logger . server . error ( `[Auth] ${ err ?. message || err } ` ) ;
262
314
}
263
315
264
316
// If we have no secret key or authenticated user, return 401
0 commit comments