Skip to content

Commit

Permalink
Simple Email Authorization (huggingface#801)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
nicomt and nsarrazin authored Feb 14, 2024
1 parent 10d7a5a commit 7b7114b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ ENABLE_ASSISTANTS=false #set to true to enable assistants feature

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

WEBHOOK_URL_REPORT_ASSISTANT=#provide webhook url to get notified when an assistant gets reported
WEBHOOK_URL_REPORT_ASSISTANT=#provide webhook url to get notified when an assistant gets reported

ALLOWED_USER_EMAILS=`[]` # if it's defined, only these emails will be allowed to use the app
22 changes: 22 additions & 0 deletions src/routes/login/callback/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { getOIDCUserData, validateAndParseCsrfToken } from "$lib/server/auth";
import { z } from "zod";
import { base } from "$app/paths";
import { updateUser } from "./updateUser";
import { ALLOWED_USER_EMAILS } from "$env/static/private";
import JSON5 from "json5";

const allowedUserEmails = z
.array(z.string().email())
.optional()
.default([])
.parse(JSON5.parse(ALLOWED_USER_EMAILS));

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

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

// Filter by allowed user emails
if (allowedUserEmails.length > 0) {
if (!userData.email) {
throw error(403, "User not allowed: email not returned");
}
const emailVerified = userData.email_verified ?? true;
if (!emailVerified) {
throw error(403, "User not allowed: email not verified");
}
if (!allowedUserEmails.includes(userData.email)) {
throw error(403, "User not allowed");
}
}

await updateUser({
userData,
locals,
Expand Down

0 comments on commit 7b7114b

Please sign in to comment.