TrackerOAuth.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/tracking/TrackerOAuth.kt
1package eu.friedcutlet.yokan.services.tracking23import eu.friedcutlet.yokan.services.logging.AppLogger4import eu.friedcutlet.yokan.services.system.AppDeepLinks56/**7 * Builds tracker OAuth login URLs and listens for callback deep-links. Each tracker advertises a8 * redirect URI under the `yokan://` scheme that the Android manifest already routes back into9 * the app via [AppDeepLinks.dispatch].10 *11 * Real OAuth requires client IDs/secrets which the port doesn't ship; the URL builders use the12 * documented endpoints so once a client is provisioned by the user they can paste credentials in13 * the Connect dialog and the callback will deliver `code`/`access_token`/`token` into14 * [onCallback].15 */16object TrackerOAuth {17 private const val MalAuthorizeUrl = "https://myanimelist.net/v1/oauth2/authorize"18 private const val AnilistAuthorizeUrl = "https://anilist.co/api/v2/oauth/authorize"19 private const val KitsuAuthorizeUrl = "https://kitsu.io/users/sign_in"20 private const val SimklAuthorizeUrl = "https://simkl.com/oauth/authorize"21 private const val TraktAuthorizeUrl = "https://trakt.tv/oauth/authorize"2223 private const val CallbackPrefix = "yokan://tracker"2425 fun authorizeUrl(serviceSyncId: Int, clientId: String): String? {26 val redirect = redirectUri(serviceSyncId)27 return when (serviceSyncId) {28 MalTrackerSyncId -> "$MalAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect&code_challenge=yokan&code_challenge_method=plain"29 AnilistTrackerSyncId -> "$AnilistAuthorizeUrl?client_id=$clientId&response_type=token&redirect_uri=$redirect"30 KitsuTrackerSyncId -> "$KitsuAuthorizeUrl?redirect_to=$redirect"31 SimklTrackerSyncId -> "$SimklAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect"32 TraktTrackerSyncId -> "$TraktAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect"33 else -> null34 }35 }3637 fun redirectUri(serviceSyncId: Int): String = "$CallbackPrefix/${slug(serviceSyncId)}"3839 fun listenForCallback(serviceSyncId: Int, onCallback: (TrackerOAuthResult) -> Unit): () -> Unit {40 val prefix = redirectUri(serviceSyncId)41 return AppDeepLinks.registerPrefix(prefix) { url ->42 val result = parseCallback(url)43 result.error?.let { AppLogger.warn("[tracking] ${slug(serviceSyncId)} OAuth callback returned error: $it") }44 onCallback(result)45 }46 }4748 fun parseCallback(url: String): TrackerOAuthResult {49 val fragment = url.substringAfter('#', "")50 val query = url.substringAfter('?', "").substringBefore('#')51 val parts = (fragment + "&" + query).split('&').filter { it.contains('=') }52 val map = parts.associate { piece ->53 val key = piece.substringBefore('=').trim()54 val value = piece.substringAfter('=').trim()55 key to value56 }57 return TrackerOAuthResult(58 accessToken = map["access_token"] ?: map["token"],59 authorizationCode = map["code"],60 state = map["state"],61 error = map["error"],62 raw = url,63 )64 }6566 private fun slug(serviceSyncId: Int): String = when (serviceSyncId) {67 MalTrackerSyncId -> "myanimelist"68 AnilistTrackerSyncId -> "anilist"69 KitsuTrackerSyncId -> "kitsu"70 SimklTrackerSyncId -> "simkl"71 TraktTrackerSyncId -> "trakt"72 MangaUpdatesTrackerSyncId -> "mangaupdates"73 ShikimoriTrackerSyncId -> "shikimori"74 BangumiTrackerSyncId -> "bangumi"75 HikkaTrackerSyncId -> "hikka"76 MangaBakaTrackerSyncId -> "mangabaka"77 TmdbTrackerSyncId -> "tmdb"78 else -> "unknown"79 }80}8182data class TrackerOAuthResult(83 val accessToken: String?,84 val authorizationCode: String?,85 val state: String?,86 val error: String?,87 val raw: String,88)89