feat: add authentication

This commit is contained in:
2023-08-04 22:35:25 +02:00
parent f9638c35fc
commit 469db6525d
33 changed files with 492 additions and 100 deletions

15
lib/auth.ts Normal file
View File

@ -0,0 +1,15 @@
import { OAuth2Client } from "https://deno.land/x/oauth2_client@v1.0.2/mod.ts";
import {
GITEA_CLIENT_ID,
GITEA_CLIENT_SECRET,
GITEA_REDIRECT_URL,
GITEA_SERVER,
} from "@lib/env.ts";
export const oauth2Client = new OAuth2Client({
clientId: GITEA_CLIENT_ID,
clientSecret: GITEA_CLIENT_SECRET,
authorizationEndpointUri: `${GITEA_SERVER}/login/oauth/authorize`,
tokenUri: `${GITEA_SERVER}/login/oauth/access_token`,
redirectUri: GITEA_REDIRECT_URL,
});

17
lib/cache/cache.ts vendored
View File

@ -10,7 +10,7 @@ const REDIS_HOST = Deno.env.get("REDIS_HOST");
const REDIS_PASS = Deno.env.get("REDIS_PASS") || "";
const REDIS_PORT = Deno.env.get("REDIS_PORT");
async function createCache<T>(): Promise<Map<string, T> | Redis> {
async function createCache<T>(): Promise<Redis> {
if (REDIS_HOST) {
const conf: RedisConnectOptions = {
hostname: REDIS_HOST,
@ -32,6 +32,13 @@ async function createCache<T>(): Promise<Map<string, T> | Redis> {
const mockRedis = new Map<string, RedisValue>();
return {
async keys() {
return mockRedis.keys();
},
async delete(key: string) {
mockRedis.delete(key);
return key;
},
async set(key: string, value: RedisValue) {
mockRedis.set(key, value);
return value.toString();
@ -74,6 +81,14 @@ type RedisOptions = {
expires?: number;
};
export function del(key: string) {
return cache.del(key);
}
export function keys(prefix: string) {
return cache.keys(prefix);
}
export async function set<T extends RedisValue>(
id: string,
content: T,

View File

@ -1,7 +1,7 @@
import { Document } from "@lib/documents.ts";
import * as cache from "@lib/cache/cache.ts";
const CACHE_INTERVAL = 20; // 5 seconds;
const CACHE_INTERVAL = 60;
const CACHE_KEY = "documents";
export async function getDocuments() {
@ -19,12 +19,12 @@ export function setDocuments(documents: Document[]) {
}
export function getDocument(id: string) {
return cache.get<string>(CACHE_KEY + "/" + id);
return cache.get<string>(CACHE_KEY + ":" + id.replaceAll("/", ":"));
}
export async function setDocument(id: string, content: string) {
await cache.set(
CACHE_KEY + "/" + id,
CACHE_KEY + ":" + id.replaceAll("/", ":"),
content,
{ expires: CACHE_INTERVAL },
);

14
lib/cache/image.ts vendored
View File

@ -1,4 +1,4 @@
import { hash } from "@lib/hash.ts";
import { hash } from "@lib/string.ts";
import * as cache from "@lib/cache/cache.ts";
import { ImageMagick } from "https://deno.land/x/imagemagick_deno@0.0.25/mod.ts";
@ -13,10 +13,12 @@ const CACHE_KEY = "images";
function getCacheKey({ url: _url, width, height }: ImageCacheOptions) {
const url = new URL(_url);
return `${CACHE_KEY}/${url.hostname}/${url.pathname}/${width}/${height}`
return `${CACHE_KEY}:${url.hostname}:${
url.pathname.replaceAll("/", ":")
}:${width}:${height}`
.replace(
"//",
"/",
"::",
":",
);
}
@ -44,7 +46,7 @@ export async function getImage({ url, width, height }: ImageCacheOptions) {
? JSON.parse(pointerCacheRaw)
: pointerCacheRaw;
const imageContent = await cache.get(pointerCache.id, true);
const imageContent = await cache.get(`image:${pointerCache.id}`, true);
if (!imageContent) return;
return {
@ -68,7 +70,7 @@ export async function setImage(
const cacheKey = getCacheKey({ url, width, height });
const pointerId = await hash(cacheKey);
await cache.set(pointerId, clone);
await cache.set(`image:${pointerId}`, clone);
cache.expire(pointerId, 60 * 60 * 24);
cache.expire(cacheKey, 60 * 60 * 24);

View File

@ -14,7 +14,7 @@ export function createCrud<T>(
},
) {
function pathFromId(id: string) {
return `${prefix}${id}.md`;
return `${prefix}${id.replaceAll(":", "")}.md`;
}
async function read(id: string) {

18
lib/db.ts Normal file
View File

@ -0,0 +1,18 @@
import z from "https://deno.land/x/zod@v3.21.4/index.ts";
import { createSchema } from "@lib/db/createSchema.ts";
const UserSchema = z.object({
id: z.string().optional().default(() => crypto.randomUUID()),
createdAt: z.date().default(() => new Date()),
email: z.string().email(),
name: z.string(),
});
export const userDB = createSchema("user", UserSchema);
const SessionSchema = z.object({
id: z.string().default(() => crypto.randomUUID()),
createdAt: z.date().default(() => new Date()),
expiresAt: z.date().default(() => new Date()),
userId: z.string(),
});
export const sessionDB = createSchema("session", SessionSchema);

32
lib/db/createSchema.ts Normal file
View File

@ -0,0 +1,32 @@
import { z } from "zod";
import * as cache from "@lib/cache/cache.ts";
export function createSchema<T extends z.ZodSchema>(name: string, schema: T) {
type Data = z.infer<T>;
return {
async create(input: Omit<Data, "id">): Promise<Data> {
const data = schema.safeParse(input);
if (data.success) {
const d = data.data;
const id = d["id"];
if (!id) return d;
await cache.set(`${name}:${id}`, JSON.stringify(d));
return d;
}
return null;
},
async findAll(): Promise<Data[]> {
const keys = await cache.keys(`${name}:*`);
return Promise.all(keys.map((k) => {
return cache.get<string>(k);
})).then((values) => values.map((v) => JSON.parse(v || "null")));
},
async find(id: string) {
const k = await cache.get<string>(`${name}:${id}`);
return JSON.parse(k || "null") as Data | null;
},
delete(id: string) {
return cache.del(`${name}:${id}`);
},
};
}

View File

@ -4,3 +4,15 @@ export const REDIS_PASS = Deno.env.get("REDIS_PASS");
export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");
export const YOUTUBE_API_KEY = Deno.env.get("YOUTUBE_API_KEY");
export const GITEA_SERVER = Deno.env.get("GITEA_SERVER");
export const GITEA_CLIENT_ID = Deno.env.get("GITEA_CLIENT_ID");
export const GITEA_CLIENT_SECRET = Deno.env.get("GITEA_CLIENT_SECRET");
export const GITEA_REDIRECT_URL = Deno.env.get("GITEA_REDIRECT_URL");
export const DATABASE_URL = Deno.env.get("DATABASE_URL") || "dev.db";
const duration = Deno.env.get("SESSION_DURATION");
export const SESSION_DURATION = duration ? +duration : (60 * 60 * 24);
export const JWT_SECRET = Deno.env.get("JWT_SECRET");

View File

@ -22,4 +22,11 @@ class BadRequestError extends DomainError {
}
}
export { BadRequestError, DomainError, NotFoundError };
class AccessDeniedError extends DomainError {
status = 403;
constructor(public statusText = "Access Denied") {
super();
}
}
export { AccessDeniedError, BadRequestError, DomainError, NotFoundError };

View File

@ -1,9 +0,0 @@
export async function hash(message: string) {
const data = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
"",
);
return hashHex;
}

View File

@ -15,6 +15,7 @@ export type Article = {
status: "finished" | "not-finished";
date: Date;
link: string;
image?: string;
author?: string;
rating?: number;
};

View File

@ -13,6 +13,8 @@ export function safeFileName(inputString: string): string {
// Remove characters that are not safe for file names
fileName = fileName.replace(/[^\w.-]/g, "");
fileName = fileName.replaceAll(":", "");
return fileName;
}
@ -34,7 +36,9 @@ export function extractHashTags(inputString: string) {
export const isYoutubeLink = (link: string) => {
try {
const url = new URL(link);
return ["youtu.be", "youtube.com","www.youtube.com" ].includes(url.hostname);
return ["youtu.be", "youtube.com", "www.youtube.com"].includes(
url.hostname,
);
} catch (_err) {
return false;
}
@ -52,3 +56,39 @@ export function extractYoutubeId(link: string) {
return url.pathname.replace(/^\//, "");
}
export async function hash(message: string) {
const data = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
"",
);
return hashHex;
}
// Helper function to calculate SHA-256 hash
export async function sha256(input: string) {
const encoder = new TextEncoder();
const data = encoder.encode(input);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
return base64urlencode(new Uint8Array(hashBuffer));
}
// Helper function to encode a byte array as a URL-safe base64 string
function base64urlencode(data: Uint8Array) {
const base64 = btoa(String.fromCharCode(...data));
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
export function getCookie(name: string): string | null {
if (typeof document === "undefined") return null;
const nameLenPlus = name.length + 1;
return document.cookie
.split(";")
.map((c) => c.trim())
.filter((cookie) => {
return cookie.substring(0, nameLenPlus) === `${name}=`;
})
.map((cookie) => {
return decodeURIComponent(cookie.substring(nameLenPlus));
})[0] || null;
}

View File

@ -15,7 +15,7 @@ export function getMovieCredits(id: number) {
}
export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
const cachedPoster = await cache.get("posters/" + id);
const cachedPoster = await cache.get("posters:" + id);
if (cachedPoster) return cachedPoster as ArrayBuffer;
@ -23,6 +23,6 @@ export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
const response = await fetch(posterUrl);
const poster = await response.arrayBuffer();
cache.set(`posters/${id}`, new Uint8Array());
cache.set(`posters:${id}`, new Uint8Array());
return poster;
}

View File

@ -14,3 +14,12 @@ export interface TMDBMovie {
vote_average: number;
vote_count: number;
}
export interface GiteaOauthUser {
sub: string;
name: string;
preferred_username: string;
email: string;
picture: string;
groups: any;
}