@@ -68,7 +68,7 @@ oidcRouter.get('/dlrg/callback', async (c) => {
6868 Authorization : `Bearer ${ result . access_token } ` ,
6969 } ,
7070 } )
71- const profileRaw = await userInfoResponse . json ( ) as Record < string , unknown >
71+ const profileRaw = ( await userInfoResponse . json ( ) ) as Record < string , unknown >
7272 const profile = ZProfile . parse ( profileRaw )
7373 const existingUser = await prisma . account . findUnique ( {
7474 where : {
@@ -136,16 +136,41 @@ oidcRouter.get('/dlrg/login', async (c) => {
136136 const as = await oauth . processDiscoveryResponse ( issuer , discoveryRequestResponse )
137137 const authorizationUrl = new URL ( as . authorization_endpoint ! )
138138
139+ const requestHost = c . req . header ( 'Host' ) ?. trim ( ) . toLowerCase ( )
140+ if ( ! requestHost ) {
141+ return c . text ( 'Invalid domain' , 400 )
142+ }
143+
144+ // Accept hostnames stored as plain host, with port, or as full URL.
145+ const hostFromHeader = new URL ( `http://${ requestHost } ` )
146+ const candidateHostnames = Array . from (
147+ new Set ( [
148+ requestHost ,
149+ hostFromHeader . host ,
150+ hostFromHeader . hostname ,
151+ `https://${ hostFromHeader . host } ` ,
152+ `https://${ hostFromHeader . hostname } ` ,
153+ `http://${ hostFromHeader . host } ` ,
154+ `http://${ hostFromHeader . hostname } ` ,
155+ ] )
156+ )
157+
139158 const host = await prisma . hostname . findFirst ( {
140159 where : {
141- hostname : c . req . header ( 'Host' ) ,
142- }
160+ hostname : {
161+ in : candidateHostnames ,
162+ } ,
163+ } ,
143164 } )
144165 if ( host === null ) {
145166 return c . text ( 'Invalid domain' , 400 )
146167 }
147168
148- const redirectUri = new URL ( '/api/connect/dlrg/callback' , host . hostname )
169+ const redirectBase =
170+ host . hostname . startsWith ( 'http://' ) || host . hostname . startsWith ( 'https://' )
171+ ? host . hostname
172+ : `https://${ host . hostname } `
173+ const redirectUri = new URL ( '/api/connect/dlrg/callback' , redirectBase )
149174 const registerAs = c . req . query ( 'as' ) ?. trim ( )
150175 if ( registerAs !== undefined && registerAs ?. length > 0 ) {
151176 redirectUri . searchParams . set ( 'as' , registerAs )
0 commit comments