-
Notifications
You must be signed in to change notification settings - Fork 590
/
AuthenticationPresenterImpl.kt
68 lines (58 loc) · 2.51 KB
/
AuthenticationPresenterImpl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.homeassistant.companion.android.onboarding.authentication
import android.net.Uri
import android.util.Log
import io.homeassistant.companion.android.api.AuthenticationService
import io.homeassistant.companion.android.api.HomeAssistantApi
import io.homeassistant.companion.android.api.Session
import io.homeassistant.companion.android.api.AuthorizationCode
import retrofit2.Call
import retrofit2.Response
class AuthenticationPresenterImpl(private val view: AuthenticationView) : AuthenticationPresenter {
companion object {
private const val TAG = "AuthenticationPresenter"
private const val AUTH_CALLBACK = "homeassistant://auth-callback"
}
private lateinit var url: String
override fun initialize(url: String) {
this.url = url
}
override fun onViewReady() {
view.loadUrl(
Uri.parse(url)
.buildUpon()
.appendPath("auth")
.appendPath("authorize")
.appendQueryParameter("response_type", "code")
.appendQueryParameter("client_id", AuthenticationService.CLIENT_ID)
.appendQueryParameter("redirect_uri", AUTH_CALLBACK)
.build()
.toString()
)
}
override fun onRedirectUrl(redirectUrl: String): Boolean {
val callbackUrl = Uri.parse(redirectUrl)
val code = callbackUrl.getQueryParameter("code")
if (callbackUrl.toString().contains(AUTH_CALLBACK) && code != null) {
HomeAssistantApi(url)
.authenticationService
.getToken("authorization_code", code, AuthenticationService.CLIENT_ID)
.enqueue(object : retrofit2.Callback<AuthorizationCode> {
override fun onFailure(call: Call<AuthorizationCode>, t: Throwable) {
Log.e(TAG, "authorization code error", t)
}
override fun onResponse(call: Call<AuthorizationCode>, response: Response<AuthorizationCode>) {
val body = response.body()
if (response.isSuccessful && body != null) {
Session.getInstance().registerSession(body, url)
view.openWebview(url)
} else {
Log.e(TAG, "authorization code error ${response.errorBody()?.string()}")
}
}
})
return true
} else {
return false
}
}
}