Set Wear OAuth redirect URI automatically and catch exceptions (#3244)

- Allow the Wear support libraries to specify the redirect URI to use for OAuth login
 - Catch any exceptions that might be thrown when building an OAuth request
This commit is contained in:
Joris Pelgröm 2023-01-20 20:11:01 +01:00 committed by GitHub
parent e382a4b687
commit a4fd7d4fd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 25 deletions

View file

@ -13,7 +13,7 @@ interface AuthenticationRepository {
suspend fun getSessionState(): SessionState
suspend fun buildAuthenticationUrl(baseUrl: String, callbackUrl: String): String
suspend fun buildAuthenticationUrl(baseUrl: String): String
suspend fun buildBearerToken(): String

View file

@ -89,13 +89,12 @@ class AuthenticationRepositoryImpl @Inject constructor(
}
}
override suspend fun buildAuthenticationUrl(baseUrl: String, callbackUrl: String): String {
override suspend fun buildAuthenticationUrl(baseUrl: String): String {
return baseUrl.toHttpUrlOrNull()!!
.newBuilder()
.addPathSegments("auth/authorize")
.addEncodedQueryParameter("response_type", "code")
.addEncodedQueryParameter("client_id", AuthenticationService.CLIENT_ID)
.addEncodedQueryParameter("redirect_uri", callbackUrl)
.build()
.toString()
}

View file

@ -13,7 +13,6 @@ import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.DataMap
import com.google.android.gms.wearable.DataMapItem
import dagger.hilt.android.qualifiers.ActivityContext
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.data.authentication.AuthenticationRepository
import io.homeassistant.companion.android.common.data.url.UrlRepository
import kotlinx.coroutines.CoroutineScope
@ -47,17 +46,19 @@ class OnboardingPresenterImpl @Inject constructor(
// ManualSetupPresenterImpl. Also a good starting point for manual URL if it is possible to
// enter this on the device without the app in the future.
mainScope.launch {
val request = OAuthRequest.Builder(context)
.setAuthProviderUrl(
Uri.parse(
authenticationUseCase.buildAuthenticationUrl(
url,
OAuthRequest.WEAR_REDIRECT_URL_PREFIX + BuildConfig.APPLICATION_ID
)
val request: OAuthRequest
try {
request = OAuthRequest.Builder(context)
.setAuthProviderUrl(
Uri.parse(authenticationUseCase.buildAuthenticationUrl(url))
)
)
.setCodeChallenge(CodeChallenge(codeVerifier))
.build()
.setCodeChallenge(CodeChallenge(codeVerifier))
.build()
} catch (e: Exception) {
Log.e(TAG, "Unable to build OAuthRequest", e)
view.showError(commonR.string.failed_unsupported)
return@launch
}
authClient = RemoteAuthClient.create(context)
authClient?.let {

View file

@ -9,7 +9,6 @@ import androidx.wear.phone.interactions.authentication.OAuthRequest
import androidx.wear.phone.interactions.authentication.OAuthResponse
import androidx.wear.phone.interactions.authentication.RemoteAuthClient
import dagger.hilt.android.qualifiers.ActivityContext
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.data.authentication.AuthenticationRepository
import io.homeassistant.companion.android.common.data.url.UrlRepository
import kotlinx.coroutines.CoroutineScope
@ -39,17 +38,19 @@ class ManualSetupPresenterImpl @Inject constructor(
override fun onNextClicked(context: Context, url: String) {
view.showLoading()
mainScope.launch {
val request = OAuthRequest.Builder(context)
.setAuthProviderUrl(
Uri.parse(
authenticationUseCase.buildAuthenticationUrl(
url,
OAuthRequest.WEAR_REDIRECT_URL_PREFIX + BuildConfig.APPLICATION_ID
)
val request: OAuthRequest
try {
request = OAuthRequest.Builder(context)
.setAuthProviderUrl(
Uri.parse(authenticationUseCase.buildAuthenticationUrl(url))
)
)
.setCodeChallenge(CodeChallenge(codeVerifier))
.build()
.setCodeChallenge(CodeChallenge(codeVerifier))
.build()
} catch (e: Exception) {
Log.e(TAG, "Unable to build OAuthRequest", e)
view.showError(commonR.string.failed_unsupported)
return@launch
}
authClient = RemoteAuthClient.create(context)
authClient?.let {