Skip to content

Commit f6a58d0

Browse files
authored
Catch auth middleware errors (#287)
1 parent d4c2594 commit f6a58d0

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

src/server/middleware/auth.ts

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -182,69 +182,73 @@ export const withAuth = async (server: FastifyInstance) => {
182182
return;
183183
}
184184

185-
// If we have a valid secret key, skip authentication check
186-
const thirdwebApiSecretKey = req.headers.authorization?.split(" ")[1];
187-
if (thirdwebApiSecretKey === env.THIRDWEB_API_SECRET_KEY) {
188-
// If the secret key is being used, treat the user as the auth wallet
189-
const config = await getConfiguration();
190-
const wallet = new LocalWallet();
191-
await wallet.import({
192-
encryptedJson: config.authWalletEncryptedJson,
193-
password: env.THIRDWEB_API_SECRET_KEY,
194-
});
185+
try {
186+
// If we have a valid secret key, skip authentication check
187+
const thirdwebApiSecretKey = req.headers.authorization?.split(" ")[1];
188+
if (thirdwebApiSecretKey === env.THIRDWEB_API_SECRET_KEY) {
189+
// If the secret key is being used, treat the user as the auth wallet
190+
const config = await getConfiguration();
191+
const wallet = new LocalWallet();
192+
await wallet.import({
193+
encryptedJson: config.authWalletEncryptedJson,
194+
password: env.THIRDWEB_API_SECRET_KEY,
195+
});
195196

196-
req.user = {
197-
address: await wallet.getAddress(),
198-
session: {
199-
permissions: Permission.Admin,
200-
},
201-
};
202-
return;
203-
}
197+
req.user = {
198+
address: await wallet.getAddress(),
199+
session: {
200+
permissions: Permission.Admin,
201+
},
202+
};
203+
return;
204+
}
204205

205-
// Otherwise, check for an authenticated user
206-
const jwt = getJWT(req);
207-
if (jwt) {
208-
// 1. Check if the token is a valid engine JWT
209-
const token = await getToken({ jwt });
206+
// Otherwise, check for an authenticated user
207+
const jwt = getJWT(req);
208+
if (jwt) {
209+
// 1. Check if the token is a valid engine JWT
210+
const token = await getToken({ jwt });
210211

211-
// First, we ensure that the token hasn't been revoked
212-
if (token?.revokedAt === null) {
213-
// Then we perform our standard auth checks for the user
214-
const user = await getUser(req);
212+
// First, we ensure that the token hasn't been revoked
213+
if (token?.revokedAt === null) {
214+
// Then we perform our standard auth checks for the user
215+
const user = await getUser(req);
215216

216-
// Ensure that the token user is an admin or owner
217-
if (
218-
(user && user?.session?.permissions === Permission.Owner) ||
219-
user?.session?.permissions === Permission.Admin
220-
) {
221-
req.user = user;
222-
return;
217+
// Ensure that the token user is an admin or owner
218+
if (
219+
(user && user?.session?.permissions === Permission.Owner) ||
220+
user?.session?.permissions === Permission.Admin
221+
) {
222+
req.user = user;
223+
return;
224+
}
223225
}
224-
}
225226

226-
// 2. Otherwise, check if the token is a valid api-server JWT
227-
const user =
228-
(await authWithApiServer(jwt, "thirdweb.com")) ||
229-
(await authWithApiServer(jwt, "thirdweb-preview.com"));
227+
// 2. Otherwise, check if the token is a valid api-server JWT
228+
const user =
229+
(await authWithApiServer(jwt, "thirdweb.com")) ||
230+
(await authWithApiServer(jwt, "thirdweb-preview.com"));
230231

231-
// If we have an api-server user, return it with the proper permissions
232-
if (user) {
233-
const res = await getPermissions({ walletAddress: user.address });
232+
// If we have an api-server user, return it with the proper permissions
233+
if (user) {
234+
const res = await getPermissions({ walletAddress: user.address });
234235

235-
if (
236-
res?.permissions === Permission.Owner ||
237-
res?.permissions === Permission.Admin
238-
) {
239-
req.user = {
240-
address: user.address,
241-
session: {
242-
permissions: res.permissions,
243-
},
244-
};
245-
return;
236+
if (
237+
res?.permissions === Permission.Owner ||
238+
res?.permissions === Permission.Admin
239+
) {
240+
req.user = {
241+
address: user.address,
242+
session: {
243+
permissions: res.permissions,
244+
},
245+
};
246+
return;
247+
}
246248
}
247249
}
250+
} catch {
251+
// no-op
248252
}
249253

250254
// If we have no secret key or authenticated user, return 401

0 commit comments

Comments
 (0)