Improve login trigger

This commit is contained in:
Mikołaj Pich 2018-09-09 16:15:17 +02:00
parent 914292c294
commit 020b1f433b
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
4 changed files with 19 additions and 24 deletions

View file

@ -1,3 +1,3 @@
package io.github.wulkanowy.api.auth
class BadCredentialsException internal constructor(message: String) : NotLoggedInException(message)
class BadCredentialsException internal constructor(message: String) : VulcanException(message)

View file

@ -1,3 +1,3 @@
package io.github.wulkanowy.api.auth
open class NotLoggedInException(message: String) : VulcanException(message)
class NotLoggedInException(message: String) : Exception(message)

View file

@ -17,6 +17,14 @@ class ErrorInterceptor : Interceptor {
}
private fun checkForError(doc: Document) {
doc.select(".loginButton").let {
if (it.isNotEmpty()) throw NotLoggedInException(it.text())
}
doc.body().text().let { // /messages
if (it.contains("The custom error module does not recognize this error")) throw NotLoggedInException("Zaloguj się")
}
doc.select(".ErrorMessage, #ErrorTextLabel").let {
if (it.isNotEmpty()) throw BadCredentialsException(it.text())
}
@ -29,15 +37,10 @@ class ErrorInterceptor : Interceptor {
if (it.isNotEmpty()) throw AccountPermissionException(it.text())
}
if (doc.body().text().contains("The custom error module does not recognize this error")) { // /messages
throw NotLoggedInException("Zaloguj się")
}
when(doc.title()) {
"Błąd" -> throw VulcanException(doc.body().text())
"Błąd strony" -> throw VulcanException(doc.select(".errorMessage").text())
"Logowanie" -> throw AccountPermissionException(doc.select("div").last().html().split("<br>")[1].trim())
"Dziennik UONET+", "Uczeń" -> throw NotLoggedInException(doc.select(".loginButton").text())
"Przerwa techniczna" -> throw ServiceUnavailableException(doc.title())
}
}

View file

@ -54,14 +54,11 @@ class ServiceManager(
}
fun getSnpService(withLogin: Boolean = true, interceptor: Boolean = true): StudentAndParentService {
if (withLogin && schoolId.isBlank()) throw NotLoggedInException("School id is not set")
val client = getClientBuilder()
if (interceptor) client.addInterceptor(studentAndParentInterceptor)
if (withLogin) {
if (schoolId.isBlank()) throw NotLoggedInException("School id is not set")
}
return getRetrofit(client, "uonetplus-opiekun", "$symbol/$schoolId/", withLogin)
.addConverterFactory(JspoonConverterFactory.create()).build()
.create(StudentAndParentService::class.java)
@ -74,20 +71,15 @@ class ServiceManager(
}
private fun getRetrofit(client: OkHttpClient.Builder, subDomain: String, urlAppend: String, login: Boolean = true): Retrofit.Builder {
val retrofit = Retrofit.Builder()
return Retrofit.Builder()
.baseUrl("$schema://$subDomain.$host/$urlAppend")
.client(client.build())
return if (login) {
retrofit.addCallAdapterFactory(RxJava2ReauthCallAdapterFactory.create(
loginRepository.login(email, password).toFlowable(),
{ it != NotLoggedInException("Zaloguj się") },
1
))
} else {
retrofit.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
}
.addCallAdapterFactory(if (!login) RxJava2CallAdapterFactory.create() else
RxJava2ReauthCallAdapterFactory.create(
loginRepository.login(email, password).toFlowable(),
{ it is NotLoggedInException }
)
)
}
private fun getClientBuilder(): OkHttpClient.Builder {