|
| 1 | +// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL |
| 2 | + |
| 3 | +package com.metallic.chiaki.cloudplay |
| 4 | + |
| 5 | +import android.util.Log |
| 6 | +import com.metallic.chiaki.cloudplay.api.HttpClient |
| 7 | +import com.metallic.chiaki.common.Preferences |
| 8 | +import org.json.JSONObject |
| 9 | + |
| 10 | +object CloudLocaleBootstrap |
| 11 | +{ |
| 12 | + private const val TAG = "CloudLocaleBootstrap" |
| 13 | + private val lock = Any() |
| 14 | + |
| 15 | + fun ensureConfigured(preferences: Preferences, npssoToken: String): Boolean |
| 16 | + { |
| 17 | + if (preferences.isCloudLanguageConfigured()) |
| 18 | + return true |
| 19 | + if (npssoToken.isBlank()) |
| 20 | + { |
| 21 | + Log.w(TAG, "Cannot bootstrap locale: empty npsso token") |
| 22 | + return false |
| 23 | + } |
| 24 | + |
| 25 | + synchronized(lock) |
| 26 | + { |
| 27 | + if (preferences.isCloudLanguageConfigured()) |
| 28 | + return true |
| 29 | + |
| 30 | + Log.i(TAG, "Bootstrapping cloud locale via Kamaji session (first time only)") |
| 31 | + return runBootstrap(preferences, npssoToken) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private fun runBootstrap(preferences: Preferences, npssoToken: String): Boolean |
| 36 | + { |
| 37 | + return try |
| 38 | + { |
| 39 | + val duid = DuidUtil.generateDuid() |
| 40 | + val oauthCode = fetchOAuthCode(npssoToken, duid) ?: run { |
| 41 | + Log.w(TAG, "Locale bootstrap failed: OAuth") |
| 42 | + return false |
| 43 | + } |
| 44 | + if (!createKamajiSessionAndSaveLocale(preferences, oauthCode, duid)) |
| 45 | + { |
| 46 | + Log.w(TAG, "Locale bootstrap failed: Kamaji session") |
| 47 | + return false |
| 48 | + } |
| 49 | + Log.i(TAG, "Locale bootstrap OK: ${preferences.getCloudLanguage()}") |
| 50 | + true |
| 51 | + } |
| 52 | + catch (e: Exception) |
| 53 | + { |
| 54 | + Log.w(TAG, "Locale bootstrap error", e) |
| 55 | + false |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fun fetchOAuthCode(npssoToken: String, duid: String): String? |
| 60 | + { |
| 61 | + val uri = android.net.Uri.parse("${PsnApiConstants.ACCOUNT_BASE}/v1/oauth/authorize") |
| 62 | + .buildUpon() |
| 63 | + .appendQueryParameter("smcid", "pc:psnow") |
| 64 | + .appendQueryParameter("applicationId", "psnow") |
| 65 | + .appendQueryParameter("response_type", "code") |
| 66 | + .appendQueryParameter("scope", PsnApiConstants.PS4_SCOPES) |
| 67 | + .appendQueryParameter("client_id", PsnApiConstants.CLIENT_ID) |
| 68 | + .appendQueryParameter("redirect_uri", PsnApiConstants.REDIRECT_URI) |
| 69 | + .appendQueryParameter("service_entity", "urn:service-entity:psn") |
| 70 | + .appendQueryParameter("prompt", "none") |
| 71 | + .appendQueryParameter("renderMode", "mobilePortrait") |
| 72 | + .appendQueryParameter("hidePageElements", "forgotPasswordLink") |
| 73 | + .appendQueryParameter("displayFooter", "none") |
| 74 | + .appendQueryParameter("disableLinks", "qriocityLink") |
| 75 | + .appendQueryParameter("mid", "PSNOW") |
| 76 | + .appendQueryParameter("duid", duid) |
| 77 | + .appendQueryParameter("layout_type", "popup") |
| 78 | + .appendQueryParameter("service_logo", "ps") |
| 79 | + .appendQueryParameter("tp_psn", "true") |
| 80 | + .appendQueryParameter("noEVBlock", "true") |
| 81 | + .build() |
| 82 | + |
| 83 | + val response = HttpClient.get(uri.toString(), mapOf("Cookie" to "npsso=$npssoToken"), followRedirects = false) |
| 84 | + if (response.statusCode != 302) |
| 85 | + return null |
| 86 | + |
| 87 | + val location = HttpClient.extractLocation(response.headers) ?: return null |
| 88 | + val match = Regex("[?&]code=([^&]+)").find(location) ?: return null |
| 89 | + return match.groupValues.getOrNull(1)?.takeIf { it.isNotEmpty() } |
| 90 | + } |
| 91 | + |
| 92 | + private fun createKamajiSessionAndSaveLocale( |
| 93 | + preferences: Preferences, |
| 94 | + oauthCode: String, |
| 95 | + duid: String |
| 96 | + ): Boolean |
| 97 | + { |
| 98 | + val url = "${PsnApiConstants.KAMAJI_BASE}/user/session" |
| 99 | + val body = "code=$oauthCode&client_id=${PsnApiConstants.CLIENT_ID}&duid=$duid" |
| 100 | + val headers = mapOf( |
| 101 | + "Content-Type" to "text/plain;charset=UTF-8", |
| 102 | + "X-Alt-Referer" to PsnApiConstants.REDIRECT_URI, |
| 103 | + "Origin" to PsnApiConstants.ORIGIN, |
| 104 | + "Referer" to PsnApiConstants.REFERER, |
| 105 | + "Accept" to "*/*" |
| 106 | + ) |
| 107 | + |
| 108 | + val response = HttpClient.post(url, body, headers) |
| 109 | + if (response.statusCode != 200) |
| 110 | + return false |
| 111 | + |
| 112 | + val json = JSONObject(response.body) |
| 113 | + if (json.optJSONObject("header")?.optString("status_code") != "0x0000") |
| 114 | + return false |
| 115 | + |
| 116 | + val data = json.optJSONObject("data") |
| 117 | + preferences.setCloudLanguageFromSession( |
| 118 | + data?.optString("language"), |
| 119 | + data?.optString("country") |
| 120 | + ) |
| 121 | + return preferences.isCloudLanguageConfigured() |
| 122 | + } |
| 123 | +} |
0 commit comments