[Vulcan/Web] Move web data to loginStore per-symbol.

This commit is contained in:
Kuba Szczodrzyński 2021-02-23 12:00:01 +01:00
parent 85ac5769a1
commit 3e8b3de2b7
2 changed files with 31 additions and 26 deletions

View File

@ -16,12 +16,11 @@ import pl.szczodrzynski.edziennik.data.db.entity.Team
import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils
class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) {
fun isWebMainLoginValid() = symbol.isNotNullNorEmpty()
fun isWebMainLoginValid() = webExpiryTime-30 > currentTimeUnix() && (webExpiryTime[symbol] ?: 0) - 30 > currentTimeUnix()
&& webAuthCookie.isNotNullNorEmpty() && webAuthCookie[symbol].isNotNullNorEmpty()
&& webHost.isNotNullNorEmpty() && webHost.isNotNullNorEmpty()
&& webType.isNotNullNorEmpty() && webType.isNotNullNorEmpty()
&& symbol.isNotNullNorEmpty()
fun isApiLoginValid() = currentSemesterEndDate-30 > currentTimeUnix() fun isApiLoginValid() = currentSemesterEndDate-30 > currentTimeUnix()
&& apiFingerprint[symbol].isNotNullNorEmpty() && apiFingerprint[symbol].isNotNullNorEmpty()
&& apiPrivateKey[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) * If the time passes, the certificate needs to be POSTed again (if valid)
* or re-generated. * or re-generated.
*/ */
var webExpiryTime: Long var webExpiryTime: Map<String, Long?> = mapOf()
get() { mWebExpiryTime = mWebExpiryTime ?: profile?.getStudentData("webExpiryTime", 0L); return mWebExpiryTime ?: 0L } get() { mWebExpiryTime = mWebExpiryTime ?: loginStore.getLoginData("webExpiryTime", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebExpiryTime ?: mapOf() }
set(value) { profile?.putStudentData("webExpiryTime", value); mWebExpiryTime = value } set(value) { loginStore.putLoginData("webExpiryTime", app.gson.toJson(value)); mWebExpiryTime = value }
private var mWebExpiryTime: Long? = null private var mWebExpiryTime: Map<String, Long?>? = null
/** /**
* EfebSsoAuthCookie retrieved after posting a certificate * EfebSsoAuthCookie retrieved after posting a certificate
*/ */
var webAuthCookie: String? var webAuthCookie: Map<String, String?> = mapOf()
get() { mWebAuthCookie = mWebAuthCookie ?: profile?.getStudentData("webAuthCookie", null); return mWebAuthCookie } get() { mWebAuthCookie = mWebAuthCookie ?: loginStore.getLoginData("webAuthCookie", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebAuthCookie ?: mapOf() }
set(value) { profile?.putStudentData("webAuthCookie", value); mWebAuthCookie = value } set(value) { loginStore.putLoginData("webAuthCookie", app.gson.toJson(value)); mWebAuthCookie = value }
private var mWebAuthCookie: String? = null private var mWebAuthCookie: Map<String, String?>? = null
/** /**
* Permissions needed to get JSONs from home page * Permissions needed to get JSONs from home page
*/ */
var webPermissions: String? var webPermissions: Map<String, String?> = mapOf()
get() { mWebPermissions = mWebPermissions ?: profile?.getStudentData("webPermissions", null); return mWebPermissions } get() { mWebPermissions = mWebPermissions ?: loginStore.getLoginData("webPermissions", null)?.let { app.gson.fromJson(it, field.toMutableMap()::class.java) }; return mWebPermissions ?: mapOf() }
set(value) { profile?.putStudentData("webPermissions", value); mWebPermissions = value } set(value) { loginStore.putLoginData("webPermissions", app.gson.toJson(value)); mWebPermissions = value }
private var mWebPermissions: String? = null private var mWebPermissions: Map<String, String?>? = null
} }

View File

@ -83,10 +83,12 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) {
onResult(symbol, STATE_NO_REGISTER) onResult(symbol, STATE_NO_REGISTER)
return return
} }
if (!validateCallback(text, response, jsonResponse = false)) { if (!validateCallback(symbol, text, response, jsonResponse = false)) {
return 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) 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<String>) -> Unit) { fun getStartPage(symbol: String = data.symbol ?: "default", postErrors: Boolean = true, onSuccess: (html: String, schoolSymbols: List<String>) -> Unit) {
val callback = object : TextCallbackHandler() { val callback = object : TextCallbackHandler() {
override fun onSuccess(text: String?, response: Response?) { 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 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<String>() val schoolSymbols = mutableListOf<String>()
val clientUrl = "://uonetplus-uczen.${data.webHost}/$symbol/" val clientUrl = "://uonetplus-uczen.${data.webHost}/$symbol/"
@ -186,7 +190,7 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) {
.enqueue() .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) { if (text == null) {
data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY)
.withResponse(response)) .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 cookies = data.app.cookieJar.getAll(data.webHost ?: "vulcan.net.pl")
val authCookie = cookies["EfebSsoAuthCookie"] val authCookie = cookies["EfebSsoAuthCookie"]
if ((authCookie == null || authCookie == "null") && data.webAuthCookie != null) { if ((authCookie == null || authCookie == "null") && data.webAuthCookie[symbol] != null) {
data.app.cookieJar.set(data.webHost ?: "vulcan.net.pl", "EfebSsoAuthCookie", data.webAuthCookie) data.app.cookieJar.set(data.webHost ?: "vulcan.net.pl", "EfebSsoAuthCookie", data.webAuthCookie[symbol])
} }
else if (authCookie.isNotNullNorBlank() && authCookie != "null" && authCookie != data.webAuthCookie) { else if (authCookie.isNotNullNorBlank() && authCookie != "null" && authCookie != data.webAuthCookie[symbol]) {
data.webAuthCookie = authCookie data.webAuthCookie = data.webAuthCookie.toMutableMap().also { map ->
map[symbol] = authCookie
}
} }
return true return true
} }
@ -250,7 +256,7 @@ open class VulcanWebMain(open val data: DataVulcan, open val lastSync: Long?) {
val callback = object : TextCallbackHandler() { val callback = object : TextCallbackHandler() {
override fun onSuccess(text: String?, response: Response?) { override fun onSuccess(text: String?, response: Response?) {
if (!validateCallback(text, response)) if (!validateCallback(data.symbol ?: "default", text, response))
return return
try { try {