This commit is contained in:
JOLIMAITRE Matthieu 2024-05-23 17:34:35 +02:00
parent 60133a4a08
commit c70202e5d1
6 changed files with 117 additions and 8 deletions

72
auth/auth.ts Normal file
View 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",
}),
);