@@ -92,7 +92,8 @@ export function refreshSessionCookie(cookies: Cookies, sessionId: string) {
9292
9393export async function findUser (
9494 sessionId : string ,
95- coupledCookieHash ?: string
95+ coupledCookieHash : string | undefined ,
96+ url : URL
9697) : Promise < {
9798 user : User | null ;
9899 invalidateSession : boolean ;
@@ -121,7 +122,8 @@ export async function findUser(
121122 // Attempt to refresh the token
122123 const newTokenSet = await refreshOAuthToken (
123124 { redirectURI : `${ config . PUBLIC_ORIGIN } ${ base } /login/callback` } ,
124- session . oauth . refreshToken
125+ session . oauth . refreshToken ,
126+ url
125127 ) ;
126128
127129 if ( ! newTokenSet || ! newTokenSet . access_token ) {
@@ -236,7 +238,7 @@ export async function generateCsrfToken(
236238
237239let lastIssuer : Issuer < BaseClient > | null = null ;
238240let lastIssuerFetchedAt : Date | null = null ;
239- async function getOIDCClient ( settings : OIDCSettings ) : Promise < BaseClient > {
241+ async function getOIDCClient ( settings : OIDCSettings , url : URL ) : Promise < BaseClient > {
240242 if (
241243 lastIssuer &&
242244 lastIssuerFetchedAt &&
@@ -261,6 +263,13 @@ async function getOIDCClient(settings: OIDCSettings): Promise<BaseClient> {
261263 id_token_signed_response_alg : OIDConfig . ID_TOKEN_SIGNED_RESPONSE_ALG || undefined ,
262264 } ;
263265
266+ if ( OIDConfig . CLIENT_ID === "__CIMD__" ) {
267+ OIDConfig . CLIENT_ID = new URL (
268+ "/.well-known/oauth-cimd" ,
269+ config . PUBLIC_ORIGIN || url . origin
270+ ) . toString ( ) ;
271+ }
272+
264273 const alg_supported = issuer . metadata [ "id_token_signing_alg_values_supported" ] ;
265274
266275 if ( Array . isArray ( alg_supported ) ) {
@@ -272,9 +281,9 @@ async function getOIDCClient(settings: OIDCSettings): Promise<BaseClient> {
272281
273282export async function getOIDCAuthorizationUrl (
274283 settings : OIDCSettings ,
275- params : { sessionId : string ; next ?: string }
284+ params : { sessionId : string ; next ?: string ; url : URL }
276285) : Promise < string > {
277- const client = await getOIDCClient ( settings ) ;
286+ const client = await getOIDCClient ( settings , params . url ) ;
278287 const csrfToken = await generateCsrfToken (
279288 params . sessionId ,
280289 settings . redirectURI ,
@@ -291,9 +300,10 @@ export async function getOIDCAuthorizationUrl(
291300export async function getOIDCUserData (
292301 settings : OIDCSettings ,
293302 code : string ,
294- iss ?: string
303+ iss : string | undefined ,
304+ url : URL
295305) : Promise < OIDCUserInfo > {
296- const client = await getOIDCClient ( settings ) ;
306+ const client = await getOIDCClient ( settings , url ) ;
297307 const token = await client . callback ( settings . redirectURI , { code, iss } ) ;
298308 const userData = await client . userinfo ( token ) ;
299309
@@ -305,9 +315,10 @@ export async function getOIDCUserData(
305315 */
306316export async function refreshOAuthToken (
307317 settings : OIDCSettings ,
308- refreshToken : string
318+ refreshToken : string ,
319+ url : URL
309320) : Promise < TokenSet | null > {
310- const client = await getOIDCClient ( settings ) ;
321+ const client = await getOIDCClient ( settings , url ) ;
311322 const tokenSet = await client . refresh ( refreshToken ) ;
312323 return tokenSet ;
313324}
@@ -371,6 +382,7 @@ export async function getCoupledCookieHash(cookie: CookieRecord): Promise<string
371382export async function authenticateRequest (
372383 headers : HeaderRecord ,
373384 cookie : CookieRecord ,
385+ url : URL ,
374386 isApi ?: boolean
375387) : Promise < App . Locals & { secretSessionId : string } > {
376388 // once the entire API has been moved to elysia
@@ -415,7 +427,7 @@ export async function authenticateRequest(
415427 secretSessionId = token ;
416428 sessionId = await sha256 ( token ) ;
417429
418- const result = await findUser ( sessionId , await getCoupledCookieHash ( cookie ) ) ;
430+ const result = await findUser ( sessionId , await getCoupledCookieHash ( cookie ) , url ) ;
419431
420432 if ( result . invalidateSession ) {
421433 secretSessionId = crypto . randomUUID ( ) ;
@@ -539,7 +551,7 @@ export async function triggerOauthFlow({
539551
540552 const authorizationUrl = await getOIDCAuthorizationUrl (
541553 { redirectURI } ,
542- { sessionId : locals . sessionId , next }
554+ { sessionId : locals . sessionId , next, url }
543555 ) ;
544556
545557 throw redirect ( 302 , authorizationUrl ) ;
0 commit comments