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 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 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) { 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 { doc.select(".ErrorMessage, #ErrorTextLabel").let {
if (it.isNotEmpty()) throw BadCredentialsException(it.text()) if (it.isNotEmpty()) throw BadCredentialsException(it.text())
} }
@ -29,15 +37,10 @@ class ErrorInterceptor : Interceptor {
if (it.isNotEmpty()) throw AccountPermissionException(it.text()) 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()) { when(doc.title()) {
"Błąd" -> throw VulcanException(doc.body().text()) "Błąd" -> throw VulcanException(doc.body().text())
"Błąd strony" -> throw VulcanException(doc.select(".errorMessage").text()) "Błąd strony" -> throw VulcanException(doc.select(".errorMessage").text())
"Logowanie" -> throw AccountPermissionException(doc.select("div").last().html().split("<br>")[1].trim()) "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()) "Przerwa techniczna" -> throw ServiceUnavailableException(doc.title())
} }
} }

View file

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