Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Dec 10, 2024
1 parent e6c4082 commit f2f4716
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions src/server/middleware/adminRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,71 +22,71 @@ const ADMIN_ROUTES_PASSWORD = env.THIRDWEB_API_SECRET_KEY;

// Add queues to monitor here.
const QUEUES: Queue[] = [
SendWebhookQueue.q,
ProcessEventsLogQueue.q,
ProcessTransactionReceiptsQueue.q,
SendTransactionQueue.q,
MineTransactionQueue.q,
CancelRecycledNoncesQueue.q,
PruneTransactionsQueue.q,
NonceResyncQueue.q,
NonceHealthCheckQueue.q,
SendWebhookQueue.q,
ProcessEventsLogQueue.q,
ProcessTransactionReceiptsQueue.q,
SendTransactionQueue.q,
MineTransactionQueue.q,
CancelRecycledNoncesQueue.q,
PruneTransactionsQueue.q,
NonceResyncQueue.q,
NonceHealthCheckQueue.q,
];

export const withAdminRoutes = async (fastify: FastifyInstance) => {
fastify.after(async () => {
// Create a new route for Bullboard routes.
const serverAdapter = new FastifyAdapter();
serverAdapter.setBasePath(ADMIN_QUEUES_BASEPATH);
fastify.after(async () => {
// Create a new route for Bullboard routes.
const serverAdapter = new FastifyAdapter();
serverAdapter.setBasePath(ADMIN_QUEUES_BASEPATH);

createBullBoard({
queues: QUEUES.map((q) => new BullMQAdapter(q)),
serverAdapter,
});
createBullBoard({
queues: QUEUES.map((q) => new BullMQAdapter(q)),
serverAdapter,
});

await fastify.register(serverAdapter.registerPlugin(), {
basePath: ADMIN_QUEUES_BASEPATH,
prefix: ADMIN_QUEUES_BASEPATH,
});
await fastify.register(serverAdapter.registerPlugin(), {
basePath: ADMIN_QUEUES_BASEPATH,
prefix: ADMIN_QUEUES_BASEPATH,
});

fastify.addHook("onRequest", async (req, reply) => {
if (req.url.startsWith(ADMIN_QUEUES_BASEPATH)) {
const authHeader = req.headers.authorization;
fastify.addHook("onRequest", async (req, reply) => {
if (req.url.startsWith(ADMIN_QUEUES_BASEPATH)) {
const authHeader = req.headers.authorization;

if (!authHeader || !authHeader.startsWith("Basic ")) {
reply
.status(StatusCodes.UNAUTHORIZED)
.header("WWW-Authenticate", 'Basic realm="Admin Routes"')
.send({ error: "Unauthorized" });
return;
}
if (!authHeader || !authHeader.startsWith("Basic ")) {
reply
.status(StatusCodes.UNAUTHORIZED)
.header("WWW-Authenticate", 'Basic realm="Admin Routes"')
.send({ error: "Unauthorized" });
return;
}

// Parse the basic auth credentials (`Basic <base64 of username:password>`).
const base64Credentials = authHeader.split(" ")[1];
const credentials = Buffer.from(base64Credentials, "base64").toString(
"utf8",
);
const [username, password] = credentials.split(":");
// Parse the basic auth credentials (`Basic <base64 of username:password>`).
const base64Credentials = authHeader.split(" ")[1];
const credentials = Buffer.from(base64Credentials, "base64").toString(
"utf8",
);
const [username, password] = credentials.split(":");

if (!assertAdminBasicAuth(username, password)) {
reply
.status(StatusCodes.UNAUTHORIZED)
.header("WWW-Authenticate", 'Basic realm="Admin Routes"')
.send({ error: "Unauthorized" });
return;
}
}
});
});
if (!assertAdminBasicAuth(username, password)) {
reply
.status(StatusCodes.UNAUTHORIZED)
.header("WWW-Authenticate", 'Basic realm="Admin Routes"')
.send({ error: "Unauthorized" });
return;
}
}
});
});
};

const assertAdminBasicAuth = (username: string, password: string) => {
if (username === ADMIN_ROUTES_USERNAME) {
try {
const buf1 = Buffer.from(password.padEnd(100));
const buf2 = Buffer.from(ADMIN_ROUTES_PASSWORD.padEnd(100));
return timingSafeEqual(buf1, buf2);
} catch {}
}
return false;
if (username === ADMIN_ROUTES_USERNAME) {
try {
const buf1 = Buffer.from(password.padEnd(100));
const buf2 = Buffer.from(ADMIN_ROUTES_PASSWORD.padEnd(100));
return timingSafeEqual(buf1, buf2);
} catch {}
}
return false;
};

0 comments on commit f2f4716

Please sign in to comment.