feat: add authentication

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

View File

@ -11,3 +11,5 @@ export { default as IconRefresh } from "https://deno.land/x/tabler_icons_tsx@0.0
export { default as IconCirclePlus } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-plus.tsx"; export { default as IconCirclePlus } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-plus.tsx";
export { default as IconCircleMinus } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-minus.tsx"; export { default as IconCircleMinus } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/circle-minus.tsx";
export { default as IconLoader2 } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/loader-2.tsx"; export { default as IconLoader2 } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/loader-2.tsx";
export { default as IconLogin } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/login.tsx";
export { default as IconLogout } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/logout.tsx";

View File

@ -3,6 +3,7 @@
"tasks": { "tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts", "start": "deno run -A --watch=static/,routes/ dev.ts",
"debug": "deno run --inspect-wait -A main.ts", "debug": "deno run --inspect-wait -A main.ts",
"generate-prisma": "deno run -A --unstable npm:prisma@^5.1 generate --help",
"update": "deno run -A -r https://fresh.deno.dev/update ." "update": "deno run -A -r https://fresh.deno.dev/update ."
}, },
"lint": { "lint": {
@ -29,7 +30,8 @@
"@components": "./components", "@components": "./components",
"@components/": "./components/", "@components/": "./components/",
"@islands": "./islands", "@islands": "./islands",
"@islands/": "./islands/" "@islands/": "./islands/",
"zod": "https://deno.land/x/zod@v3.21.4/mod.ts"
}, },
"compilerOptions": { "compilerOptions": {
"jsx": "react-jsx", "jsx": "react-jsx",

0
dev.db Normal file
View File

View File

@ -8,25 +8,28 @@ import * as $2 from "./routes/_middleware.ts";
import * as $3 from "./routes/api/articles/[name].ts"; import * as $3 from "./routes/api/articles/[name].ts";
import * as $4 from "./routes/api/articles/create/index.ts"; import * as $4 from "./routes/api/articles/create/index.ts";
import * as $5 from "./routes/api/articles/index.ts"; import * as $5 from "./routes/api/articles/index.ts";
import * as $6 from "./routes/api/cache/index.ts"; import * as $6 from "./routes/api/auth/callback.ts";
import * as $7 from "./routes/api/images/index.ts"; import * as $7 from "./routes/api/auth/login.ts";
import * as $8 from "./routes/api/index.ts"; import * as $8 from "./routes/api/auth/logout.ts";
import * as $9 from "./routes/api/movies/[name].ts"; import * as $9 from "./routes/api/cache/index.ts";
import * as $10 from "./routes/api/movies/enhance/[name].ts"; import * as $10 from "./routes/api/images/index.ts";
import * as $11 from "./routes/api/movies/index.ts"; import * as $11 from "./routes/api/index.ts";
import * as $12 from "./routes/api/query/index.ts"; import * as $12 from "./routes/api/movies/[name].ts";
import * as $13 from "./routes/api/recipes/[name].ts"; import * as $13 from "./routes/api/movies/enhance/[name].ts";
import * as $14 from "./routes/api/recipes/index.ts"; import * as $14 from "./routes/api/movies/index.ts";
import * as $15 from "./routes/api/tmdb/[id].ts"; import * as $15 from "./routes/api/query/index.ts";
import * as $16 from "./routes/api/tmdb/credits/[id].ts"; import * as $16 from "./routes/api/recipes/[name].ts";
import * as $17 from "./routes/api/tmdb/query.ts"; import * as $17 from "./routes/api/recipes/index.ts";
import * as $18 from "./routes/articles/[name].tsx"; import * as $18 from "./routes/api/tmdb/[id].ts";
import * as $19 from "./routes/articles/index.tsx"; import * as $19 from "./routes/api/tmdb/credits/[id].ts";
import * as $20 from "./routes/index.tsx"; import * as $20 from "./routes/api/tmdb/query.ts";
import * as $21 from "./routes/movies/[name].tsx"; import * as $21 from "./routes/articles/[name].tsx";
import * as $22 from "./routes/movies/index.tsx"; import * as $22 from "./routes/articles/index.tsx";
import * as $23 from "./routes/recipes/[name].tsx"; import * as $23 from "./routes/index.tsx";
import * as $24 from "./routes/recipes/index.tsx"; import * as $24 from "./routes/movies/[name].tsx";
import * as $25 from "./routes/movies/index.tsx";
import * as $26 from "./routes/recipes/[name].tsx";
import * as $27 from "./routes/recipes/index.tsx";
import * as $$0 from "./islands/Counter.tsx"; import * as $$0 from "./islands/Counter.tsx";
import * as $$1 from "./islands/IngredientsList.tsx"; import * as $$1 from "./islands/IngredientsList.tsx";
import * as $$2 from "./islands/KMenu.tsx"; import * as $$2 from "./islands/KMenu.tsx";
@ -44,25 +47,28 @@ const manifest = {
"./routes/api/articles/[name].ts": $3, "./routes/api/articles/[name].ts": $3,
"./routes/api/articles/create/index.ts": $4, "./routes/api/articles/create/index.ts": $4,
"./routes/api/articles/index.ts": $5, "./routes/api/articles/index.ts": $5,
"./routes/api/cache/index.ts": $6, "./routes/api/auth/callback.ts": $6,
"./routes/api/images/index.ts": $7, "./routes/api/auth/login.ts": $7,
"./routes/api/index.ts": $8, "./routes/api/auth/logout.ts": $8,
"./routes/api/movies/[name].ts": $9, "./routes/api/cache/index.ts": $9,
"./routes/api/movies/enhance/[name].ts": $10, "./routes/api/images/index.ts": $10,
"./routes/api/movies/index.ts": $11, "./routes/api/index.ts": $11,
"./routes/api/query/index.ts": $12, "./routes/api/movies/[name].ts": $12,
"./routes/api/recipes/[name].ts": $13, "./routes/api/movies/enhance/[name].ts": $13,
"./routes/api/recipes/index.ts": $14, "./routes/api/movies/index.ts": $14,
"./routes/api/tmdb/[id].ts": $15, "./routes/api/query/index.ts": $15,
"./routes/api/tmdb/credits/[id].ts": $16, "./routes/api/recipes/[name].ts": $16,
"./routes/api/tmdb/query.ts": $17, "./routes/api/recipes/index.ts": $17,
"./routes/articles/[name].tsx": $18, "./routes/api/tmdb/[id].ts": $18,
"./routes/articles/index.tsx": $19, "./routes/api/tmdb/credits/[id].ts": $19,
"./routes/index.tsx": $20, "./routes/api/tmdb/query.ts": $20,
"./routes/movies/[name].tsx": $21, "./routes/articles/[name].tsx": $21,
"./routes/movies/index.tsx": $22, "./routes/articles/index.tsx": $22,
"./routes/recipes/[name].tsx": $23, "./routes/index.tsx": $23,
"./routes/recipes/index.tsx": $24, "./routes/movies/[name].tsx": $24,
"./routes/movies/index.tsx": $25,
"./routes/recipes/[name].tsx": $26,
"./routes/recipes/index.tsx": $27,
}, },
islands: { islands: {
"./islands/Counter.tsx": $$0, "./islands/Counter.tsx": $$0,

View File

@ -2,6 +2,7 @@ import { Menu } from "@islands/KMenu/types.ts";
import { addMovieInfos } from "@islands/KMenu/commands/add_movie_infos.ts"; import { addMovieInfos } from "@islands/KMenu/commands/add_movie_infos.ts";
import { createNewMovie } from "@islands/KMenu/commands/create_movie.ts"; import { createNewMovie } from "@islands/KMenu/commands/create_movie.ts";
import { createNewArticle } from "@islands/KMenu/commands/create_article.ts"; import { createNewArticle } from "@islands/KMenu/commands/create_article.ts";
import { getCookie } from "@lib/string.ts";
export const menus: Record<string, Menu> = { export const menus: Record<string, Menu> = {
main: { main: {
@ -18,6 +19,30 @@ export const menus: Record<string, Menu> = {
state.activeState.value = "normal"; state.activeState.value = "normal";
state.visible.value = false; state.visible.value = false;
}, },
visible: () => {
if (!getCookie("session_cookie")) return false;
return true;
},
},
{
title: "Login",
icon: "IconLogin",
cb: () => {
window.location.pathname = "/api/auth/login";
},
visible: () => {
return !getCookie("session_cookie");
},
},
{
title: "Logout",
icon: "IconLogout",
cb: () => {
window.location.pathname = "/api/auth/logout";
},
visible: () => {
return !!getCookie("session_cookie");
},
}, },
createNewArticle, createNewArticle,
createNewMovie, createNewMovie,

View File

@ -1,6 +1,7 @@
import { MenuEntry } from "@islands/KMenu/types.ts"; import { MenuEntry } from "@islands/KMenu/types.ts";
import { Movie } from "@lib/resource/movies.ts"; import { Movie } from "@lib/resource/movies.ts";
import { TMDBMovie } from "@lib/types.ts"; import { TMDBMovie } from "@lib/types.ts";
import { getCookie } from "@lib/string.ts";
export const addMovieInfos: MenuEntry = { export const addMovieInfos: MenuEntry = {
title: "Add Movie infos", title: "Add Movie infos",
@ -43,6 +44,7 @@ export const addMovieInfos: MenuEntry = {
}, },
visible: () => { visible: () => {
const loc = globalThis["location"]; const loc = globalThis["location"];
if (!getCookie("session_cookie")) return false;
return loc?.pathname?.includes("movie"); return loc?.pathname?.includes("movie");
}, },
}; };

View File

@ -1,5 +1,6 @@
import { MenuEntry } from "@islands/KMenu/types.ts"; import { MenuEntry } from "@islands/KMenu/types.ts";
import { fetchStream, isValidUrl } from "@lib/helpers.ts"; import { fetchStream, isValidUrl } from "@lib/helpers.ts";
import { getCookie } from "@lib/string.ts";
export const createNewArticle: MenuEntry = { export const createNewArticle: MenuEntry = {
title: "Create new article", title: "Create new article",
@ -35,5 +36,8 @@ export const createNewArticle: MenuEntry = {
} }
}); });
}, },
visible: () => true, visible: () => {
if (!getCookie("session_cookie")) return false;
return true;
},
}; };

View File

@ -2,6 +2,7 @@ import { MenuEntry } from "@islands/KMenu/types.ts";
import { TMDBMovie } from "@lib/types.ts"; import { TMDBMovie } from "@lib/types.ts";
import { debounce } from "@lib/helpers.ts"; import { debounce } from "@lib/helpers.ts";
import { Movie } from "@lib/resource/movies.ts"; import { Movie } from "@lib/resource/movies.ts";
import { getCookie } from "@lib/string.ts";
export const createNewMovie: MenuEntry = { export const createNewMovie: MenuEntry = {
title: "Create new movie", title: "Create new movie",
@ -40,8 +41,6 @@ export const createNewMovie: MenuEntry = {
const movies = await response.json() as TMDBMovie[]; const movies = await response.json() as TMDBMovie[];
console.log({ query, currentQuery, movies });
if (query !== currentQuery) return; if (query !== currentQuery) return;
state.menus["input_link"] = { state.menus["input_link"] = {
@ -55,6 +54,7 @@ export const createNewMovie: MenuEntry = {
method: "POST", method: "POST",
}); });
const movie = await response.json() as Movie; const movie = await response.json() as Movie;
unsub();
window.location.href = "/movies/" + movie.name; window.location.href = "/movies/" + movie.name;
}, },
}; };
@ -68,5 +68,8 @@ export const createNewMovie: MenuEntry = {
search(value); search(value);
}); });
}, },
visible: () => true, visible: () => {
if (!getCookie("session_cookie")) return false;
return true;
},
}; };

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

View File

@ -1,7 +1,7 @@
import { Document } from "@lib/documents.ts"; import { Document } from "@lib/documents.ts";
import * as cache from "@lib/cache/cache.ts"; import * as cache from "@lib/cache/cache.ts";
const CACHE_INTERVAL = 20; // 5 seconds; const CACHE_INTERVAL = 60;
const CACHE_KEY = "documents"; const CACHE_KEY = "documents";
export async function getDocuments() { export async function getDocuments() {
@ -19,12 +19,12 @@ export function setDocuments(documents: Document[]) {
} }
export function getDocument(id: string) { 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) { export async function setDocument(id: string, content: string) {
await cache.set( await cache.set(
CACHE_KEY + "/" + id, CACHE_KEY + ":" + id.replaceAll("/", ":"),
content, content,
{ expires: CACHE_INTERVAL }, { 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 * as cache from "@lib/cache/cache.ts";
import { ImageMagick } from "https://deno.land/x/imagemagick_deno@0.0.25/mod.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) { function getCacheKey({ url: _url, width, height }: ImageCacheOptions) {
const url = new URL(_url); 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( .replace(
"//", "::",
"/", ":",
); );
} }
@ -44,7 +46,7 @@ export async function getImage({ url, width, height }: ImageCacheOptions) {
? JSON.parse(pointerCacheRaw) ? JSON.parse(pointerCacheRaw)
: pointerCacheRaw; : pointerCacheRaw;
const imageContent = await cache.get(pointerCache.id, true); const imageContent = await cache.get(`image:${pointerCache.id}`, true);
if (!imageContent) return; if (!imageContent) return;
return { return {
@ -68,7 +70,7 @@ export async function setImage(
const cacheKey = getCacheKey({ url, width, height }); const cacheKey = getCacheKey({ url, width, height });
const pointerId = await hash(cacheKey); const pointerId = await hash(cacheKey);
await cache.set(pointerId, clone); await cache.set(`image:${pointerId}`, clone);
cache.expire(pointerId, 60 * 60 * 24); cache.expire(pointerId, 60 * 60 * 24);
cache.expire(cacheKey, 60 * 60 * 24); cache.expire(cacheKey, 60 * 60 * 24);

View File

@ -14,7 +14,7 @@ export function createCrud<T>(
}, },
) { ) {
function pathFromId(id: string) { function pathFromId(id: string) {
return `${prefix}${id}.md`; return `${prefix}${id.replaceAll(":", "")}.md`;
} }
async function read(id: string) { 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 TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
export const OPENAI_API_KEY = Deno.env.get("OPENAI_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 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"; status: "finished" | "not-finished";
date: Date; date: Date;
link: string; link: string;
image?: string;
author?: string; author?: string;
rating?: number; rating?: number;
}; };

View File

@ -13,6 +13,8 @@ export function safeFileName(inputString: string): string {
// Remove characters that are not safe for file names // Remove characters that are not safe for file names
fileName = fileName.replace(/[^\w.-]/g, ""); fileName = fileName.replace(/[^\w.-]/g, "");
fileName = fileName.replaceAll(":", "");
return fileName; return fileName;
} }
@ -34,7 +36,9 @@ export function extractHashTags(inputString: string) {
export const isYoutubeLink = (link: string) => { export const isYoutubeLink = (link: string) => {
try { try {
const url = new URL(link); 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) { } catch (_err) {
return false; return false;
} }
@ -52,3 +56,39 @@ export function extractYoutubeId(link: string) {
return url.pathname.replace(/^\//, ""); 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> { 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; if (cachedPoster) return cachedPoster as ArrayBuffer;
@ -23,6 +23,6 @@ export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
const response = await fetch(posterUrl); const response = await fetch(posterUrl);
const poster = await response.arrayBuffer(); const poster = await response.arrayBuffer();
cache.set(`posters/${id}`, new Uint8Array()); cache.set(`posters:${id}`, new Uint8Array());
return poster; return poster;
} }

View File

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

12
package.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "silver-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

View File

@ -1,6 +1,10 @@
//routes/middleware-error-handler/_middleware.ts //routes/middleware-error-handler/_middleware.ts
import { MiddlewareHandlerContext } from "$fresh/server.ts"; import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { DomainError } from "@lib/errors.ts"; import { DomainError } from "@lib/errors.ts";
import { getCookies } from "https://deno.land/std@0.197.0/http/cookie.ts";
import { decode, verify } from "https://deno.land/x/djwt@v2.2/mod.ts";
import { sessionDB, userDB } from "@lib/db.ts";
import { JWT_SECRET } from "@lib/env.ts";
export async function handler( export async function handler(
_req: Request, _req: Request,
@ -8,6 +12,18 @@ export async function handler(
) { ) {
try { try {
ctx.state.flag = true; ctx.state.flag = true;
const allCookies = getCookies(_req.headers);
const sessionCookie = allCookies["session_cookie"];
if (!ctx.state.session && sessionCookie && JWT_SECRET) {
try {
const payload = await verify(sessionCookie, JWT_SECRET, "HS512");
if (payload) {
ctx.state.session = payload;
}
} catch (_err) {
//
}
}
return await ctx.next(); return await ctx.next();
} catch (error) { } catch (error) {
console.error(error); console.error(error);

View File

@ -1,15 +1,14 @@
import { Handlers } from "$fresh/server.ts"; import { Handlers } from "$fresh/server.ts";
import { Readability } from "https://cdn.skypack.dev/@mozilla/readability"; import { Readability } from "https://cdn.skypack.dev/@mozilla/readability";
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts"; import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts";
import { BadRequestError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { createStreamResponse, isValidUrl, json } from "@lib/helpers.ts"; import { createStreamResponse, isValidUrl } from "@lib/helpers.ts";
import * as openai from "@lib/openai.ts"; import * as openai from "@lib/openai.ts";
import tds from "https://cdn.skypack.dev/turndown@7.1.1"; import tds from "https://cdn.skypack.dev/turndown@7.1.1";
//import { gfm } from "https://cdn.skypack.dev/@guyplusplus/turndown-plugin-gfm@1.0.7";
import { Article, createArticle } from "@lib/resource/articles.ts"; import { Article, createArticle } from "@lib/resource/articles.ts";
import { getYoutubeVideoDetails } from "@lib/youtube.ts"; import { getYoutubeVideoDetails } from "@lib/youtube.ts";
import { extractYoutubeId, formatDate, isYoutubeLink } from "@lib/string.ts"; import { extractYoutubeId, isYoutubeLink } from "@lib/string.ts";
const parser = new DOMParser(); const parser = new DOMParser();
@ -34,6 +33,11 @@ async function processCreateArticle(
const title = document?.querySelector("title")?.innerText; const title = document?.querySelector("title")?.innerText;
const images: HTMLImageElement[] = [];
document?.querySelectorAll("img").forEach((img) => {
images.push(img as unknown as HTMLImageElement);
});
const metaAuthor = const metaAuthor =
document?.querySelector('meta[name="twitter:creator"]')?.getAttribute( document?.querySelector('meta[name="twitter:creator"]')?.getAttribute(
"content", "content",
@ -62,6 +66,19 @@ async function processCreateArticle(
}); });
const url = new URL(fetchUrl); const url = new URL(fetchUrl);
function makeUrlAbsolute(src: string) {
if (src.startsWith("/")) {
return `${url.origin}${src.replace(/$\//, "")}`;
}
if (!src.startsWith("https://") && !src.startsWith("http://")) {
return `${url.origin.replace(/\/$/, "")}/${src.replace(/^\//, "")})`;
}
return src;
}
service.addRule("fix image links", { service.addRule("fix image links", {
filter: ["img"], filter: ["img"],
replacement: function (_: string, node: HTMLImageElement) { replacement: function (_: string, node: HTMLImageElement) {
@ -69,17 +86,7 @@ async function processCreateArticle(
const alt = node.getAttribute("alt") || ""; const alt = node.getAttribute("alt") || "";
if (!src || src.startsWith("data:image")) return ""; if (!src || src.startsWith("data:image")) return "";
if (src.startsWith("/")) { return `![${alt}](${makeUrlAbsolute(src)})`;
return `![${alt}](${url.origin}${src.replace(/$\//, "")})`;
}
if (!src.startsWith("https://") && !src.startsWith("http://")) {
return `![${alt}](${url.origin.replace(/\/$/, "")}/${
src.replace(/^\//, "")
})`;
}
return `![${alt}](${src})`;
}, },
}); });
service.addRule("fix normal links", { service.addRule("fix normal links", {
@ -119,19 +126,40 @@ async function processCreateArticle(
const id = shortTitle || title || ""; const id = shortTitle || title || "";
const meta: Article["meta"] = {
author: (author || "").replace("@", "twitter:"),
link: fetchUrl,
status: "not-finished",
date: new Date(),
};
const largestImage = images.filter((img) => {
const src = img.getAttribute("src");
return !!src && !src.startsWith("data:");
}).sort((a, b) => {
const aSize = +(a.getAttribute("width") || 0) +
+(a.getAttribute("height") || 0);
const bSize = +(b.getAttribute("width") || 0) +
+(b.getAttribute("height") || 0);
return aSize > bSize ? -1 : 1;
})[0];
const newArticle = { const newArticle = {
type: "article",
id, id,
name: title || "", name: title || "",
content: markdown, content: markdown,
tags: tags || [], tags: tags || [],
meta: { meta,
author: (author || "").replace("@", "twitter:"),
link: fetchUrl,
status: "not-finished",
date: new Date(),
},
} as const; } as const;
if (largestImage) {
const src = makeUrlAbsolute(largestImage.getAttribute("src") || "");
if (src) {
meta.image = src;
}
}
streamResponse.enqueue("finished processing"); streamResponse.enqueue("finished processing");
await createArticle(newArticle); await createArticle(newArticle);
@ -181,7 +209,12 @@ async function processCreateYoutubeVideo(
} }
export const handler: Handlers = { export const handler: Handlers = {
GET(req) { GET(req, ctx) {
const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const url = new URL(req.url); const url = new URL(req.url);
const fetchUrl = url.searchParams.get("url"); const fetchUrl = url.searchParams.get("url");

View File

@ -0,0 +1,75 @@
import { Handlers } from "$fresh/server.ts";
import { create, getNumericDate } from "https://deno.land/x/djwt@v2.2/mod.ts";
import { oauth2Client } from "@lib/auth.ts";
import {
getCookies,
setCookie,
} from "https://deno.land/std@0.197.0/http/cookie.ts";
import { codeChallengeMap } from "./login.ts";
import { GITEA_SERVER, JWT_SECRET, SESSION_DURATION } from "@lib/env.ts";
import { userDB } from "@lib/db.ts";
import { GiteaOauthUser } from "@lib/types.ts";
import { BadRequestError } from "@lib/errors.ts";
export const handler: Handlers = {
async GET(request, ctx) {
if (!JWT_SECRET) {
throw new BadRequestError();
}
// Exchange the authorization code for an access token
const cookies = getCookies(request.headers);
const codeVerifier = codeChallengeMap.get(cookies["code_challenge"]);
const tokens = await oauth2Client.code.getToken(request.url, {
codeVerifier,
});
// Use the access token to make an authenticated API request
const userInfo = `${GITEA_SERVER}/login/oauth/userinfo`;
const userResponse = await fetch(userInfo, {
headers: {
Authorization: `token ${tokens.accessToken}`,
},
});
const oauthUser = await userResponse.json() as GiteaOauthUser;
const allUsers = await userDB.findAll();
let user = allUsers.find((u) => u.name === oauthUser.name);
if (!user) {
user = await userDB.create({
createdAt: new Date(),
email: oauthUser.email,
name: oauthUser.name,
});
}
const jwt = await create({ alg: "HS512", type: "JWT" }, {
id: user.id,
name: user.name,
exp: getNumericDate(SESSION_DURATION),
}, JWT_SECRET);
const headers = new Headers({
location: "/",
});
setCookie(headers, {
name: "session_cookie",
value: jwt,
path: "/",
maxAge: SESSION_DURATION,
httpOnly: false,
secure: true,
sameSite: "Lax",
});
return new Response(null, {
headers,
status: 302,
});
},
};

29
routes/api/auth/login.ts Normal file
View File

@ -0,0 +1,29 @@
import { Handlers } from "$fresh/server.ts";
import { oauth2Client } from "@lib/auth.ts";
import { sha256 } from "@lib/string.ts";
import { setCookie } from "https://deno.land/std@0.197.0/http/cookie.ts";
export const codeChallengeMap = new Map();
export const handler: Handlers = {
async GET() {
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
const codeChallenge = uri.searchParams.get("code_challenge");
if (!codeChallenge) return new Response();
codeChallengeMap.set(codeChallenge, codeVerifier);
const headers = new Headers();
setCookie(headers, {
name: "code_challenge",
value: codeChallenge,
});
headers.append("location", uri.href);
return new Response(null, {
headers,
status: 302,
});
},
};

17
routes/api/auth/logout.ts Normal file
View File

@ -0,0 +1,17 @@
import { deleteCookie } from "https://deno.land/std@0.197.0/http/cookie.ts";
import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
GET() {
const headers = new Headers();
headers.append("location", "/");
deleteCookie(headers, "session_cookie", {
path: "/",
});
return new Response(null, {
headers,
status: 302,
});
},
};

View File

@ -5,6 +5,7 @@ import * as tmdb from "@lib/tmdb.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { safeFileName } from "@lib/string.ts"; import { safeFileName } from "@lib/string.ts";
import { createDocument } from "@lib/documents.ts"; import { createDocument } from "@lib/documents.ts";
import { AccessDeniedError } from "@lib/errors.ts";
export const handler: Handlers = { export const handler: Handlers = {
async GET(_, ctx) { async GET(_, ctx) {
@ -12,6 +13,11 @@ export const handler: Handlers = {
return json(movie); return json(movie);
}, },
async POST(_, ctx) { async POST(_, ctx) {
const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const tmdbId = parseInt(ctx.params.name); const tmdbId = parseInt(ctx.params.name);
const movieDetails = await tmdb.getMovie(tmdbId); const movieDetails = await tmdb.getMovie(tmdbId);

View File

@ -10,7 +10,7 @@ import * as tmdb from "@lib/tmdb.ts";
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts"; import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
import { formatDate, safeFileName } from "@lib/string.ts"; import { formatDate, safeFileName } from "@lib/string.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import { BadRequestError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
async function updateMovieMetadata( async function updateMovieMetadata(
name: string, name: string,
@ -49,13 +49,18 @@ async function updateMovieMetadata(
} }
const POST = async ( const POST = async (
_req: Request, req: Request,
_ctx: HandlerContext, ctx: HandlerContext,
): Promise<Response> => { ): Promise<Response> => {
const movie = await getMovie(_ctx.params.name); const movie = await getMovie(ctx.params.name);
const body = await _req.json(); const session = ctx.state.session;
const name = _ctx.params.name; if (!session) {
throw new AccessDeniedError();
}
const body = await req.json();
const name = ctx.params.name;
const { tmdbId } = body; const { tmdbId } = body;
if (!name || !tmdbId) { if (!name || !tmdbId) {
throw new BadRequestError(); throw new BadRequestError();

View File

@ -3,6 +3,7 @@ import { json } from "@lib/helpers.ts";
import { getAllMovies, Movie } from "@lib/resource/movies.ts"; import { getAllMovies, Movie } from "@lib/resource/movies.ts";
import { Article, getAllArticles } from "@lib/resource/articles.ts"; import { Article, getAllArticles } from "@lib/resource/articles.ts";
import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts"; import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts";
import { AccessDeniedError } from "@lib/errors.ts";
const isResource = ( const isResource = (
item: Movie | Article | Recipe | boolean, item: Movie | Article | Recipe | boolean,
@ -11,7 +12,12 @@ const isResource = (
}; };
export const handler: Handlers = { export const handler: Handlers = {
async GET(req) { async GET(req, ctx) {
const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const url = new URL(req.url); const url = new URL(req.url);
const types = url.searchParams.get("type")?.split(", "); const types = url.searchParams.get("type")?.split(", ");

View File

@ -1,7 +1,7 @@
import { HandlerContext, Handlers } from "$fresh/server.ts"; import { HandlerContext, Handlers } from "$fresh/server.ts";
import { searchMovie } from "@lib/tmdb.ts"; import { searchMovie } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts"; import * as cache from "@lib/cache/cache.ts";
import { BadRequestError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
type CachedMovieQuery = { type CachedMovieQuery = {
@ -12,10 +12,15 @@ type CachedMovieQuery = {
const CACHE_INTERVAL = 1000 * 60 * 24 * 30; const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
const GET = async ( const GET = async (
_req: Request, req: Request,
_ctx: HandlerContext, ctx: HandlerContext,
) => { ) => {
const u = new URL(_req.url); const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const u = new URL(req.url);
const query = u.searchParams.get("q"); const query = u.searchParams.get("q");

View File

@ -3,6 +3,7 @@ import { MainLayout } from "@components/layouts/main.tsx";
import { Card } from "@components/Card.tsx"; import { Card } from "@components/Card.tsx";
import { PageProps } from "$fresh/server.ts"; import { PageProps } from "$fresh/server.ts";
import { menu } from "@lib/menus.ts"; import { menu } from "@lib/menus.ts";
import { KMenu } from "@islands/KMenu.tsx";
export default function Home(props: PageProps) { export default function Home(props: PageProps) {
return ( return (
@ -10,6 +11,7 @@ export default function Home(props: PageProps) {
<Head> <Head>
<title>app</title> <title>app</title>
</Head> </Head>
<KMenu type="main" context={null} />
<MainLayout url={props.url}> <MainLayout url={props.url}>
<div class="flex flex-wrap items-center gap-4 px-4"> <div class="flex flex-wrap items-center gap-4 px-4">
{menu.map((m) => { {menu.map((m) => {