[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
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<String, Long?> = 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<String, Long?>? = 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<String, String?> = 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<String, String?>? = 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<String, String?> = 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<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)
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<String>) -> 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<String>()
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 {