Skip to content

Commit e82a53a

Browse files
authored
add fallback discovery for oauth (calcom#25656)
1 parent a6578fd commit e82a53a

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

companion/services/oauthService.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,37 @@ export class CalComOAuthService {
290290
}
291291

292292
private async getDiscoveryEndpoints(): Promise<AuthSession.DiscoveryDocument> {
293+
const fallbackDiscovery: AuthSession.DiscoveryDocument = {
294+
authorizationEndpoint: `${this.config.calcomBaseUrl}/auth/oauth2/authorize`,
295+
tokenEndpoint: `${this.config.calcomBaseUrl}/api/auth/oauth/token`,
296+
revocationEndpoint: `${this.config.calcomBaseUrl}/api/auth/oauth/revoke`,
297+
};
298+
299+
const isCrossOriginWeb =
300+
Platform.OS === "web" &&
301+
typeof window !== "undefined" &&
302+
(() => {
303+
try {
304+
return new URL(this.config.calcomBaseUrl).origin !== window.location.origin;
305+
} catch {
306+
return true;
307+
}
308+
})();
309+
310+
// Skip discovery fetch when we know CORS will block it (e.g. companion.cal.com -> app.cal.com).
311+
if (isCrossOriginWeb) {
312+
return fallbackDiscovery;
313+
}
314+
293315
try {
294316
const discovery = await AuthSession.fetchDiscoveryAsync(this.config.calcomBaseUrl);
295-
return discovery;
296-
} catch {
297317
return {
298-
authorizationEndpoint: `${this.config.calcomBaseUrl}/auth/oauth2/authorize`,
299-
tokenEndpoint: `${this.config.calcomBaseUrl}/api/auth/oauth/token`,
300-
} as AuthSession.DiscoveryDocument;
318+
...fallbackDiscovery,
319+
...discovery,
320+
};
321+
} catch (error) {
322+
console.warn("Failed to load discovery document, using fallback endpoints", error);
323+
return fallbackDiscovery;
301324
}
302325
}
303326

0 commit comments

Comments
 (0)