From be8fd38c59e8c869f1e86a9404a8ae047b0d4119 Mon Sep 17 00:00:00 2001 From: syui Date: Tue, 17 Jun 2025 14:13:11 +0900 Subject: [PATCH] fix --- oauth/src/services/atproto-oauth.ts | 40 ++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/oauth/src/services/atproto-oauth.ts b/oauth/src/services/atproto-oauth.ts index f23e86e..a78f138 100644 --- a/oauth/src/services/atproto-oauth.ts +++ b/oauth/src/services/atproto-oauth.ts @@ -41,6 +41,7 @@ class AtprotoOAuthService { this.oauthClient = await BrowserOAuthClient.load({ clientId: clientId, handleResolver: 'https://bsky.social', // Default resolver + plcDirectoryUrl: 'https://plc.directory', // Default PLC directory }); @@ -176,20 +177,43 @@ class AtprotoOAuthService { return `${origin}/client-metadata.json`; } - private detectPDSFromHandle(handle: string): string { - // Supported PDS hosts and their corresponding handles + private async detectPDSFromHandle(handle: string): Promise { + // Handle detection for OAuth PDS routing + + // Check if handle ends with known PDS domains first const pdsMapping = { 'syu.is': 'https://syu.is', 'bsky.social': 'https://bsky.social', }; - // Check if handle ends with known PDS domains for (const [domain, pdsUrl] of Object.entries(pdsMapping)) { if (handle.endsWith(`.${domain}`)) { return pdsUrl; } } + // For handles that don't match domain patterns, resolve via API + try { + // Try to resolve handle to get the actual PDS + const endpoints = ['https://syu.is', 'https://bsky.social']; + + for (const endpoint of endpoints) { + try { + const response = await fetch(`${endpoint}/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(handle)}`); + if (response.ok) { + const data = await response.json(); + if (data.did) { + return endpoint; + } + } + } catch (e) { + continue; + } + } + } catch (e) { + // Handle resolution failed, using default + } + // Default to bsky.social return 'https://bsky.social'; } @@ -217,9 +241,13 @@ class AtprotoOAuthService { // Re-initialize OAuth client with correct PDS if needed if (pdsUrl !== 'https://bsky.social') { + // Determine PLC directory for syu.is only + const plcDirectoryUrl = handle.endsWith('.syu.is') || handle.endsWith('.syui.ai') ? 'https://plc.syu.is' : 'https://plc.directory'; + this.oauthClient = await BrowserOAuthClient.load({ clientId: this.getClientId(), handleResolver: pdsUrl, + plcDirectoryUrl: plcDirectoryUrl, }); } @@ -591,10 +619,4 @@ class AtprotoOAuthService { } export const atprotoOAuthService = new AtprotoOAuthService(); - -// Debug: Make service available globally for testing -if (typeof window !== 'undefined') { - (window as any).atprotoOAuthService = atprotoOAuthService; -} - export type { AtprotoSession };