make ava redirect to the authentication page

This commit is contained in:
May 2024-07-09 16:29:27 -07:00
parent dc6ba5f2c1
commit c61a32369c
Signed by: split
GPG key ID: C325C61F0BF517C0
9 changed files with 61 additions and 14 deletions

View file

@ -12,5 +12,3 @@ OAUTH2__CLIENT_SECRET=
# Userinfo route
USERINFO__ROUTE=
# Identifier
USERINFO__IDENTIFIER=

17
src/lib/configuration.ts Normal file
View file

@ -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

View file

@ -1,3 +1,28 @@
export function userAuthorize() {
import { redirect } from "@sveltejs/kit"
import configuration from "./configuration"
const states = new Map<string, ReturnType<typeof setTimeout>>()
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())
}

View file

@ -0,0 +1,3 @@
export async function load({request}) {
}

View file

@ -1,6 +1,6 @@
<script lang="ts">
import "@fontsource-variable/inter";
export let data: { userid?: string };
export let data: { user?: { sub: string, username: string } };
</script>
<!DOCTYPE html>
<html lang="en">
@ -32,7 +32,7 @@
<nav>
<a href="/">Home</a>
<a href="/set">Set avatar</a>
{#if data.userid}
{#if data.user}
<a href="/logout">Log out</a>
{/if}
</nav>

View file

@ -6,5 +6,5 @@
</p>
<p>
If you'd like to set a profile picture, <a href="/set">click here</a>.
If logged out, you will be redirected to the OAuth2 provider of this instance.
If logged out, you will be redirected to the <abbr title="OpenID Connect">OIDC</abbr> provider of this instance.
</p>

View file

@ -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)
}

View file

@ -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)
}

View file

@ -1,8 +1,8 @@
<script lang="ts">
export let data: { userid: string };
export let data: { user: { sub: string, username: string } };
</script>
<h1>Set an avatar</h1>
<h1>Hi, {data.user.username}</h1>
<p>
Your identifier is {data.userid}.
Your identifier is {data.user.sub}.
</p>