-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
396 additions
and
233 deletions.
There are no files selected for viewing
111 changes: 48 additions & 63 deletions
111
packages/celest_auth/android/src/main/kotlin/dev/celest/celest_auth/CelestAuth.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,79 +1,64 @@ | ||
package dev.celest.celest_auth | ||
|
||
import android.content.BroadcastReceiver | ||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.CancellationSignal | ||
import android.service.voice.VoiceInteractionSession.ActivityId | ||
import androidx.annotation.Keep | ||
import androidx.credentials.CreateCredentialResponse | ||
import androidx.credentials.CreatePublicKeyCredentialRequest | ||
import androidx.credentials.CredentialManager | ||
import androidx.credentials.CustomCredential | ||
import androidx.credentials.CredentialManagerCallback | ||
import androidx.credentials.GetCredentialRequest | ||
import androidx.credentials.GetCredentialResponse | ||
import androidx.credentials.GetPublicKeyCredentialOption | ||
import androidx.credentials.exceptions.CreateCredentialException | ||
import androidx.credentials.exceptions.GetCredentialException | ||
import com.google.android.gms.tasks.OnSuccessListener | ||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import com.google.android.gms.fido.Fido | ||
import kotlinx.coroutines.coroutineScope | ||
import java.util.concurrent.Executors | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
@Keep | ||
class CelestAuthListener: BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent?) { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
@Keep | ||
class CelestAuth { | ||
private lateinit var context: Context | ||
private lateinit var credentialManager: CredentialManager | ||
|
||
fun init(context: Context) { | ||
this.context = context | ||
credentialManager = CredentialManager.create(context) | ||
} | ||
|
||
fun signInWithCustomTabs( | ||
uri: Uri, | ||
onSuccess: () -> Unit, | ||
onError: (Exception) -> Unit, | ||
) { | ||
|
||
} | ||
class CelestAuth(private val mainActivity: Activity) { | ||
private val credentialManager: CredentialManager = CredentialManager.create(mainActivity) | ||
private val executor = Executors.newCachedThreadPool() | ||
|
||
// Adapted from: https://developer.android.com/training/sign-in/credential-manager | ||
suspend fun signInWithGoogle( | ||
clientId: String, | ||
nonce: String | ||
) { | ||
// TODO: GetSignInWithGoogleOption? | ||
val googleIdOption = GetGoogleIdOption.Builder() | ||
.setFilterByAuthorizedAccounts(true) | ||
.setAutoSelectEnabled(true) | ||
.setServerClientId(clientId) | ||
.setNonce(nonce) | ||
.build() | ||
val request = GetCredentialRequest.Builder() | ||
.addCredentialOption(googleIdOption) | ||
.build() | ||
coroutineScope { | ||
try { | ||
val result = credentialManager.getCredential(context, request) | ||
} catch (e: GetCredentialException) { | ||
TODO() | ||
} | ||
} | ||
fun register( | ||
requestJson: String, | ||
callback: CredentialManagerCallback<CreateCredentialResponse, CreateCredentialException>, | ||
): CancellationSignal { | ||
val request = CreatePublicKeyCredentialRequest( | ||
requestJson = requestJson, | ||
preferImmediatelyAvailableCredentials = true | ||
) | ||
val cancellationSignal = CancellationSignal() | ||
credentialManager.createCredentialAsync( | ||
mainActivity, | ||
request, | ||
cancellationSignal, | ||
executor, | ||
callback, | ||
) | ||
return cancellationSignal | ||
} | ||
|
||
fun handleSignIn(result: GetCredentialResponse) { | ||
when (val credential = result.credential) { | ||
is CustomCredential -> { | ||
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { | ||
// Use googleIdTokenCredential and extract id to validate and | ||
// authenticate on your server. | ||
val googleIdTokenCredential = GoogleIdTokenCredential | ||
.createFrom(credential.data) | ||
} | ||
} | ||
} | ||
fun authenticate( | ||
requestJson: String, | ||
callback: CredentialManagerCallback<GetCredentialResponse, GetCredentialException>, | ||
): CancellationSignal { | ||
val request = GetCredentialRequest.Builder().addCredentialOption( | ||
GetPublicKeyCredentialOption(requestJson = requestJson) | ||
).build() | ||
val cancellationSignal = CancellationSignal() | ||
credentialManager.getCredentialAsync( | ||
mainActivity, | ||
request, | ||
cancellationSignal, | ||
executor, | ||
callback, | ||
) | ||
return cancellationSignal | ||
} | ||
} |
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
Oops, something went wrong.