@@ -19,6 +19,62 @@ const exchangeGrantSchema = z.object({
1919 grant : z . string ( ) . trim ( ) . min ( 12 ) . max ( 128 ) ,
2020} )
2121
22+ function readSingleHeader ( value : unknown ) : string | null {
23+ if ( typeof value !== "string" ) {
24+ return null
25+ }
26+ const first = value . split ( "," ) [ 0 ] ?. trim ( ) ?? ""
27+ return first || null
28+ }
29+
30+ function isWebAppHost ( hostname : string ) : boolean {
31+ const normalized = hostname . trim ( ) . toLowerCase ( )
32+ return normalized === "app.openworklabs.com"
33+ || normalized === "app.openwork.software"
34+ || normalized . startsWith ( "app." )
35+ }
36+
37+ function withDenProxyPath ( origin : string ) : string {
38+ const url = new URL ( origin )
39+ const pathname = url . pathname . replace ( / \/ + $ / , "" )
40+ if ( pathname . toLowerCase ( ) . endsWith ( "/api/den" ) ) {
41+ return url . toString ( ) . replace ( / \/ + $ / , "" )
42+ }
43+ url . pathname = `${ pathname } /api/den` . replace ( / \/ + / g, "/" )
44+ return url . toString ( ) . replace ( / \/ + $ / , "" )
45+ }
46+
47+ function resolveDesktopDenBaseUrl ( req : express . Request ) : string {
48+ const originHeader = readSingleHeader ( req . headers . origin )
49+ if ( originHeader ) {
50+ try {
51+ const originUrl = new URL ( originHeader )
52+ if ( ( originUrl . protocol === "https:" || originUrl . protocol === "http:" ) && isWebAppHost ( originUrl . hostname ) ) {
53+ return withDenProxyPath ( originUrl . origin )
54+ }
55+ } catch { }
56+ }
57+
58+ const forwardedProto = readSingleHeader ( req . headers [ "x-forwarded-proto" ] )
59+ const forwardedHost = readSingleHeader ( req . headers [ "x-forwarded-host" ] )
60+ const host = readSingleHeader ( req . headers . host )
61+ const protocol = forwardedProto ?? req . protocol ?? "https"
62+ const targetHost = forwardedHost ?? host
63+ if ( ! targetHost ) {
64+ return "https://app.openworklabs.com/api/den"
65+ }
66+
67+ const origin = `${ protocol } ://${ targetHost } `
68+ try {
69+ const url = new URL ( origin )
70+ if ( isWebAppHost ( url . hostname ) ) {
71+ return withDenProxyPath ( url . origin )
72+ }
73+ } catch { }
74+
75+ return origin
76+ }
77+
2278function buildOpenworkDeepLink ( input : { scheme ?: string | null ; grant : string ; denBaseUrl : string } ) {
2379 const requestedScheme = input . scheme ?. trim ( ) || "openwork"
2480 const scheme = / ^ [ a - z ] [ a - z 0 - 9 + . - ] * $ / i. test ( requestedScheme ) ? requestedScheme : "openwork"
@@ -51,10 +107,7 @@ desktopAuthRouter.post("/desktop-handoff", asyncRoute(async (req, res) => {
51107 consumed_at : null ,
52108 } )
53109
54- const forwardedProto = typeof req . headers [ "x-forwarded-proto" ] === "string" ? req . headers [ "x-forwarded-proto" ] : null
55- const host = typeof req . headers . host === "string" ? req . headers . host : null
56- const protocol = forwardedProto ?? req . protocol ?? "https"
57- const denBaseUrl = host ? `${ protocol } ://${ host } ` : "https://app.openworklabs.com"
110+ const denBaseUrl = resolveDesktopDenBaseUrl ( req )
58111 res . json ( {
59112 grant,
60113 expiresAt : expiresAt . toISOString ( ) ,
0 commit comments