(feur)
This commit is contained in:
parent
60133a4a08
commit
c70202e5d1
6 changed files with 117 additions and 8 deletions
72
auth/auth.ts
Normal file
72
auth/auth.ts
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import { assert } from "$std/assert/assert.ts";
|
||||||
|
import { User } from "../storage/store.ts";
|
||||||
|
import { generate } from "https://deno.land/std@0.224.0/uuid/v1.ts";
|
||||||
|
|
||||||
|
class Authentificator {
|
||||||
|
tokens;
|
||||||
|
user_storage;
|
||||||
|
constructor(user_storage: UserStorage) {
|
||||||
|
this.tokens = new TokenSet();
|
||||||
|
this.user_storage = user_storage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserStorage {
|
||||||
|
get_user(login: string): User | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Token {
|
||||||
|
raw;
|
||||||
|
user_id;
|
||||||
|
constructor(raw: string, user_id: string) {
|
||||||
|
this.raw = raw;
|
||||||
|
this.user_id = user_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TokenSet {
|
||||||
|
tokens;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.tokens = new Map<string, Token>();
|
||||||
|
}
|
||||||
|
|
||||||
|
get(token: string) {
|
||||||
|
return this.tokens.get(token) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
create(user_id: string) {
|
||||||
|
const raw = generate();
|
||||||
|
assert(typeof raw == "string");
|
||||||
|
const token = new Token(raw, user_id);
|
||||||
|
this.tokens.set(raw, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StaticUserStorage implements UserStorage {
|
||||||
|
users;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.users = new Map<string, User>();
|
||||||
|
}
|
||||||
|
|
||||||
|
get_user(user_id: string) {
|
||||||
|
return this.users.get(user_id) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
with(user: User) {
|
||||||
|
this.users.set(user.id, user);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const auth = new Authentificator(
|
||||||
|
new StaticUserStorage().with({
|
||||||
|
id: "pleinplein",
|
||||||
|
email: "feur@feur.feur",
|
||||||
|
like_set: new Set(),
|
||||||
|
name: "Feur",
|
||||||
|
password: "feurfeur",
|
||||||
|
pfp_url: "https://feur.feur.feur/feur.feur",
|
||||||
|
}),
|
||||||
|
);
|
|
@ -19,9 +19,9 @@
|
||||||
- email
|
- email
|
||||||
- password
|
- password
|
||||||
- pfp_url
|
- pfp_url
|
||||||
|
- like_set
|
||||||
* posts
|
* posts
|
||||||
* comments
|
* comments
|
||||||
* like_set
|
|
||||||
|
|
||||||
- Post
|
- Post
|
||||||
- title
|
- title
|
||||||
|
|
0
routes/feur.tsx
Normal file
0
routes/feur.tsx
Normal file
|
@ -2,16 +2,14 @@ import { useSignal } from "@preact/signals";
|
||||||
import Counter from "../islands/Counter.tsx";
|
import Counter from "../islands/Counter.tsx";
|
||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import { getCookies } from "$std/http/cookie.ts";
|
import { getCookies } from "$std/http/cookie.ts";
|
||||||
import { User } from "../storage/models/User.ts";
|
import { db, User } from "../storage/store.ts";
|
||||||
|
import { auth } from "../auth/auth.ts";
|
||||||
interface Data {
|
|
||||||
user: User;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
GET(req, ctx) {
|
GET(req, ctx) {
|
||||||
const cookies = getCookies(req.headers);
|
const cookies = getCookies(req.headers);
|
||||||
return ctx.render!({ allowed: cookies.auth === "bar" });
|
const user = get_session_user(cookies);
|
||||||
|
return ctx.render!({ user });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
40
routes/middleware.ts
Normal file
40
routes/middleware.ts
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
|
import { deleteCookie, getCookies, setCookie } from "$std/http/cookie.ts";
|
||||||
|
import { auth, Token } from "../auth/auth.ts";
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
user_id: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function handler(
|
||||||
|
req: Request,
|
||||||
|
ctx: FreshContext<State>,
|
||||||
|
) {
|
||||||
|
const cookies = getCookies(req.headers);
|
||||||
|
const token = get_session_token(cookies);
|
||||||
|
|
||||||
|
ctx.state.user_id = token?.user_id ?? null;
|
||||||
|
|
||||||
|
const resp = await ctx.next();
|
||||||
|
set_session_token(resp.headers, token);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_session_token(cookies: Record<string, string>) {
|
||||||
|
const stored = cookies["auth_token"];
|
||||||
|
if (stored === undefined) return null;
|
||||||
|
const token = auth.tokens.get(stored);
|
||||||
|
if (token === null) return null;
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_session_token(headers: Headers, token: Token | null) {
|
||||||
|
if (token === null) {
|
||||||
|
deleteCookie(headers, "auth_token");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setCookie(headers, {
|
||||||
|
name: "auth_token",
|
||||||
|
value: token.raw,
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
import { createPentagon } from "https://deno.land/x/pentagon@v0.1.5/mod.ts";
|
import { createPentagon } from "https://deno.land/x/pentagon@v0.1.5/mod.ts";
|
||||||
import { project_root_dir } from "../utils.ts";
|
import { project_root_dir } from "../utils.ts";
|
||||||
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts";
|
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts";
|
||||||
import mimeDbV1520 from "$std/media_types/vendor/mime-db.v1.52.0.ts";
|
|
||||||
|
|
||||||
const kv = await Deno.openKv(project_root_dir() + "local/kv");
|
const kv = await Deno.openKv(project_root_dir() + "local/kv");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue