From ae20c30c881c3956a96c01d1deb0432a2d1de7a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 20 Feb 2021 21:31:52 +0100 Subject: [PATCH] [Vulcan/Hebe] Fix Firebase token for registration. --- .../data/api/edziennik/vulcan/data/VulcanHebe.kt | 7 ++++++- .../api/edziennik/vulcan/login/VulcanLoginHebe.kt | 12 +++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt index 42e845f6..dd8913f1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt @@ -42,6 +42,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { method: Int = GET, payload: JsonElement? = null, baseUrl: Boolean = false, + firebaseToken: String? = null, crossinline onSuccess: (json: T, response: Response?) -> Unit ) { val url = "${if (baseUrl) data.apiUrl else data.fullApiUrl}$endpoint" @@ -66,7 +67,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { "AppVersion" to VULCAN_HEBE_APP_VERSION, "CertificateId" to publicHash, "Envelope" to payload, - "FirebaseToken" to data.app.config.sync.tokenVulcanHebe, + "FirebaseToken" to (firebaseToken ?: data.app.config.sync.tokenVulcanHebe), "API" to 1, "RequestId" to UUID.randomUUID().toString(), "Timestamp" to timestampMillis, @@ -164,6 +165,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { endpoint: String, query: Map = mapOf(), baseUrl: Boolean = false, + firebaseToken: String? = null, crossinline onSuccess: (json: T, response: Response?) -> Unit ) { val queryPath = query.map { it.key + "=" + URLEncoder.encode(it.value, "UTF-8") }.join("&") @@ -171,6 +173,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { tag, if (query.isNotEmpty()) "$endpoint?$queryPath" else endpoint, baseUrl = baseUrl, + firebaseToken = firebaseToken, onSuccess = onSuccess ) } @@ -180,6 +183,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { endpoint: String, payload: JsonElement, baseUrl: Boolean = false, + firebaseToken: String? = null, crossinline onSuccess: (json: T, response: Response?) -> Unit ) { apiRequest( @@ -188,6 +192,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { method = POST, payload, baseUrl = baseUrl, + firebaseToken = firebaseToken, onSuccess = onSuccess ) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/login/VulcanLoginHebe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/login/VulcanLoginHebe.kt index 750fd4ea..1308f449 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/login/VulcanLoginHebe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/login/VulcanLoginHebe.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.data.api.VULCAN_HEBE_ENDPOINT_REGISTER_NEW import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanHebe import pl.szczodrzynski.edziennik.data.api.models.ApiError +import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.getString import pl.szczodrzynski.edziennik.isNotNullNorEmpty @@ -63,7 +64,7 @@ class VulcanLoginHebe(val data: DataVulcan, val onSuccess: () -> Unit) { } private fun loginWithToken() { - //val szkolnyApi = SzkolnyApi(data.app) + val szkolnyApi = SzkolnyApi(data.app) val hebe = VulcanHebe(data, null) if (data.hebePublicKey == null || data.hebePrivateKey == null || data.hebePublicHash == null) { @@ -73,11 +74,11 @@ class VulcanLoginHebe(val data: DataVulcan, val onSuccess: () -> Unit) { data.hebePublicHash = publicHash } - /*szkolnyApi.runCatching({ - data.app.config.sync.tokenVulcanHebe = getFirebaseToken("vulcan") + val firebaseToken = szkolnyApi.runCatching({ + getFirebaseToken("vulcan") }, onError = { // screw errors - })*/ + }) ?: data.app.config.sync.tokenVulcan hebe.apiPost( TAG, @@ -92,7 +93,8 @@ class VulcanLoginHebe(val data: DataVulcan, val onSuccess: () -> Unit) { "SelfIdentifier" to data.buildDeviceId(), "CertificateThumbprint" to data.hebePublicHash ), - baseUrl = true + baseUrl = true, + firebaseToken = firebaseToken ) { _: JsonObject, _ -> data.apiToken = data.apiToken.toMutableMap().also { it[data.symbol] = it[data.symbol]?.substring(0, 3)