Skip to content

fix(emulator): align v2 Auth CloudEvent payload with Eventarc wire format - #11323

Merged
shettyvarun268 merged 5 commits into
mainfrom
shettyvarun268/align-emulator-auth-v2-payload
Oct 21, 2026
Merged

shettyvarun268 merged 5 commits into
mainfrom
shettyvarun268/align-emulator-auth-v2-payload

Conversation

@shettyvarun268

@shettyvarun268 shettyvarun268 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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.dispatch reused the legacy 1st Gen UserInfoPayload builder 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:

  1. Metadata Timestamps: Emitted metadata.creationTime instead of protobuf canonical metadata.createTime.
  2. Provider Data: Emitted providerData[].rawId instead of providerData[].uid, causing event.data.providerData[n].uid in SDKs (such as firebase-functions/v2/identity) to evaluate to undefined in local emulation while functioning in production.
  3. Tenant ID Placement: Emitted tenantId inside data.value / data.oldValue. In the Eventarc protobuf specification (events.proto), tenantid is strictly an extension attribute on the CloudEvent envelope, not a field on User (data.proto).
  4. Non-Protobuf Fields: Included mfaInfo and providerData[].federatedId, which are not defined in the google.events.firebase.auth.v2.User proto.

Changes

  • Separated Payloads: Extracted BaseUserInfoPayload and created a dedicated V2UserInfoPayload adhering to google.events.firebase.auth.v2.User.
  • Field Normalization:
    • metadata.createTime and metadata.lastSignInTime formatted as ISO-8601 strings.
    • providerData[].uid mapped from info.rawId.
    • Removed tenantId and mfaInfo from the v2 data payload (data.value / data.oldValue), while retaining top-level cloudEvent.tenantid = user.tenantId for attribute-based routing.
  • Legacy Safety: Kept 1st Gen UserInfoPayload (createEventRequestBody) completely unchanged.

Compatibility & Impact

  • Node.js SDK (firebase-functions): Fully compatible. convertV2EventToV1Event in firebase-functions/v2/identity already supports createTime and extracts tenantid from the top-level envelope. This change fixes providerData[].uid evaluation in the emulator.
  • Python SDK (firebase-functions-python): Unblocks canonical Eventarc ingestion for upcoming v2 Auth triggers (on_user_created, on_user_deleted in PR "TypeError: Not an integer" since upgrade to 3.0.5 #318 / Add App to Project #319).
  • 1st Gen Functions: No impact (legacyEventBody remains identical).

Scenarios Tested

  • Unit tests in src/emulator/auth/cloudFunctions.spec.ts verifying both 1st Gen legacy and 2nd Gen CloudEvent payloads on create and delete.
  • Verified providerData[].uid and metadata.createTime presence in 2nd Gen payload.
  • Verified tenantid CloudEvent extension attribute presence and omission of tenantId from oldValue on user deletion.
  • End-to-end verification with Functions Emulator (trigger_multicast) receiving valid CloudEvents.

Sample Commands

npm test src/emulator/auth/cloudFunctions.spec.ts
firebase emulators:start --only auth,functions

…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`

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +123 to +125
private toIsoTimestamp(epochMs?: string): string | undefined {
return epochMs ? new Date(parseInt(epochMs, 10)).toISOString() : undefined;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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();
  }

@shettyvarun268 shettyvarun268 changed the title fix(emulator): align v2 Auth CloudEvent payload with Eventarc wire fo… fix(emulator): align v2 Auth CloudEvent payload with Eventarc wire format Oct 21, 2026
@shettyvarun268
shettyvarun268 marked this pull request as ready for review October 21, 2026 18:29

@ajperel ajperel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks pretty good. Thanks for fixing this and testing so thoroughly. Just had a few nits.

Comment thread CHANGELOG.md Resolved
Comment thread src/emulator/auth/cloudFunctions.ts Resolved
Comment thread src/emulator/auth/cloudFunctions.ts Resolved
Comment thread src/emulator/auth/cloudFunctions.ts Resolved
@shettyvarun268
shettyvarun268 enabled auto-merge (squash) October 21, 2026 21:55
@shettyvarun268
shettyvarun268 merged commit d376f5d into main Oct 21, 2026
51 checks passed
@shettyvarun268
shettyvarun268 deleted the shettyvarun268/align-emulator-auth-v2-payload branch October 21, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants