-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve google sign in with updated credentials call (#1080)
* Resolve google sign in with updated credentials call * Restore Firebase BOM * Adjust Firebase AppCheck reference --------- Co-authored-by: Ashley Davies <[email protected]>
- Loading branch information
Showing
11 changed files
with
137 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 43 additions & 2 deletions
45
after-party/src/commonMain/kotlin/io/ashdavies/party/AfterPartyPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,68 @@ | ||
package io.ashdavies.party | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import com.slack.circuit.foundation.onNavEvent | ||
import com.slack.circuit.retained.rememberRetained | ||
import com.slack.circuit.runtime.Navigator | ||
import com.slack.circuit.runtime.screen.Screen | ||
import io.ashdavies.content.PlatformContext | ||
import io.ashdavies.gallery.GalleryScreen | ||
import io.ashdavies.identity.CredentialQueries | ||
import io.ashdavies.identity.IdentityManager | ||
import io.ashdavies.identity.IdentityState | ||
import io.ashdavies.sql.rememberLocalQueries | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
@Composable | ||
internal fun AfterPartyPresenter(navigator: Navigator): AfterPartyScreen.State { | ||
internal fun AfterPartyPresenter( | ||
platformContext: PlatformContext, | ||
navigator: Navigator, | ||
): AfterPartyScreen.State = AfterPartyPresenter( | ||
identityManager = rememberIdentityManager( | ||
platformContext = platformContext, | ||
credentialQueries = rememberLocalQueries { | ||
it.credentialQueries | ||
}, | ||
), | ||
coroutineScope = rememberCoroutineScope(), | ||
navigator = navigator, | ||
) | ||
|
||
@Composable | ||
private fun AfterPartyPresenter( | ||
identityManager: IdentityManager, | ||
coroutineScope: CoroutineScope, | ||
navigator: Navigator, | ||
): AfterPartyScreen.State { | ||
val identityState by identityManager.state.collectAsState(IdentityState.Unauthenticated) | ||
var screen by rememberRetained { mutableStateOf<Screen>(GalleryScreen) } | ||
|
||
return AfterPartyScreen.State( | ||
identityState = IdentityState.Unsupported, | ||
identityState = identityState, | ||
screen = screen, | ||
) { event -> | ||
when (event) { | ||
is AfterPartyScreen.Event.Login -> coroutineScope.launch { identityManager.signIn() } | ||
is AfterPartyScreen.Event.ChildNav -> navigator.onNavEvent(event.navEvent) | ||
is AfterPartyScreen.Event.BottomNav -> screen = event.screen | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun rememberIdentityManager( | ||
platformContext: PlatformContext, | ||
credentialQueries: CredentialQueries, | ||
): IdentityManager = remember(platformContext, credentialQueries) { | ||
IdentityManager( | ||
platformContext = platformContext, | ||
credentialQueries = credentialQueries, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 65 additions & 27 deletions
92
...y-manager/src/androidMain/kotlin/io/ashdavies/identity/GoogleIdIdentityService.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,91 @@ | ||
package io.ashdavies.identity | ||
|
||
import androidx.credentials.CredentialManager | ||
import androidx.credentials.CredentialOption | ||
import androidx.credentials.CustomCredential | ||
import androidx.credentials.GetCredentialRequest | ||
import androidx.credentials.GetCredentialResponse | ||
import androidx.credentials.exceptions.GetCredentialException | ||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption | ||
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import io.ashdavies.content.PlatformContext | ||
|
||
internal actual class GoogleIdIdentityService actual constructor( | ||
private val context: PlatformContext, | ||
) : IdentityService<GoogleIdIdentityRequest> { | ||
|
||
override suspend fun request(request: GoogleIdIdentityRequest): IdentityResponse { | ||
val googleIdOption = GetGoogleIdOption.Builder() | ||
.setFilterByAuthorizedAccounts(request.filterByAuthorizedAccounts) | ||
.setAutoSelectEnabled(request.autoSelectEnabled) | ||
.setServerClientId(request.serverClientId) | ||
.setNonce(request.nonce) | ||
.build() | ||
private val credentialManager: CredentialManager by lazy(LazyThreadSafetyMode.NONE) { | ||
CredentialManager.create(context) | ||
} | ||
|
||
val getCredentialRequest = GetCredentialRequest.Builder() | ||
.addCredentialOption(googleIdOption) | ||
.build() | ||
override suspend fun request(request: GoogleIdIdentityRequest): IdentityResponse { | ||
val getCredentialResponse = try { | ||
getCredential(request, filterByAuthorizedAccounts = true) | ||
} catch (ignored: GetCredentialException) { | ||
try { | ||
getCredential(request, filterByAuthorizedAccounts = false) | ||
} catch (ignored: GetCredentialException) { | ||
val signInWithGoogleOption = GetSignInWithGoogleOption | ||
.Builder(request.serverClientId) | ||
.setNonce(request.nonce) | ||
.build() | ||
|
||
val credentialManager = CredentialManager.create(context) | ||
val getCredentialResponse = credentialManager.getCredential( | ||
context = context, | ||
request = getCredentialRequest, | ||
) | ||
getCredential(signInWithGoogleOption) | ||
} | ||
} | ||
|
||
val googleIdCredential = when (val credential = getCredentialResponse.credential) { | ||
is GoogleIdTokenCredential -> credential | ||
return when (val credential = getCredentialResponse.credential) { | ||
is GoogleIdTokenCredential -> IdentityResponse( | ||
uuid = credential.id, | ||
pictureProfileUrl = credential | ||
.profilePictureUri | ||
?.toString(), | ||
) | ||
|
||
is CustomCredential -> when (credential.type) { | ||
GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL -> { | ||
GoogleIdTokenCredential.createFrom(credential.data) | ||
is CustomCredential -> { | ||
check(credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { | ||
"Unrecognised credential type ${credential.type}" | ||
} | ||
|
||
else -> throw UnsupportedOperationException() | ||
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data) | ||
|
||
IdentityResponse( | ||
uuid = googleIdTokenCredential.id, | ||
pictureProfileUrl = googleIdTokenCredential | ||
.profilePictureUri | ||
?.toString(), | ||
) | ||
} | ||
|
||
else -> throw UnsupportedOperationException() | ||
else -> throw UnsupportedOperationException("Unrecognised credential $credential") | ||
} | ||
} | ||
|
||
return IdentityResponse( | ||
uuid = googleIdCredential.idToken, | ||
pictureProfileUrl = googleIdCredential | ||
.profilePictureUri | ||
?.toString(), | ||
private suspend fun getCredential( | ||
request: GoogleIdIdentityRequest, | ||
filterByAuthorizedAccounts: Boolean, | ||
): GetCredentialResponse { | ||
val googleIdOption = GetGoogleIdOption.Builder() | ||
.setFilterByAuthorizedAccounts(filterByAuthorizedAccounts) | ||
.setAutoSelectEnabled(request.autoSelectEnabled) | ||
.setServerClientId(request.serverClientId) | ||
.setNonce(request.nonce) | ||
.build() | ||
|
||
return getCredential(googleIdOption) | ||
} | ||
|
||
private suspend fun getCredential( | ||
option: CredentialOption, | ||
): GetCredentialResponse { | ||
val request = GetCredentialRequest.Builder() | ||
.addCredentialOption(option) | ||
.build() | ||
|
||
return credentialManager.getCredential( | ||
context = context, | ||
request = request, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters