Switch to jwks instead of custom /info

This commit is contained in:
2025-03-24 23:02:09 +01:00
parent 068b19c936
commit 6391a99bb9
8 changed files with 62 additions and 47 deletions
+7 -13
View File
@@ -1,20 +1,14 @@
import jwt from "@elysiajs/jwt";
import Elysia, { t } from "elysia";
import { createRemoteJWKSet } from "jose";
export let jwtSecret = process.env.JWT_SECRET!;
if (!jwtSecret) {
const auth = process.env.AUTH_SERVER ?? "http://auth:4568/auth";
try {
const ret = await fetch(`${auth}/info`);
const info = await ret.json();
jwtSecret = info.publicKey;
} catch (error) {
console.error(`Can't access auth server at ${auth}:\n${error}`);
}
}
const jwtSecret = process.env.JWT_SECRET;
const jwks = createRemoteJWKSet(
new URL(process.env.AUTH_SERVER ?? "http://auth:4568"),
);
export const auth = new Elysia({ name: "auth" })
.use(jwt({ secret: jwtSecret }))
.use(jwt({ secret: jwtSecret ?? jwks }))
.guard({
headers: t.Object({
authorization: t.String({ pattern: "^Bearer .+$" }),
@@ -25,7 +19,7 @@ export const auth = new Elysia({ name: "auth" })
return {
beforeHandle: () => {},
resolve: async ({ headers: { authorization }, jwt }) => {
console.log(authorization.slice(7));
console.log(authorization?.slice(7));
const user = await jwt.verify(authorization?.slice(7));
console.log("macro", user);
return { user };
-6
View File
@@ -1,5 +1,4 @@
import { swagger } from "@elysiajs/swagger";
import { jwtSecret } from "./auth";
import { app } from "./base";
import { processImages } from "./controllers/seed/images";
import { migrate } from "./db";
@@ -7,11 +6,6 @@ import { comment } from "./utils";
await migrate();
if (!jwtSecret) {
console.error("Missing jwt secret or auth server. exiting");
process.exit(1);
}
// run image processor task in background
processImages();