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

View file

@ -6,5 +6,5 @@
</p> </p>
<p> <p>
If you'd like to set a profile picture, <a href="/set">click here</a>. 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> </p>

View file

@ -1,5 +1,6 @@
import configuration from "$lib/configuration.js";
import { redirect } from "@sveltejs/kit"; import { redirect } from "@sveltejs/kit";
export function load({}) { export function load({}) {
throw redirect(301, "/") throw redirect(301, configuration.oauth2.endpoints.logout)
} }

View file

@ -1,4 +1,7 @@
export function load({ cookies }) { import {launchLogin} from "$lib"
let token = cookies.get("accessToken") 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"> <script lang="ts">
export let data: { userid: string }; export let data: { user: { sub: string, username: string } };
</script> </script>
<h1>Set an avatar</h1> <h1>Hi, {data.user.username}</h1>
<p> <p>
Your identifier is {data.userid}. Your identifier is {data.user.sub}.
</p> </p>