72 lines
1.3 KiB
TypeScript
72 lines
1.3 KiB
TypeScript
import { assert } from "$std/assert/assert.ts";
|
|
import { User } from "../lib/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",
|
|
}),
|
|
);
|