Repository navigation
fix(emulator): align v2 Auth CloudEvent payload with Eventarc wire format - #11323
Conversation
…rmat ### Description Updates the Auth Emulator's 2nd Gen CloudEvent payload (`google.firebase.auth.user.v2.created` and `google.firebase.auth.user.v2.deleted`) to emit a dedicated `V2UserInfoPayload` matching the canonical `google.events.firebase.auth.v2.AuthEventData` protobuf schema (`metadata.createTime` and `providerData[].uid`), while keeping the 1st Gen `UserInfoPayload` (`metadata.creationTime` and `providerData[].rawId`) unchanged. ### Scenarios Tested - Unit tests in `src/emulator/auth/cloudFunctions.spec.ts` verifying both 1st Gen and 2nd Gen payloads on user creation and deletion. - End-to-end verification with the Functions Emulator. ### Sample Commands `firebase emulators:start --only auth,functions`
There was a problem hiding this comment.
Code Review
This pull request updates the 2nd Gen Auth Emulator CloudEvent payloads to align with the Eventarc wire format, specifically using 'metadata.createTime' and 'providerData[].uid' for v2 events. It refactors payload creation by introducing base and v2-specific payload types and helper functions. Feedback is provided to improve the robustness of the 'toIsoTimestamp' helper by safely handling invalid numeric strings to prevent potential runtime crashes from 'RangeError' exceptions.
| private toIsoTimestamp(epochMs?: string): string | undefined { | ||
| return epochMs ? new Date(parseInt(epochMs, 10)).toISOString() : undefined; | ||
| } |
There was a problem hiding this comment.
If epochMs is not a valid numeric string, parseInt will return NaN. Passing NaN to new Date() and calling toISOString() on it will throw a RangeError: Invalid time value, which could crash the emulator. It is safer to check if the parsed value is a valid number before calling toISOString().
private toIsoTimestamp(epochMs?: string): string | undefined {
if (!epochMs) {
return undefined;
}
const ms = parseInt(epochMs, 10);
return isNaN(ms) ? undefined : new Date(ms).toISOString();
}
ajperel
left a comment
There was a problem hiding this comment.
Overall looks pretty good. Thanks for fixing this and testing so thoroughly. Just had a few nits.
…emulator-auth-v2-payload # Conflicts: # CHANGELOG.md
Description
Fixes the Auth Emulator's 2nd Gen CloudEvent payload to strictly conform to the canonical Google Cloud Eventarc protobuf schema (
google.events.firebase.auth.v2.AuthEventData).Background & Root Cause
Previously,
AuthCloudFunction.dispatchreused the legacy 1st GenUserInfoPayloadbuilder for both 1st Gen and 2nd Gen events. Because 1st Gen Auth triggers use a non-standard wire format (functions.auth.user), the 2nd Gen CloudEvents emitted by the emulator diverged from live production Eventarc events:metadata.creationTimeinstead of protobuf canonicalmetadata.createTime.providerData[].rawIdinstead ofproviderData[].uid, causingevent.data.providerData[n].uidin SDKs (such asfirebase-functions/v2/identity) to evaluate toundefinedin local emulation while functioning in production.tenantIdinsidedata.value/data.oldValue. In the Eventarc protobuf specification (events.proto),tenantidis strictly an extension attribute on the CloudEvent envelope, not a field onUser(data.proto).mfaInfoandproviderData[].federatedId, which are not defined in thegoogle.events.firebase.auth.v2.Userproto.Changes
BaseUserInfoPayloadand created a dedicatedV2UserInfoPayloadadhering togoogle.events.firebase.auth.v2.User.metadata.createTimeandmetadata.lastSignInTimeformatted as ISO-8601 strings.providerData[].uidmapped frominfo.rawId.tenantIdandmfaInfofrom the v2 data payload (data.value/data.oldValue), while retaining top-levelcloudEvent.tenantid = user.tenantIdfor attribute-based routing.UserInfoPayload(createEventRequestBody) completely unchanged.Compatibility & Impact
firebase-functions): Fully compatible.convertV2EventToV1Eventinfirebase-functions/v2/identityalready supportscreateTimeand extractstenantidfrom the top-level envelope. This change fixesproviderData[].uidevaluation in the emulator.firebase-functions-python): Unblocks canonical Eventarc ingestion for upcoming v2 Auth triggers (on_user_created,on_user_deletedin PR "TypeError: Not an integer" since upgrade to 3.0.5 #318 / Add App to Project #319).legacyEventBodyremains identical).Scenarios Tested
src/emulator/auth/cloudFunctions.spec.tsverifying both 1st Gen legacy and 2nd Gen CloudEvent payloads oncreateanddelete.providerData[].uidandmetadata.createTimepresence in 2nd Gen payload.tenantidCloudEvent extension attribute presence and omission oftenantIdfromoldValueon user deletion.trigger_multicast) receiving valid CloudEvents.Sample Commands
npm test src/emulator/auth/cloudFunctions.spec.ts firebase emulators:start --only auth,functions