package eu.friedcutlet.yokan.services.tracking import eu.friedcutlet.yokan.services.logging.AppLogger import eu.friedcutlet.yokan.services.system.AppDeepLinks /** * Builds tracker OAuth login URLs and listens for callback deep-links. Each tracker advertises a * redirect URI under the `yokan://` scheme that the Android manifest already routes back into * the app via [AppDeepLinks.dispatch]. * * Real OAuth requires client IDs/secrets which the port doesn't ship; the URL builders use the * documented endpoints so once a client is provisioned by the user they can paste credentials in * the Connect dialog and the callback will deliver `code`/`access_token`/`token` into * [onCallback]. */ object TrackerOAuth { private const val MalAuthorizeUrl = "https://myanimelist.net/v1/oauth2/authorize" private const val AnilistAuthorizeUrl = "https://anilist.co/api/v2/oauth/authorize" private const val KitsuAuthorizeUrl = "https://kitsu.io/users/sign_in" private const val SimklAuthorizeUrl = "https://simkl.com/oauth/authorize" private const val TraktAuthorizeUrl = "https://trakt.tv/oauth/authorize" private const val CallbackPrefix = "yokan://tracker" fun authorizeUrl(serviceSyncId: Int, clientId: String): String? { val redirect = redirectUri(serviceSyncId) return when (serviceSyncId) { MalTrackerSyncId -> "$MalAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect&code_challenge=yokan&code_challenge_method=plain" AnilistTrackerSyncId -> "$AnilistAuthorizeUrl?client_id=$clientId&response_type=token&redirect_uri=$redirect" KitsuTrackerSyncId -> "$KitsuAuthorizeUrl?redirect_to=$redirect" SimklTrackerSyncId -> "$SimklAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect" TraktTrackerSyncId -> "$TraktAuthorizeUrl?response_type=code&client_id=$clientId&redirect_uri=$redirect" else -> null } } fun redirectUri(serviceSyncId: Int): String = "$CallbackPrefix/${slug(serviceSyncId)}" fun listenForCallback(serviceSyncId: Int, onCallback: (TrackerOAuthResult) -> Unit): () -> Unit { val prefix = redirectUri(serviceSyncId) return AppDeepLinks.registerPrefix(prefix) { url -> val result = parseCallback(url) result.error?.let { AppLogger.warn("[tracking] ${slug(serviceSyncId)} OAuth callback returned error: $it") } onCallback(result) } } fun parseCallback(url: String): TrackerOAuthResult { val fragment = url.substringAfter('#', "") val query = url.substringAfter('?', "").substringBefore('#') val parts = (fragment + "&" + query).split('&').filter { it.contains('=') } val map = parts.associate { piece -> val key = piece.substringBefore('=').trim() val value = piece.substringAfter('=').trim() key to value } return TrackerOAuthResult( accessToken = map["access_token"] ?: map["token"], authorizationCode = map["code"], state = map["state"], error = map["error"], raw = url, ) } private fun slug(serviceSyncId: Int): String = when (serviceSyncId) { MalTrackerSyncId -> "myanimelist" AnilistTrackerSyncId -> "anilist" KitsuTrackerSyncId -> "kitsu" SimklTrackerSyncId -> "simkl" TraktTrackerSyncId -> "trakt" MangaUpdatesTrackerSyncId -> "mangaupdates" ShikimoriTrackerSyncId -> "shikimori" BangumiTrackerSyncId -> "bangumi" HikkaTrackerSyncId -> "hikka" MangaBakaTrackerSyncId -> "mangabaka" TmdbTrackerSyncId -> "tmdb" else -> "unknown" } } data class TrackerOAuthResult( val accessToken: String?, val authorizationCode: String?, val state: String?, val error: String?, val raw: String, )