From 3e8b3de2b7cd4306d8cf2b96685a57b08bdfb33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 23 Feb 2021 12:00:01 +0100 Subject: [PATCH] [Vulcan/Web] Move web data to loginStore per-symbol. --- .../data/api/edziennik/vulcan/DataVulcan.kt | 31 +++++++++---------- .../edziennik/vulcan/data/VulcanWebMain.kt | 26 ++++++++++------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt index 90ba3dd6..b822f660 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt @@ -16,12 +16,11 @@ import pl.szczodrzynski.edziennik.data.db.entity.Team import pl.szczodrzynski.edziennik.utils.Utils class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { - - fun isWebMainLoginValid() = webExpiryTime-30 > currentTimeUnix() - && webAuthCookie.isNotNullNorEmpty() + fun isWebMainLoginValid() = symbol.isNotNullNorEmpty() + && (webExpiryTime[symbol] ?: 0) - 30 > currentTimeUnix() + && webAuthCookie[symbol].isNotNullNorEmpty() && webHost.isNotNullNorEmpty() && webType.isNotNullNorEmpty() - && symbol.isNotNullNorEmpty() fun isApiLoginValid() = currentSemesterEndDate-30 > currentTimeUnix() && apiFingerprint[symbol].isNotNullNorEmpty() && apiPrivateKey[symbol].isNotNullNorEmpty() @@ -359,24 +358,24 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app * If the time passes, the certificate needs to be POSTed again (if valid) * or re-generated. */ - var webExpiryTime: Long - get() { mWebExpiryTime = mWebExpiryTime ?: profile?.getStudentData("webExpiryTime", 0L); return mWebExpiryTime ?: 0L } - set(value) { profile?.putStudentData("webExpiryTime", value); mWebExpiryTime = value } - private var mWebExpiryTime: Long? = null + var webExpiryTime: Map = mapOf() + get() { mWebExpiryTime = mWebExpiryTime ?: loginStore.getLoginData("webExpiryTime", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebExpiryTime ?: mapOf() } + set(value) { loginStore.putLoginData("webExpiryTime", app.gson.toJson(value)); mWebExpiryTime = value } + private var mWebExpiryTime: Map? = null /** * EfebSsoAuthCookie retrieved after posting a certificate */ - var webAuthCookie: String? - get() { mWebAuthCookie = mWebAuthCookie ?: profile?.getStudentData("webAuthCookie", null); return mWebAuthCookie } - set(value) { profile?.putStudentData("webAuthCookie", value); mWebAuthCookie = value } - private var mWebAuthCookie: String? = null + var webAuthCookie: Map = mapOf() + get() { mWebAuthCookie = mWebAuthCookie ?: loginStore.getLoginData("webAuthCookie", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebAuthCookie ?: mapOf() } + set(value) { loginStore.putLoginData("webAuthCookie", app.gson.toJson(value)); mWebAuthCookie = value } + private var mWebAuthCookie: Map? = null /** * Permissions needed to get JSONs from home page */ - var webPermissions: String? - get() { mWebPermissions = mWebPermissions ?: profile?.getStudentData("webPermissions", null); return mWebPermissions } - set(value) { profile?.putStudentData("webPermissions", value); mWebPermissions = value } - private var mWebPermissions: String? = null + var webPermissions: Map = mapOf() + get() { mWebPermissions = mWebPermissions ?: loginStore.getLoginData("webPermissions", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebPermissions ?: mapOf() } + set(value) { loginStore.putLoginData("webPermissions", app.gson.toJson(value)); mWebPermissions = value } + private var mWebPermissions: Map? = null } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanWebMain.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanWebMain.kt index 5d51839e..309aec70 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanWebMain.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanWebMain.kt @@ -83,10 +83,12 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { onResult(symbol, STATE_NO_REGISTER) return } - if (!validateCallback(text, response, jsonResponse = false)) { + if (!validateCallback(symbol, text, response, jsonResponse = false)) { return } - data.webExpiryTime = Date.fromIso(certificate.expiryDate) / 1000L + data.webExpiryTime = data.webExpiryTime.toMutableMap().also { map -> + map[symbol] = Date.fromIso(certificate.expiryDate) / 1000L + } onResult(symbol, STATE_SUCCESS) } @@ -120,7 +122,7 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { fun getStartPage(symbol: String = data.symbol ?: "default", postErrors: Boolean = true, onSuccess: (html: String, schoolSymbols: List) -> Unit) { val callback = object : TextCallbackHandler() { override fun onSuccess(text: String?, response: Response?) { - if (!validateCallback(text, response, jsonResponse = false) || text == null) { + if (!validateCallback(symbol, text, response, jsonResponse = false) || text == null) { return } @@ -136,7 +138,9 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { } } - data.webPermissions = Regexes.VULCAN_WEB_PERMISSIONS.find(text)?.let { it[1] } + data.webPermissions = data.webPermissions.toMutableMap().also { map -> + map[symbol] = Regexes.VULCAN_WEB_PERMISSIONS.find(text)?.let { it[1] } + } val schoolSymbols = mutableListOf() val clientUrl = "://uonetplus-uczen.${data.webHost}/$symbol/" @@ -186,7 +190,7 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { .enqueue() } - private fun validateCallback(text: String?, response: Response?, jsonResponse: Boolean = true): Boolean { + private fun validateCallback(symbol: String, text: String?, response: Response?, jsonResponse: Boolean = true): Boolean { if (text == null) { data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) .withResponse(response)) @@ -207,11 +211,13 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { val cookies = data.app.cookieJar.getAll(data.webHost ?: "vulcan.net.pl") val authCookie = cookies["EfebSsoAuthCookie"] - if ((authCookie == null || authCookie == "null") && data.webAuthCookie != null) { - data.app.cookieJar.set(data.webHost ?: "vulcan.net.pl", "EfebSsoAuthCookie", data.webAuthCookie) + if ((authCookie == null || authCookie == "null") && data.webAuthCookie[symbol] != null) { + data.app.cookieJar.set(data.webHost ?: "vulcan.net.pl", "EfebSsoAuthCookie", data.webAuthCookie[symbol]) } - else if (authCookie.isNotNullNorBlank() && authCookie != "null" && authCookie != data.webAuthCookie) { - data.webAuthCookie = authCookie + else if (authCookie.isNotNullNorBlank() && authCookie != "null" && authCookie != data.webAuthCookie[symbol]) { + data.webAuthCookie = data.webAuthCookie.toMutableMap().also { map -> + map[symbol] = authCookie + } } return true } @@ -250,7 +256,7 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) { val callback = object : TextCallbackHandler() { override fun onSuccess(text: String?, response: Response?) { - if (!validateCallback(text, response)) + if (!validateCallback(data.symbol ?: "default", text, response)) return try {