Skip to content

Commit 83621b8

Browse files
committed
wip: try to fix jwt
1 parent 760a869 commit 83621b8

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

src/api.admin.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare module "hono" {
2121
const secret = "my_super_duper_secret_key_for_admin_jwt";
2222

2323
export async function createJWT(payload: JWTPayload): Promise<string> {
24-
const jwt = await sign(payload, secret);
24+
const jwt = await sign(payload, secret, "HS256");
2525
return jwt;
2626
}
2727

@@ -35,12 +35,9 @@ export async function verifyJWT(token: string): Promise<JWTPayload | null> {
3535
}
3636
}
3737

38-
export function jwtCheck(c: Context, next: MiddlewareHandler) {
39-
return jwt({
40-
secret,
41-
algorithms: ["HS256"],
42-
})(c, next);
43-
}
38+
// export function jwtCheck(c: Context, next: MiddlewareHandler) {
39+
// return
40+
// }
4441

4542
async function hostFrontendHandler(c: Context): Promise<Response> {
4643
try {
@@ -141,7 +138,10 @@ export function setupAdminAPIRoutes(
141138
const app = new Hono();
142139
app.use(
143140
"*",
144-
jwtCheck,
141+
jwt({
142+
secret,
143+
// algorithms: ["HS256"],
144+
}),
145145
);
146146

147147
setupAPIRoutes(app, dbContext);

src/nanoedge.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
stopAllServices,
1313
} from "./managers/service-manager.ts";
1414
import { setupApiRoutes, setupDocsRoutes } from "./api.service.ts";
15-
import { setupAdminAPIRoutes } from "./api.admin.ts";
15+
import { createJWT, JWTPayload, setupAdminAPIRoutes } from "./api.admin.ts";
1616
import { Context } from "hono";
1717
import openapi from "./openapi.ts";
1818
import { setupFunctionAPIRoutes } from "./api.function.ts";
@@ -31,6 +31,18 @@ export async function createNanoEdgeRT(
3131
app.use("*", cors());
3232
app.use("*", logger());
3333

34+
// Localhost-only middleware
35+
const localhostOnly = async (c: Context, next: () => Promise<void>) => {
36+
const clientIP = c.req.header("x-forwarded-for") || c.req.header("x-real-ip") || "127.0.0.1";
37+
const allowedIPs = ["127.0.0.1", "::1", "localhost"];
38+
39+
if (!allowedIPs.includes(clientIP)) {
40+
return c.json({ error: "Access denied. Only localhost is allowed." }, 403);
41+
}
42+
43+
await next();
44+
};
45+
3446
const status = (c: Context) => {
3547
const now = new Date();
3648
const upTimeMs = now.getTime() - new Date(startTime).getTime();
@@ -50,10 +62,43 @@ export async function createNanoEdgeRT(
5062
});
5163
};
5264
app.use("/docs", swaggerUI({ url: "/openapi.json" }));
53-
app.get("/openapi.json", (c) => c.json(openapi, 200));
65+
app.get("/openapi.json", (c: Context) => c.json(openapi, 200));
5466
app.get("/health", status);
5567
app.get("/status", status);
5668
app.get("/static/*", serveStatic({ root: "./" }));
69+
70+
// JWT creation API - localhost only
71+
app.post("/jwt/create", localhostOnly, async (c: Context) => {
72+
try {
73+
const body = await c.req.json();
74+
const { sub, exp, ...additionalClaims } = body;
75+
76+
if (!sub || !exp) {
77+
return c.json({
78+
error: "Missing required fields: 'sub' (subject) and 'exp' (expiration)",
79+
}, 400);
80+
}
81+
82+
const payload: JWTPayload = {
83+
sub,
84+
exp,
85+
...additionalClaims,
86+
};
87+
88+
const token = await createJWT(payload);
89+
return c.json({
90+
token,
91+
payload,
92+
}, 200);
93+
} catch (error) {
94+
console.error("JWT creation error:", error);
95+
return c.json({
96+
error: "Failed to create JWT",
97+
message: error instanceof Error ? error.message : String(error),
98+
}, 500);
99+
}
100+
});
101+
57102
app.route("/api/docs", setupDocsRoutes(serviceManagerState));
58103
app.route("/api/v2", setupApiRoutes(serviceManagerState));
59104
app.route("/functions/v2", setupFunctionAPIRoutes(dbContext));

0 commit comments

Comments
 (0)