File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,17 +16,8 @@ export async function GET() {
1616 let code_verifier = client . randomPKCECodeVerifier ( )
1717 let code_challenge = await client . calculatePKCECodeChallenge ( code_verifier )
1818 const openIdClientConfig = await getClientConfig ( )
19- let tenantId = session . tenantId
20-
21- // Ensure tenantId is always a string and handle edge cases
22- if ( ! tenantId ||
23- tenantId === 'default' ||
24- ( typeof tenantId === 'object' && Object . keys ( tenantId ) . length === 0 ) ||
25- typeof tenantId !== 'string' ) {
26- tenantId = ''
27- } else {
28- tenantId = String ( tenantId )
29- }
19+ // Just use the saved tenantId (assumed sanitized at the time of saving)
20+ const tenantId = typeof session . tenantId === 'string' ? session . tenantId : ''
3021
3122 let parameters : Record < string , string > = {
3223 redirect_uri : clientConfig . redirect_uri ,
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { getSession } from '@/lib/actions'
33import { setUpLayoutConfig } from '@/lib/auth'
44import { headers } from 'next/headers'
55import { redirect } from 'next/navigation'
6+ import { isGuid } from '@/lib/session-utils'
67
78/**
89 * Handles the GET request to set the tenant for the current session.
@@ -27,8 +28,9 @@ export async function GET() {
2728 try {
2829 const { data } = await tenantGetTenantGuid ( { query : { host : host ! } } )
2930 console . log ( 'Fetched tenant GUID:' , data )
30- // Ensure tenantId is always a string
31- session . tenantId = data ? String ( data ) : ''
31+ const raw = data ? ( typeof data === 'string' ? data : String ( data ) ) : ''
32+ const candidate = raw . trim ( )
33+ session . tenantId = candidate && candidate !== 'default' && isGuid ( candidate ) ? candidate : ''
3234 } catch ( error ) {
3335 console . error ( 'Failed to fetch tenant GUID:' , error )
3436 session . tenantId = ''
Original file line number Diff line number Diff line change @@ -47,6 +47,15 @@ export async function getClientConfig() {
4747 return await client . discovery ( new URL ( clientConfig . url ! ) , clientConfig . client_id ! )
4848}
4949
50+ /**
51+ * Validates whether a string is a GUID (UUID v1-5).
52+ */
53+ export function isGuid ( value : string ) : boolean {
54+ return / ^ [ 0 - 9 a - f A - F ] { 8 } - [ 0 - 9 a - f A - F ] { 4 } - [ 1 - 5 ] [ 0 - 9 a - f A - F ] { 3 } - [ 8 9 a b A B ] [ 0 - 9 a - f A - F ] { 3 } - [ 0 - 9 a - f A - F ] { 12 } $ / . test (
55+ value
56+ )
57+ }
58+
5059/**
5160 * Sets the tenant ID in the session based on the provided host.
5261 * This function updates the session with the tenant ID associated with the given host.
@@ -71,11 +80,9 @@ export async function setTenantWithHost(host: string) {
7180 isObject : typeof data === 'object'
7281 } )
7382
74- // Ensure we store a string, not an object
75- if ( data ) {
76- session . tenantId = typeof data === 'string' ? data : String ( data )
77- } else {
78- session . tenantId = ''
79- }
83+ // Ensure we store a GUID or empty string
84+ const raw = data ? ( typeof data === 'string' ? data : String ( data ) ) : ''
85+ const candidate = raw . trim ( )
86+ session . tenantId = candidate && candidate !== 'default' && isGuid ( candidate ) ? candidate : ''
8087 await session . save ( )
8188}
You can’t perform that action at this time.
0 commit comments