Skip to content

Instantly share code, notes, and snippets.

View dllewellyn's full-sized avatar

Dan dllewellyn

View GitHub Profile
private fun signup() {
lifecycleScope.launch(errorHandler()) {
val result = oneTapClient.suspendBeginSignInRequest(buildSignupRequest())
signUpResult.launch(
IntentSenderRequest.Builder(result.pendingIntent)
.build()
)
}
}
private val signUpResult =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
try {
val credential = oneTapClient.getSignInCredentialFromIntent(it.data)
val idToken = credential.googleIdToken
when {
idToken != null -> {
// Got an ID token from Google. Use it to authenticate
// with your backend.
Log.d(TAG, "Got ID token.")
private val oneTapClient: SignInClient by lazy {
Identity.getSignInClient(this)
}
lifecycleScope.launch(CoroutineExceptionHandler { _, e ->
Log.e(
TAG,
e.localizedMessage ?: "",
e
)
}) {
private fun buildSignupRequest() = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(getString(R.string.one_tap_client_id))
.setFilterByAuthorizedAccounts(false)
.build()
)
.build()
suspend fun SignInClient.suspendBeginSignInRequest(beginSignInRequest: BeginSignInRequest) :BeginSignInResult =
suspendCoroutine { continuation ->
beginSignIn(beginSignInRequest).addOnSuccessListener {
continuation.resume(it)
}
.addOnFailureListener {
continuation.resumeWithException(it)
}
}
@dllewellyn
dllewellyn / AuthenticationHandler
Last active July 1, 2020 15:51
Verify authentication claims for one tap
data class AuthenticationInternalClaims(val payload: GoogleIdToken.Payload) : Authentication {
override fun getName(): String = payload.subject
override fun getAttributes(): MutableMap<String, Any> = payload // Don't actually do this...
}
@Singleton
class GoogleAuthenticationHandler : TokenValidator {
override fun validateToken(token: String?): Flowable<Authentication> {
val sharedPreferences = EncryptedSharedPreferences
.create(
fileName,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val sharedPrefsEditor = sharedPreferences.edit()
​@Named​(​"​generator​"​)
​class​ ​OtherCustomTokenValidator​ ​:​ ​RSASignatureConfiguration​ {
    ​override​ ​fun​ ​getPublicKey​()​:​ ​RSAPublicKey​ {
        ​val​ decoder ​=​ ​Base64​.getDecoder()
​val​ spec ​=​ ​X509EncodedKeySpec​(decoder.decode(​"USERESULTOFPYTHONSCRIPTHERE"​))
        ​val​ kf​:​ ​KeyFactory​ ​=​ ​KeyFactory​.getInstance(​"​RSA​"​)
        ​return​ kf.generatePublic(spec) ​as​ ​RSAPublicKey​
    }
}
​@Controller​(​"​/loin"​)
​class​ ​CustomTokenController​ {
    ​@Secured​(​SecurityRule​.​IS_ANONYMOUS​)
    ​@Get​(​"​/logmein​"​)
    ​fun​ ​getLogin​(​principal​:​ ​Principal​) ​:​ ​String​ {
        ​FirebaseUtil​.initialise()
        ​return​ ​FirebaseAuth​.getInstance().createCustomToken(principal.name)
    }
}
@Secured(SecurityRule.IS_ANONYMOUS)
@Controller("/authenticated")
class CertificateCheckerController {
@Secured(SecurityRule.IS_AUTHENTICATED)
@Get("/verification")
fun verify(principal: Principal?) =
principal