feat: trying to add hashes to scripts

This commit is contained in:
Max Richter
2026-01-10 13:03:13 +01:00
parent e65938ecc2
commit e55f787a29
79 changed files with 4209 additions and 720 deletions

View File

@@ -1,5 +1,4 @@
import { Handlers } from "$fresh/server.ts";
import { create, getNumericDate } from "https://deno.land/x/djwt@v2.2/mod.ts";
import { create, getNumericDate } from "@zaubrik/djwt";
import { oauth2Client } from "@lib/auth.ts";
import { getCookies, setCookie } from "@std/http/cookie";
import { codeChallengeMap } from "./login.ts";
@@ -9,15 +8,16 @@ import { BadRequestError } from "@lib/errors.ts";
import { db } from "@lib/db/sqlite.ts";
import { userTable } from "@lib/db/schema.ts";
import { eq } from "drizzle-orm";
import { define } from "../../../utils.ts";
export const handler: Handlers = {
async GET(request) {
export const handler = define.handlers({
async GET(ctx) {
if (!JWT_SECRET) {
throw new BadRequestError();
}
// Exchange the authorization code for an access token
const cookies = getCookies(request.headers);
const cookies = getCookies(ctx.req.headers);
const stored = codeChallengeMap.get(cookies["code_challenge"]);
if (!stored) {
@@ -26,7 +26,7 @@ export const handler: Handlers = {
const { codeVerifier, redirect } = stored;
const tokens = await oauth2Client.code.getToken(request.url, {
const tokens = await oauth2Client.code.getToken(ctx.req.url, {
codeVerifier,
});
@@ -53,11 +53,23 @@ export const handler: Handlers = {
user = res[0];
}
if (!JWT_SECRET) {
throw new BadRequestError();
}
const key = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(JWT_SECRET),
{ name: "HMAC", hash: "SHA-512" },
false,
["sign", "verify"],
);
const jwt = await create({ alg: "HS512", type: "JWT" }, {
id: user.id,
name: user.name,
exp: getNumericDate(SESSION_DURATION),
}, JWT_SECRET);
}, key);
const headers = new Headers({
location: redirect || "/",
@@ -78,4 +90,4 @@ export const handler: Handlers = {
status: 302,
});
},
};
});

View File

@@ -1,14 +1,15 @@
import { Handlers } from "$fresh/server.ts";
import { oauth2Client } from "@lib/auth.ts";
import { setCookie } from "@std/http/cookie";
import { define } from "../../../utils.ts";
export const codeChallengeMap = new Map<
string,
{ codeVerifier: string; redirect?: string }
>();
export const handler: Handlers = {
async GET(req) {
export const handler = define.handlers({
async GET(ctx) {
const req = ctx.req;
const url = new URL(req.url);
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
@@ -33,4 +34,4 @@ export const handler: Handlers = {
status: 302,
});
},
};
});

View File

@@ -1,8 +1,9 @@
import { deleteCookie } from "@std/http/cookie";
import { Handlers } from "$fresh/server.ts";
import { define } from "../../../utils.ts";
export const handler: Handlers = {
GET(req) {
export const handler = define.handlers({
GET(ctx) {
const req = ctx.req;
const url = new URL(req.url);
const redirect = decodeURIComponent(url.searchParams.get("redirect") || "");
@@ -19,4 +20,4 @@ export const handler: Handlers = {
status: 302,
});
},
};
});