Skip to content

Commit

Permalink
FirebaseAuth: Improve signInWithCredential
Browse files Browse the repository at this point in the history
  • Loading branch information
DaVinci9196 authored and mar-v-in committed Aug 11, 2024
1 parent 882c8a5 commit 2fae497
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
2 changes: 2 additions & 0 deletions firebase-auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ android {

dependencies {
api project(':play-services-basement')

annotationProcessor project(':safe-parcel-processor')
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,27 @@ class FirebaseAuthServiceImpl(private val context: Context, override val lifecyc
}

override fun signInWithCredential(request: SignInWithCredentialAidlRequest, callbacks: IFirebaseAuthCallbacks) {
Log.d(TAG, "Not yet implemented: signInWithCredential")
callbacks.onFailure(Status(CommonStatusCodes.CANCELED, "Not supported"))
lifecycleScope.launchWhenStarted {
Log.d(TAG, "signInWithCredential request: ${request.request}")
try {
val tokenResult = client.verifyAssertion(request.request.requestUri, request.request.postBody, request.request.returnSecureToken, request.request.returnIdpCredential)
Log.d(TAG, "signInWithCredential callback: $tokenResult ")
val idToken = tokenResult.getString("idToken")
val refreshToken = tokenResult.getString("refreshToken")
val getTokenResponse = client.getTokenByRefreshToken(refreshToken).toGetTokenResponse()
val accountInfoResult = client.getAccountInfo(idToken = idToken).getJSONArray("users").getJSONObject(0).toGetAccountInfoUser()
Log.d(TAG, "signInWithCredential callback: onGetTokenResponseAndUser")
callbacks.onGetTokenResponseAndUser(getTokenResponse, accountInfoResult)
} catch (e: Exception) {
Log.w(TAG, "signInWithCredential callback: onFailure", e)
callbacks.onFailure(Status(CommonStatusCodes.INTERNAL_ERROR, e.message))
}
}
}

override fun signInWithCredentialCompat(verifyAssertionRequest: VerifyAssertionRequest, callbacks: IFirebaseAuthCallbacks) {
Log.d(TAG, "Not yet implemented: signInWithCredentialCompat")
callbacks.onFailure(Status(CommonStatusCodes.CANCELED, "Not supported"))
Log.d(TAG, "signInWithCredentialCompat verifyAssertionRequest: $verifyAssertionRequest")
signInWithCredential(SignInWithCredentialAidlRequest(verifyAssertionRequest), callbacks)
}

override fun signInWithCustomToken(request: SignInWithCustomTokenAidlRequest, callbacks: IFirebaseAuthCallbacks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class IdentityToolkitClient(context: Context, private val apiKey: String, privat
.put("token", token)
.put("returnSecureToken", returnSecureToken))

suspend fun verifyAssertion(requestUri: String? = null, postBody: String? = null, returnSecureToken: Boolean = true, returnIdpCredential: Boolean = true): JSONObject =
request("verifyAssertion", JSONObject()
.put("requestUri", requestUri)
.put("postBody", postBody)
.put("returnSecureToken", returnSecureToken)
.put("returnIdpCredential", returnIdpCredential))

suspend fun verifyPassword(email: String? = null, password: String? = null, tenantId: String? = null, returnSecureToken: Boolean = true): JSONObject =
request("verifyPassword", JSONObject()
.put("email", email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@

package com.google.firebase.auth.api.internal;

import org.microg.safeparcel.AutoSafeParcelable;
import android.os.Parcel;

import androidx.annotation.NonNull;

import com.google.android.gms.common.internal.safeparcel.AbstractSafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelableCreatorAndWriter;

@SafeParcelable.Class
public class SignInWithCredentialAidlRequest extends AbstractSafeParcelable {

@Field(1)
public VerifyAssertionRequest request;

public SignInWithCredentialAidlRequest(VerifyAssertionRequest request) {
this.request = request;
}

public SignInWithCredentialAidlRequest() {
}

@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
CREATOR.writeToParcel(this, dest, flags);
}

public static final SafeParcelableCreatorAndWriter<SignInWithCredentialAidlRequest> CREATOR = findCreator(SignInWithCredentialAidlRequest.class);

public class SignInWithCredentialAidlRequest extends AutoSafeParcelable {
public static final Creator<SignInWithCredentialAidlRequest> CREATOR = new AutoCreator<>(SignInWithCredentialAidlRequest.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,55 @@

package com.google.firebase.auth.api.internal;

import org.microg.safeparcel.AutoSafeParcelable;
import android.os.Parcel;

import androidx.annotation.NonNull;

import com.google.android.gms.common.internal.safeparcel.AbstractSafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
import com.google.android.gms.common.internal.safeparcel.SafeParcelableCreatorAndWriter;

@SafeParcelable.Class
public class VerifyAssertionRequest extends AbstractSafeParcelable {

@Field(2)
public String requestUri;
@Field(3)
public String accessToken;
@Field(4)
public String idToken;
@Field(5)
public String instanceId;
@Field(6)
public String providerId;
@Field(7)
public String pendingIdToken;
@Field(8)
public String postBody;
@Field(9)
public String localId;
@Field(10)
public boolean returnIdpCredential;
@Field(11)
public boolean returnSecureToken;
@Field(12)
public String delegatedProjectNumber;
@Field(13)
public String sessionId;
@Field(14)
public String queryParameter;
@Field(15)
public String tenantId;
@Field(16)
public boolean returnRefreshToken;
@Field(17)
public String tenantProjectNumber;

@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
CREATOR.writeToParcel(this, dest, flags);
}

public static final SafeParcelableCreatorAndWriter<VerifyAssertionRequest> CREATOR = findCreator(VerifyAssertionRequest.class);

public class VerifyAssertionRequest extends AutoSafeParcelable {
public static final Creator<VerifyAssertionRequest> CREATOR = new AutoCreator<>(VerifyAssertionRequest.class);
}

0 comments on commit 2fae497

Please sign in to comment.