feat: dont use arctic i hate libraries i hate libraries

This commit is contained in:
dusk 2024-08-24 14:25:28 +03:00
parent e71045f844
commit 8e8c5fc525
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
4 changed files with 25 additions and 5 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -39,7 +39,7 @@
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@std/toml": "npm:@jsr/std__toml", "@std/toml": "npm:@jsr/std__toml",
"arctic": "^2.0.0-next.5", "base64url": "^3.0.1",
"rehype-autolink-headings": "^7.1.0", "rehype-autolink-headings": "^7.1.0",
"rehype-slug": "^6.0.0" "rehype-slug": "^6.0.0"
}, },

View File

@ -2,12 +2,32 @@ import { dev } from "$app/environment";
import { env } from "$env/dynamic/private"; import { env } from "$env/dynamic/private";
import { PUBLIC_BASE_URL } from "$env/static/public"; import { PUBLIC_BASE_URL } from "$env/static/public";
import type { Cookies } from "@sveltejs/kit"; 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 callbackUrl = `${PUBLIC_BASE_URL}/guestbook/`
export const discord = new Discord(env.DISCORD_CLIENT_ID, "", callbackUrl) export const discord = {
export const github = new GitHub(env.GITHUB_CLIENT_ID, "", callbackUrl) 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) => { export const createAuthUrl = (authCb: (state: string) => URL, cookies: Cookies) => {
const state = generateState() const state = generateState()

View File

@ -40,7 +40,7 @@ const postAction = (client: any, scopes: string[]) => {
const params = new URLSearchParams({ author, content }) const params = new URLSearchParams({ author, content })
scopedCookies.set("postData", params.toString()) scopedCookies.set("postData", params.toString())
// get auth url to redirect user to // 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) redirect(303, authUrl)
} }
} }