From 4cbb573d1729537d999eb16519597f58d0edc442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 24 Sep 2019 20:12:25 +0200 Subject: [PATCH] [APIv2/Librus] Update Librus login methods. --- .../edziennik/api/v2/librus/LibrusHelpers.kt | 6 ---- .../api/v2/librus/data/DataLibrus.kt | 23 ++++++++++++- .../api/v2/librus/login/LoginLibrusApi.kt | 2 +- .../v2/librus/login/LoginLibrusMessages.kt | 34 +++++++++++-------- .../api/v2/librus/login/LoginLibrusPortal.kt | 2 +- .../v2/librus/login/LoginLibrusSynergia.kt | 3 +- .../edziennik/api/v2/models/Data.kt | 8 +++++ 7 files changed, 54 insertions(+), 24 deletions(-) delete mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusHelpers.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusHelpers.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusHelpers.kt deleted file mode 100644 index 7096c1d0..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusHelpers.kt +++ /dev/null @@ -1,6 +0,0 @@ -/* - * Copyright (c) Kuba SzczodrzyƄski 2019-9-21. - */ - -package pl.szczodrzynski.edziennik.api.v2.librus - diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt index 139545cc..b6aa2a4e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt @@ -5,13 +5,34 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.api.interfaces.ProgressCallback +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_MESSAGES +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_PORTAL +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_SYNERGIA import pl.szczodrzynski.edziennik.api.v2.models.Data +import pl.szczodrzynski.edziennik.currentTimeUnix import pl.szczodrzynski.edziennik.datamodels.LoginStore import pl.szczodrzynski.edziennik.datamodels.Profile +import pl.szczodrzynski.edziennik.isNotNullNorEmpty class DataLibrus(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { + fun isPortalLoginValid() = portalTokenExpiryTime-30 > currentTimeUnix() && portalRefreshToken.isNotNullNorEmpty() && portalAccessToken.isNotNullNorEmpty() + fun isApiLoginValid() = apiTokenExpiryTime-30 > currentTimeUnix() && apiAccessToken.isNotNullNorEmpty() + fun isSynergiaLoginValid() = synergiaSessionIdExpiryTime-30 > currentTimeUnix() && synergiaSessionId.isNotNullNorEmpty() + fun isMessagesLoginValid() = messagesSessionIdExpiryTime-30 > currentTimeUnix() && messagesSessionId.isNotNullNorEmpty() + + override fun satisfyLoginMethods() { + if (isPortalLoginValid()) + loginMethods += LOGIN_METHOD_LIBRUS_PORTAL + if (isApiLoginValid()) + loginMethods += LOGIN_METHOD_LIBRUS_API + if (isSynergiaLoginValid()) + loginMethods += LOGIN_METHOD_LIBRUS_SYNERGIA + if (isMessagesLoginValid()) + loginMethods += LOGIN_METHOD_LIBRUS_MESSAGES + } + /* _____ _ _ | __ \ | | | | | |__) |__ _ __| |_ __ _| | diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt index 3166f211..ca8cf85e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusApi.kt @@ -37,7 +37,7 @@ class LoginLibrusApi { return } - if (data.apiTokenExpiryTime-30 > currentTimeUnix() && data.apiAccessToken.isNotNullNorEmpty()) { + if (data.isApiLoginValid()) { onSuccess() } else { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt index 1f6667bf..ec3bf565 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusMessages.kt @@ -20,27 +20,33 @@ class LoginLibrusMessages(val data: DataLibrus, val onSuccess: () -> Unit) { return@run } - if (data.messagesSessionIdExpiryTime-30 > currentTimeUnix() && data.messagesSessionId.isNotNullNorEmpty()) { + if (data.isMessagesLoginValid()) { onSuccess() } else { - when (data.loginStore.mode) { - LOGIN_MODE_LIBRUS_SYNERGIA -> loginWithCredentials() - else -> { - loginWithSynergia() - } + if (data.loginMethods.contains(LOGIN_METHOD_LIBRUS_SYNERGIA)) { + loginWithSynergia() + } + else if (data.apiLogin != null && data.apiPassword != null) { + loginWithCredentials() + } + else { + data.error(TAG, ERROR_LOGIN_DATA_MISSING) } } }} + /** + * XML (Flash messages website) login method. Uses a Synergia login and password. + */ 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 - } + + } + + /** + * A login method using the Synergia website (/wiadomosci2 Auto Login). + */ + private fun loginWithSynergia() { + } } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt index 242d5596..b3fbe09b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusPortal.kt @@ -33,7 +33,7 @@ class LoginLibrusPortal(val data: DataLibrus, val onSuccess: () -> Unit) { } // succeed having a non-expired access token and a refresh token - if (data.portalTokenExpiryTime-30 > currentTimeUnix() && data.portalRefreshToken.isNotNullNorEmpty() && data.portalAccessToken.isNotNullNorEmpty()) { + if (data.isPortalLoginValid()) { onSuccess() } else if (data.portalRefreshToken != null) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt index e78cd860..ea43ff37 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LoginLibrusSynergia.kt @@ -29,7 +29,7 @@ class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { return@run } - if (data.synergiaSessionIdExpiryTime-30 > currentTimeUnix() && data.synergiaSessionId.isNotNullNorEmpty()) { + if (data.isSynergiaLoginValid()) { onSuccess() } else { @@ -48,6 +48,7 @@ class LoginLibrusSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { /** * HTML form-based login method. Uses a Synergia login and password. */ + // TODO if loginWithCredentials fails and it is possible to use API, use it private fun loginWithCredentials() { } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index edca2ce6..7608c756 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -25,6 +25,14 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) */ val loginMethods = mutableListOf() + /** + * A method which may be overridden in child Data* classes. + * + * Calling it should populate [loginMethods] with all + * already available login methods (e.g. non-expired OAuth token). + */ + open fun satisfyLoginMethods() {} + val teacherList = LongSparseArray() val subjectList = LongSparseArray() val teamList = mutableListOf()