Skip to content

Commit 7b7114b

Browse files
nicomtnsarrazin
andauthored
Simple Email Authorization (huggingface#801)
* Add ALLOWED_USER_EMAILS validation in login callback * Update src/routes/login/callback/+page.server.ts Adding email validation Co-authored-by: Nathan Sarrazin <[email protected]> * Update src/routes/login/callback/+page.server.ts More detailed error when missing email Co-authored-by: Nathan Sarrazin <[email protected]> --------- Co-authored-by: Nathan Sarrazin <[email protected]>
1 parent 10d7a5a commit 7b7114b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ ENABLE_ASSISTANTS=false #set to true to enable assistants feature
136136

137137
ALTERNATIVE_REDIRECT_URLS=`[]` #valide alternative redirect URL for OAuth
138138

139-
WEBHOOK_URL_REPORT_ASSISTANT=#provide webhook url to get notified when an assistant gets reported
139+
WEBHOOK_URL_REPORT_ASSISTANT=#provide webhook url to get notified when an assistant gets reported
140+
141+
ALLOWED_USER_EMAILS=`[]` # if it's defined, only these emails will be allowed to use the app

src/routes/login/callback/+page.server.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { getOIDCUserData, validateAndParseCsrfToken } from "$lib/server/auth";
33
import { z } from "zod";
44
import { base } from "$app/paths";
55
import { updateUser } from "./updateUser";
6+
import { ALLOWED_USER_EMAILS } from "$env/static/private";
7+
import JSON5 from "json5";
8+
9+
const allowedUserEmails = z
10+
.array(z.string().email())
11+
.optional()
12+
.default([])
13+
.parse(JSON5.parse(ALLOWED_USER_EMAILS));
614

715
export async function load({ url, locals, cookies, request, getClientAddress }) {
816
const { error: errorName, error_description: errorDescription } = z
@@ -33,6 +41,20 @@ export async function load({ url, locals, cookies, request, getClientAddress })
3341

3442
const { userData } = await getOIDCUserData({ redirectURI: validatedToken.redirectUrl }, code);
3543

44+
// Filter by allowed user emails
45+
if (allowedUserEmails.length > 0) {
46+
if (!userData.email) {
47+
throw error(403, "User not allowed: email not returned");
48+
}
49+
const emailVerified = userData.email_verified ?? true;
50+
if (!emailVerified) {
51+
throw error(403, "User not allowed: email not verified");
52+
}
53+
if (!allowedUserEmails.includes(userData.email)) {
54+
throw error(403, "User not allowed");
55+
}
56+
}
57+
3658
await updateUser({
3759
userData,
3860
locals,

0 commit comments

Comments
 (0)