From c61a32369cdedd3d3990624e01e37be89d538df3 Mon Sep 17 00:00:00 2001 From: split Date: Tue, 9 Jul 2024 16:29:27 -0700 Subject: [PATCH] make ava redirect to the authentication page --- .env.example | 4 +--- src/lib/configuration.ts | 17 +++++++++++++++++ src/lib/index.ts | 27 ++++++++++++++++++++++++++- src/routes/+layout.server.ts | 3 +++ src/routes/+layout.svelte | 4 ++-- src/routes/+page.svelte | 2 +- src/routes/logout/+page.server.ts | 3 ++- src/routes/set/+page.server.ts | 9 ++++++--- src/routes/set/+page.svelte | 6 +++--- 9 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 src/lib/configuration.ts create mode 100644 src/routes/+layout.server.ts diff --git a/.env.example b/.env.example index bf33140..b27bf64 100644 --- a/.env.example +++ b/.env.example @@ -11,6 +11,4 @@ OAUTH2__CLIENT_ID= OAUTH2__CLIENT_SECRET= # Userinfo route -USERINFO__ROUTE= -# Identifier -USERINFO__IDENTIFIER= \ No newline at end of file +USERINFO__ROUTE= \ No newline at end of file diff --git a/src/lib/configuration.ts b/src/lib/configuration.ts new file mode 100644 index 0000000..116f3f7 --- /dev/null +++ b/src/lib/configuration.ts @@ -0,0 +1,17 @@ +const configuration = { + oauth2: { + endpoints: { + authenticate: process.env.OAUTH2__AUTHENTICATE, + logout: process.env.OAUTH2__LOGOUT, + token: process.env.OAUTH2__TOKEN + }, + client: { + id: process.env.OAUTH2__CLIENT_ID, + secret: process.env.OAUTH2__CLIENT_SECRET + } + }, + userinfo: { + route: process.env.USERINFO__ROUTE + } +} +export default configuration \ No newline at end of file diff --git a/src/lib/index.ts b/src/lib/index.ts index 5893a83..95d0c57 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,28 @@ -export function userAuthorize() { +import { redirect } from "@sveltejs/kit" +import configuration from "./configuration" +const states = new Map>() + +export function launchLogin(req: Request) { + // Create a state to be used in the OAuth2 authorization request + const state = crypto.randomUUID() + + // Generate the query string and construct a URL using it + const searchParams = new URLSearchParams({ + response_type: "code", + client_id: configuration.oauth2.client.id, + redirect_uri: new URL(`/set`, req.url).toString(), + scope: "openid profile email", + state + }) + // Did not think this would work lmao + const target = new URL( + `?${searchParams.toString()}`, + configuration.oauth2.endpoints.authenticate + ) + + states + .set(state, setTimeout(() => states.delete(state), 60000)) + + return redirect(302, target.toString()) } \ No newline at end of file diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts new file mode 100644 index 0000000..74c2718 --- /dev/null +++ b/src/routes/+layout.server.ts @@ -0,0 +1,3 @@ +export async function load({request}) { + +} \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index e1fd901..918a0ba 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,6 +1,6 @@ @@ -32,7 +32,7 @@ diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 986861d..131d2aa 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -6,5 +6,5 @@

If you'd like to set a profile picture, click here. - If logged out, you will be redirected to the OAuth2 provider of this instance. + If logged out, you will be redirected to the OIDC provider of this instance.

diff --git a/src/routes/logout/+page.server.ts b/src/routes/logout/+page.server.ts index 40f67e2..6bcf1dd 100644 --- a/src/routes/logout/+page.server.ts +++ b/src/routes/logout/+page.server.ts @@ -1,5 +1,6 @@ +import configuration from "$lib/configuration.js"; import { redirect } from "@sveltejs/kit"; export function load({}) { - throw redirect(301, "/") + throw redirect(301, configuration.oauth2.endpoints.logout) } \ No newline at end of file diff --git a/src/routes/set/+page.server.ts b/src/routes/set/+page.server.ts index 0a8b1ef..60fb579 100644 --- a/src/routes/set/+page.server.ts +++ b/src/routes/set/+page.server.ts @@ -1,4 +1,7 @@ -export function load({ cookies }) { - let token = cookies.get("accessToken") - +import {launchLogin} from "$lib" +export async function load({ request, parent }) { + //const { user } = await parent(); + let user = null + if (!user) + throw launchLogin(request) } \ No newline at end of file diff --git a/src/routes/set/+page.svelte b/src/routes/set/+page.svelte index 7a9dcf5..aa6096b 100644 --- a/src/routes/set/+page.svelte +++ b/src/routes/set/+page.svelte @@ -1,8 +1,8 @@ -

Set an avatar

+

Hi, {data.user.username}

- Your identifier is {data.userid}. + Your identifier is {data.user.sub}.

\ No newline at end of file