Permission Denied Error in Android Emulator when Enforcing App Check for Flutter App

Viewed 272

After spending hours for getting App Check working on the physical devices (iOS and Android) of my Flutter app i also tried the provided documentation steps in order to get it working for Android emulator as well. Whatever i try i always get the following error for all my listening streams that are invoked against the Firestore database when App Check is enforced:

W/Firestore( 5612): (24.1.2) [Firestore]: Listen for Query(target=Query(Contract where isArchive==false and contractorId==myContractorId order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

I've added the depencency in app/build.gradle:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0'
}

and have overridden onCreate in MainActivity.kt:

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        if (BuildConfig.DEBUG) {
            //Log.d("MainActivity", "debug mode entered")
            FirebaseApp.initializeApp( /*context=*/this)
            val firebaseAppCheck: FirebaseAppCheck = FirebaseAppCheck.getInstance()
            firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance())
        } else {
            //Log.d("MainActivity", "else mode entered")
        }
        super.onCreate(savedInstanceState)
    }
}

The resulting debug token was added successfully to the list of debug tokens in Firebase console. All Firestore collection rules look like the following example rule:

match /Contractor/{contractor} {
  allow read, write: if request.auth.uid != null;
}

Activating App Check in the Flutter main function looks like this:

await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseAppCheck.instance.activate();

Without enforced App Check i don't have any problems running the Flutter app on my Android emulator. All Firebase plugins are the most recent ones. What else can i try or change in order to get this working? If it doesn't work also on Android emulators we will give up using App Check.

1 Answers

Have you added this in cloud function:

  if (context.app == undefined && !process.env.FUNCTIONS_EMULATOR) {
    throw new functions.https.HttpsError(
        "failed-precondition",
        "The function must be called from an App-Check verified app.");
  }

You need to add this code:

!process.env.FUNCTIONS_EMULATOR
Related