-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update FirebaseCore.def * Adding missing Auth calls * Add iOS implementation * Add providers * Trying to resolve Firestore issues * Added iOS methods * Add Javascript * Cleaned up .def files Dependencies now part of actual submodules * Add Test * Fixed compile for tests * Add missing firetore frameworks * Account for nullable results * Use Carthage instead of zip for linking. This cleans up dependencies and reduces build time * Track firestore version * Update yaml * Remove useless podspec * Code cleanup * Fixing remarks * Move ActionData into operator * fix test Co-authored-by: Grigory Avdyushin <avdyushin.g@gmail.com>
- Loading branch information
Showing
18 changed files
with
1,463 additions
and
138 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
136 changes: 136 additions & 0 deletions
136
firebase-auth/src/androidMain/kotlin/dev/gitlive/firebase/auth/credentials.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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.gitlive.firebase.auth | ||
|
||
import android.app.Activity | ||
import com.google.firebase.FirebaseException | ||
import com.google.firebase.auth.PhoneAuthProvider | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.tasks.await | ||
import java.util.concurrent.TimeUnit | ||
|
||
actual open class AuthCredential(open val android: com.google.firebase.auth.AuthCredential) { | ||
actual val providerId: String | ||
get() = android.provider | ||
} | ||
|
||
actual class PhoneAuthCredential(override val android: com.google.firebase.auth.PhoneAuthCredential) : AuthCredential(android) | ||
|
||
actual class OAuthCredential(override val android: com.google.firebase.auth.OAuthCredential) : AuthCredential(android) | ||
|
||
actual object EmailAuthProvider { | ||
actual fun credential( | ||
email: String, | ||
password: String | ||
): AuthCredential = AuthCredential(com.google.firebase.auth.EmailAuthProvider.getCredential(email, password)) | ||
} | ||
|
||
actual object FacebookAuthProvider { | ||
actual fun credential(accessToken: String): AuthCredential = AuthCredential(com.google.firebase.auth.FacebookAuthProvider.getCredential(accessToken)) | ||
} | ||
|
||
actual object GithubAuthProvider { | ||
actual fun credential(token: String): AuthCredential = AuthCredential(com.google.firebase.auth.GithubAuthProvider.getCredential(token)) | ||
} | ||
|
||
actual object GoogleAuthProvider { | ||
actual fun credential(idToken: String, accessToken: String): AuthCredential = AuthCredential(com.google.firebase.auth.GoogleAuthProvider.getCredential(idToken, accessToken)) | ||
} | ||
|
||
actual class OAuthProvider(val android: com.google.firebase.auth.OAuthProvider.Builder, private val auth: FirebaseAuth) { | ||
actual constructor(provider: String, auth: FirebaseAuth) : this(com.google.firebase.auth.OAuthProvider.newBuilder(provider, auth.android), auth) | ||
|
||
actual companion object { | ||
actual fun credentials(type: OAuthCredentialsType): AuthCredential { | ||
val credential = com.google.firebase.auth.OAuthProvider.newCredentialBuilder(type.providerId).apply { | ||
when (type) { | ||
is OAuthCredentialsType.AccessToken -> accessToken = type.accessToken | ||
is OAuthCredentialsType.IdAndAccessToken -> { | ||
accessToken = type.accessToken | ||
setIdToken(type.idToken) | ||
} | ||
is OAuthCredentialsType.IdAndAccessTokenAndRawNonce -> { | ||
accessToken = type.accessToken | ||
setIdTokenWithRawNonce(type.idToken, type.rawNonce) | ||
} | ||
is OAuthCredentialsType.IdTokenAndRawNonce -> { | ||
setIdTokenWithRawNonce(type.idToken, type.rawNonce) | ||
} | ||
} | ||
}.build() | ||
return (credential as? com.google.firebase.auth.OAuthCredential)?.let { OAuthCredential(it) } ?: AuthCredential(credential) | ||
} | ||
} | ||
|
||
private var customParameters: Map<String, String> = emptyMap() | ||
|
||
actual fun addScope(vararg scope: String) { | ||
android.scopes = android.scopes + scope.asList() | ||
} | ||
actual fun setCustomParameters(parameters: Map<String, String>) { | ||
customParameters = parameters | ||
} | ||
|
||
actual suspend fun signIn(signInProvider: SignInProvider): AuthResult = AuthResult(auth.android.startActivityForSignInWithProvider(signInProvider, android.apply { addCustomParameters(customParameters) }.build()).await()) | ||
} | ||
|
||
actual typealias SignInProvider = Activity | ||
|
||
actual class PhoneAuthProvider(val android: com.google.firebase.auth.PhoneAuthProvider) { | ||
|
||
|
||
actual constructor(auth: FirebaseAuth) : this(com.google.firebase.auth.PhoneAuthProvider.getInstance(auth.android)) | ||
|
||
actual fun credential(verificationId: String, smsCode: String): PhoneAuthCredential = PhoneAuthCredential(com.google.firebase.auth.PhoneAuthProvider.getCredential(verificationId, smsCode)) | ||
actual suspend fun verifyPhoneNumber(phoneNumber: String, verificationProvider: PhoneVerificationProvider): AuthCredential = coroutineScope { | ||
val response = CompletableDeferred<Result<AuthCredential>>() | ||
val callback = object : | ||
PhoneAuthProvider.OnVerificationStateChangedCallbacks() { | ||
|
||
override fun onCodeSent(verificationId: String, forceResending: PhoneAuthProvider.ForceResendingToken) { | ||
verificationProvider.codeSent { android.verifyPhoneNumber(phoneNumber, verificationProvider.timeout, verificationProvider.unit, verificationProvider.activity, this, forceResending) } | ||
} | ||
|
||
override fun onCodeAutoRetrievalTimeOut(verificationId: String) { | ||
launch { | ||
val code = verificationProvider.getVerificationCode() | ||
try { | ||
val credentials = | ||
credential(verificationId, code) | ||
response.complete(Result.success(credentials)) | ||
} catch (e: Exception) { | ||
response.complete(Result.failure(e)) | ||
} | ||
} | ||
} | ||
|
||
override fun onVerificationCompleted(credential: com.google.firebase.auth.PhoneAuthCredential) { | ||
response.complete(Result.success(AuthCredential(credential))) | ||
} | ||
|
||
override fun onVerificationFailed(error: FirebaseException) { | ||
response.complete(Result.failure(error)) | ||
} | ||
|
||
} | ||
android.verifyPhoneNumber(phoneNumber, verificationProvider.timeout, verificationProvider.unit, verificationProvider.activity, callback) | ||
|
||
response.await().getOrThrow() | ||
} | ||
} | ||
|
||
actual interface PhoneVerificationProvider { | ||
val activity: Activity | ||
val timeout: Long | ||
val unit: TimeUnit | ||
fun codeSent(triggerResend: (Unit) -> Unit) | ||
suspend fun getVerificationCode(): String | ||
} | ||
|
||
actual object TwitterAuthProvider { | ||
actual fun credential(token: String, secret: String): AuthCredential = AuthCredential(com.google.firebase.auth.TwitterAuthProvider.getCredential(token, secret)) | ||
} |
42 changes: 42 additions & 0 deletions
42
firebase-auth/src/androidMain/kotlin/dev/gitlive/firebase/auth/multifactor.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.gitlive.firebase.auth | ||
|
||
import kotlinx.coroutines.tasks.await | ||
|
||
actual class MultiFactor(val android: com.google.firebase.auth.MultiFactor) { | ||
actual val enrolledFactors: List<MultiFactorInfo> | ||
get() = android.enrolledFactors.map { MultiFactorInfo(it) } | ||
actual suspend fun enroll(multiFactorAssertion: MultiFactorAssertion, displayName: String?) = android.enroll(multiFactorAssertion.android, displayName).await().run { Unit } | ||
actual suspend fun getSession(): MultiFactorSession = MultiFactorSession(android.session.await()) | ||
actual suspend fun unenroll(multiFactorInfo: MultiFactorInfo) = android.unenroll(multiFactorInfo.android).await().run { Unit } | ||
actual suspend fun unenroll(factorUid: String) = android.unenroll(factorUid).await().run { Unit } | ||
} | ||
|
||
actual class MultiFactorInfo(val android: com.google.firebase.auth.MultiFactorInfo) { | ||
actual val displayName: String? | ||
get() = android.displayName | ||
actual val enrollmentTime: Double | ||
get() = android.enrollmentTimestamp.toDouble() | ||
actual val factorId: String | ||
get() = android.factorId | ||
actual val uid: String | ||
get() = android.uid | ||
} | ||
|
||
actual class MultiFactorAssertion(val android: com.google.firebase.auth.MultiFactorAssertion) { | ||
actual val factorId: String | ||
get() = android.factorId | ||
} | ||
|
||
actual class MultiFactorSession(val android: com.google.firebase.auth.MultiFactorSession) | ||
|
||
actual class MultiFactorResolver(val android: com.google.firebase.auth.MultiFactorResolver) { | ||
actual val auth: FirebaseAuth = FirebaseAuth(android.firebaseAuth) | ||
actual val hints: List<MultiFactorInfo> = android.hints.map { MultiFactorInfo(it) } | ||
actual val session: MultiFactorSession = MultiFactorSession(android.session) | ||
|
||
actual suspend fun resolveSignIn(assertion: MultiFactorAssertion): AuthResult = AuthResult(android.resolveSignIn(assertion.android).await()) | ||
} |
Oops, something went wrong.