diff --git a/bun.lockb b/bun.lockb index 6f67b2c..4fd6ee7 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index a704149..d41fad2 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "type": "module", "dependencies": { "@std/toml": "npm:@jsr/std__toml", - "arctic": "^2.0.0-next.5", + "base64url": "^3.0.1", "rehype-autolink-headings": "^7.1.0", "rehype-slug": "^6.0.0" }, diff --git a/src/lib/guestbookAuth.ts b/src/lib/guestbookAuth.ts index 493892d..3bf9d67 100644 --- a/src/lib/guestbookAuth.ts +++ b/src/lib/guestbookAuth.ts @@ -2,12 +2,32 @@ import { dev } from "$app/environment"; import { env } from "$env/dynamic/private"; import { PUBLIC_BASE_URL } from "$env/static/public"; import type { Cookies } from "@sveltejs/kit"; -import { Discord, generateState, GitHub } from "arctic"; +import base64url from "base64url"; export const callbackUrl = `${PUBLIC_BASE_URL}/guestbook/` -export const discord = new Discord(env.DISCORD_CLIENT_ID, "", callbackUrl) -export const github = new GitHub(env.GITHUB_CLIENT_ID, "", callbackUrl) +export const discord = { + getAuthUrl: (state: string, scopes: string[] = []) => { + const client_id = env.DISCORD_CLIENT_ID + const redir_uri = encodeURIComponent(callbackUrl) + const scope = scopes.join("+") + return `https://discord.com/oauth2/authorize?client_id=${client_id}&response_type=code&redirect_uri=${redir_uri}&scope=${scope}&state=${state}` + } +} +export const github = { + getAuthUrl: (state: string, scopes: string[] = []) => { + const client_id = env.GITHUB_CLIENT_ID + const redir_uri = encodeURIComponent(callbackUrl) + const scope = encodeURIComponent(scopes.join(" ")) + return `https://github.com/login/oauth/authorize?client_id=${client_id}&redirect_uri=${redir_uri}&scope=${scope}&state=${state}` + } +} + +export const generateState = () => { + const randomValues = new Uint8Array(32) + crypto.getRandomValues(randomValues) + return base64url(Buffer.from(randomValues)) +} export const createAuthUrl = (authCb: (state: string) => URL, cookies: Cookies) => { const state = generateState() diff --git a/src/routes/guestbook/+page.server.ts b/src/routes/guestbook/+page.server.ts index 69112e4..5bc1052 100644 --- a/src/routes/guestbook/+page.server.ts +++ b/src/routes/guestbook/+page.server.ts @@ -40,7 +40,7 @@ const postAction = (client: any, scopes: string[]) => { const params = new URLSearchParams({ author, content }) scopedCookies.set("postData", params.toString()) // get auth url to redirect user to - const authUrl = await auth.createAuthUrl((state) => client.createAuthorizationURL(state, scopes), cookies) + const authUrl = auth.createAuthUrl(client.getAuthUrl, cookies) redirect(303, authUrl) } }