Hi,
when testing/evaluating rules in "firestore simulator" with custom claims on the auth-object and a login with email and password (I guess it won't work for other logins, too?) the custom claims connected to the uid are not added to the auth-token/object and the rules (actually read/write-operations) fail. When reading data logged into the real application, the rules work fine. For testing this feels like a nightmare.
What do I mean?
I have following rule defined:
match /users/{userId} {
allow read: if isSignedIn() && (request.auth.uid == userId || (isManager() && resource.data.ns == getNamespace()));
allow write: if isSignedIn() && (request.auth.uid == userId || (isManager() && request.resource.data.ns == getNamespace()));
}
and following helper functions:
//Functions
function isSignedIn() {
return request.auth != null;
}
function isManager() {
}
function getNamespace() {
return request.auth.token.ns;
}
In the simulator area i chose "authenticated" and my provider is "password". I then use a uid of a user who has the claims used (
request.auth.token.pm == true and request.auth.token.ns corresponds to the "ns"-field in the data of the user i try to read) and a valid password.
I try to read a user with matching "ns/namespace" but the evaluation of the rule already fails on "isManager()" even though my user does have the right claim.
The Authentication-Payload:
{
"uid": "9mcyD3RodwMpJ89Sasomemoreboring",
"token": {
"sub": "9mcyD3RodwMpJ89Sasomemoreboring",
"aud": "some-project-id-123",
"email_verified": true,
"firebase": {
"sign_in_provider": "password"
}
}
}
Here it becomes clear, that the used authentication-payload by the simulator does not contain the custom claims set on the firebase-account for that uid. For better testability of firestore-rules with the in-browser-simulator it would be great when the auth-payload would contain the custom claims.
Thank you very much and kind regards
Malte Redschlag