mirror of
https://github.com/home-assistant/android
synced 2024-10-01 13:53:53 +00:00
Logout (#49)
* Logout the use when revokeExternalAuth is called * callback even if onGetExternalAuth failed * Add http log * Open on boarding activity when logout * fix revoke token field name
This commit is contained in:
parent
2ed8fab74a
commit
433ead291d
|
@ -5,6 +5,8 @@ interface WebView {
|
|||
|
||||
fun loadUrl(url: String)
|
||||
|
||||
fun setExternalAuth(callback: String, externalAuth: String)
|
||||
fun setExternalAuth(script: String)
|
||||
|
||||
fun openOnBoarding()
|
||||
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import io.homeassistant.companion.android.DaggerPresenterComponent
|
|||
import io.homeassistant.companion.android.PresenterModule
|
||||
import io.homeassistant.companion.android.R
|
||||
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
|
||||
import io.homeassistant.companion.android.onboarding.OnboardingActivity
|
||||
import io.homeassistant.companion.android.settings.SettingsActivity
|
||||
import org.json.JSONObject
|
||||
import javax.inject.Inject
|
||||
|
@ -58,6 +59,11 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
presenter.onGetExternalAuth(JSONObject(callback).get("callback") as String)
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun revokeExternalAuth(callback: String) {
|
||||
presenter.onRevokeExternalAuth(JSONObject(callback).get("callback") as String)
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun externalBus(message: String) {
|
||||
Log.d(TAG, "External bus $message")
|
||||
|
@ -89,6 +95,11 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
presenter.onViewReady()
|
||||
}
|
||||
|
||||
override fun openOnBoarding() {
|
||||
finish()
|
||||
startActivity(Intent(this, OnboardingActivity::class.java))
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
if (webView.canGoBack()) {
|
||||
webView.goBack()
|
||||
|
@ -101,9 +112,9 @@ class WebViewActivity : AppCompatActivity(), io.homeassistant.companion.android.
|
|||
webView.loadUrl(url)
|
||||
}
|
||||
|
||||
override fun setExternalAuth(callback: String, externalAuth: String) {
|
||||
override fun setExternalAuth(script: String) {
|
||||
webView.post {
|
||||
webView.evaluateJavascript("$callback(true, $externalAuth);", null)
|
||||
webView.evaluateJavascript(script, null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ interface WebViewPresenter {
|
|||
|
||||
fun onGetExternalAuth(callback: String)
|
||||
|
||||
fun onRevokeExternalAuth(callback: String)
|
||||
|
||||
fun onFinish()
|
||||
|
||||
}
|
||||
|
|
|
@ -35,9 +35,23 @@ class WebViewPresenterImpl @Inject constructor(
|
|||
override fun onGetExternalAuth(callback: String) {
|
||||
mainScope.launch {
|
||||
try {
|
||||
view.setExternalAuth(callback, authenticationUseCase.retrieveExternalAuthentication())
|
||||
view.setExternalAuth("$callback(true, ${authenticationUseCase.retrieveExternalAuthentication()})")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to retrieve external auth", e)
|
||||
view.setExternalAuth("$callback(false)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRevokeExternalAuth(callback: String) {
|
||||
mainScope.launch {
|
||||
try {
|
||||
authenticationUseCase.revokeSession()
|
||||
view.setExternalAuth("$callback(true)")
|
||||
view.openOnBoarding()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to revoke session", e)
|
||||
view.setExternalAuth("$callback(false)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ object WebViewPresenterImplSpec : Spek({
|
|||
}
|
||||
|
||||
it("should set external auth") {
|
||||
verify { view.setExternalAuth("externalAuthSetToken", "{\"access_token\":\"ABCDEFGH\",\"expires_in\":1800}") }
|
||||
verify { view.setExternalAuth("externalAuthSetToken(true, {\"access_token\":\"ABCDEFGH\",\"expires_in\":1800})") }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,9 +60,30 @@ object WebViewPresenterImplSpec : Spek({
|
|||
}
|
||||
|
||||
it("should not crash") {
|
||||
coVerify {
|
||||
view wasNot Called
|
||||
}
|
||||
verify { view.setExternalAuth("externalAuthSetToken(false)") }
|
||||
}
|
||||
}
|
||||
|
||||
describe("on revoke external auth on success") {
|
||||
beforeEachTest {
|
||||
coEvery { authenticationUseCase.revokeSession() } just runs
|
||||
presenter.onRevokeExternalAuth("externalAuthRevokeToken")
|
||||
}
|
||||
|
||||
it("should set external auth") {
|
||||
verify { view.setExternalAuth("externalAuthRevokeToken(true)") }
|
||||
verify { view.openOnBoarding() }
|
||||
}
|
||||
}
|
||||
|
||||
describe("on revoke external auth on error") {
|
||||
beforeEachTest {
|
||||
coEvery { authenticationUseCase.revokeSession() } throws Exception()
|
||||
presenter.onRevokeExternalAuth("externalAuthRevokeToken")
|
||||
}
|
||||
|
||||
it("should set external auth") {
|
||||
verify { view.setExternalAuth("externalAuthRevokeToken(false)") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ dependencies {
|
|||
implementation "com.squareup.retrofit2:retrofit:$retrofit2Version"
|
||||
implementation "com.squareup.retrofit2:converter-jackson:$retrofit2Version"
|
||||
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"
|
||||
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"
|
||||
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion"
|
||||
|
||||
implementation "org.threeten:threetenbp:$threeTenBpVersion"
|
||||
|
|
|
@ -3,6 +3,8 @@ package io.homeassistant.companion.android.data
|
|||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy
|
||||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory
|
||||
import javax.inject.Inject
|
||||
|
@ -18,6 +20,13 @@ class HomeAssistantRetrofit @Inject constructor(url: String) {
|
|||
.registerKotlinModule()
|
||||
)
|
||||
)
|
||||
.client(
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
})
|
||||
.build()
|
||||
)
|
||||
.baseUrl(url)
|
||||
.build()
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ interface AuthenticationService {
|
|||
@FormUrlEncoded
|
||||
@POST("auth/token")
|
||||
suspend fun revokeToken(
|
||||
@Field("refresh_token") refreshToken: String,
|
||||
@Field("token") refreshToken: String,
|
||||
@Field("action") action: String
|
||||
)
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ object AuthenticationServiceSpec : Spek({
|
|||
assertThat(request.method).isEqualTo("POST")
|
||||
assertThat(request.path).isEqualTo("/auth/token")
|
||||
assertThat(request.body.readUtf8())
|
||||
.contains("refresh_token=IJKLMNOPQRST")
|
||||
.contains("token=IJKLMNOPQRST")
|
||||
.contains("action=revoke")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue