[APIv2/Librus] Update Librus Synergia login method. Add Librus Messages login method.

This commit is contained in:
Kuba Szczodrzyński 2019-09-23 09:02:59 +02:00
parent a785db4d47
commit e95d9ee514
3 changed files with 67 additions and 17 deletions

View File

@ -137,6 +137,25 @@ class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app
get() { mSynergiaSessionIdExpiryTime = mSynergiaSessionIdExpiryTime ?: profile?.getStudentData("accountSIDTime", 0L); return mSynergiaSessionIdExpiryTime ?: 0L }
set(value) { profile?.putStudentData("accountSIDTime", value) ?: return; mSynergiaSessionIdExpiryTime = value }
/**
* A Messages web Session ID (DZIENNIKSID).
* Used in endpoints with Messages login method.
*/
private var mMessagesSessionId: String? = null
var messagesSessionId: String?
get() { mMessagesSessionId = mMessagesSessionId ?: profile?.getStudentData("messagesSID", null); return mMessagesSessionId }
set(value) { profile?.putStudentData("messagesSID", value) ?: return; mMessagesSessionId = value }
/**
* The expiry time for [messagesSessionId], as a UNIX timestamp.
* Used in endpoints with Messages login method.
* TODO verify how long is the session ID valid.
*/
private var mMessagesSessionIdExpiryTime: Long? = null
var messagesSessionIdExpiryTime: Long
get() { mMessagesSessionIdExpiryTime = mMessagesSessionIdExpiryTime ?: profile?.getStudentData("messagesSIDTime", 0L); return mMessagesSessionIdExpiryTime ?: 0L }
set(value) { profile?.putStudentData("messagesSIDTime", value) ?: return; mMessagesSessionIdExpiryTime = value }
/* ____ _ _
/ __ \| | | |
| | | | |_| |__ ___ _ __

View File

@ -4,5 +4,43 @@
package pl.szczodrzynski.edziennik.api.v2.librus.login
class LoginLibrusMessages {
import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus
import pl.szczodrzynski.edziennik.currentTimeUnix
import pl.szczodrzynski.edziennik.isNotNullNorEmpty
class LoginLibrusMessages(val data: DataLibrus, val onSuccess: () -> Unit) {
companion object {
private const val TAG = "LoginLibrusMessages"
}
init { run {
if (data.profile == null) {
data.error(TAG, ERROR_PROFILE_MISSING)
return@run
}
if (data.messagesSessionIdExpiryTime-30 > currentTimeUnix() && data.messagesSessionId.isNotNullNorEmpty()) {
onSuccess()
}
else {
when (data.loginStore.mode) {
LOGIN_MODE_LIBRUS_SYNERGIA -> loginWithCredentials()
else -> {
loginWithSynergia()
}
}
}
}}
private fun loginWithCredentials() {
if (data.apiLogin == null || data.apiPassword == null) {
if (!data.loginMethods.contains(LOGIN_METHOD_LIBRUS_SYNERGIA)) {
data.error(TAG, ERROR_LOGIN_METHOD_NOT_SATISFIED)
return
}
data.error(TAG, ERROR_LOGIN_DATA_MISSING)
return
}
}
}

View File

@ -7,7 +7,6 @@ package pl.szczodrzynski.edziennik.api.v2.librus.login
import com.google.gson.JsonObject
import im.wangchao.mhttp.Request
import im.wangchao.mhttp.Response
import im.wangchao.mhttp.body.MediaTypeUtils
import im.wangchao.mhttp.callback.JsonCallbackHandler
import im.wangchao.mhttp.callback.TextCallbackHandler
import okhttp3.HttpUrl
@ -34,11 +33,14 @@ class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) {
onSuccess()
}
else {
when (data.loginStore.mode) {
LOGIN_MODE_LIBRUS_SYNERGIA -> loginWithSynergia()
else -> {
if (data.loginMethods.contains(LOGIN_METHOD_LIBRUS_API)) {
loginWithApi()
}
else if (data.apiLogin != null && data.apiPassword != null) {
loginWithCredentials()
}
else {
data.error(TAG, ERROR_LOGIN_DATA_MISSING)
}
}
}}
@ -46,11 +48,7 @@ class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) {
/**
* HTML form-based login method. Uses a Synergia login and password.
*/
private fun loginWithSynergia() {
if (data.apiLogin == null || data.apiPassword == null) {
data.error(TAG, ERROR_LOGIN_DATA_MISSING)
return
}
private fun loginWithCredentials() {
}
@ -58,11 +56,6 @@ class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) {
* A login method using the Synergia API (AutoLoginToken endpoint).
*/
private fun loginWithApi() {
if (!data.loginMethods.contains(LOGIN_METHOD_LIBRUS_API)) {
data.error(TAG, ERROR_LOGIN_METHOD_NOT_SATISFIED)
return
}
val onSuccess = { json: JsonObject ->
loginWithToken(json.getString("Token"))
}