fxa/packages/fxa-event-broker
dependabot[bot] e6bb0d7c37
chore(deps-dev): bump eslint-config-prettier from 8.3.0 to 8.7.0
Bumps [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) from 8.3.0 to 8.7.0.
- [Release notes](https://github.com/prettier/eslint-config-prettier/releases)
- [Changelog](https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/eslint-config-prettier/compare/v8.3.0...v8.7.0)

---
updated-dependencies:
- dependency-name: eslint-config-prettier
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-03-07 07:03:02 +00:00
..
.vscode chore(multiple): update mocha wherever used 2022-06-28 09:03:42 -07:00
config feat(event-broker): wire up events from auth-server through event-broker to webhooks in local dev 2021-12-15 16:00:50 -08:00
scripts task(CI): build, unit test, and integration test jobs 2023-01-10 12:52:01 -08:00
src task(ci): Tag unit tests 2022-12-09 17:26:27 -08:00
test task(CI): build, unit test, and integration test jobs 2023-01-10 12:52:01 -08:00
.nsprc chore(deps): Updates to address nsp advisory 1179 2020-03-19 10:42:24 -07:00
CHANGELOG.md Merge pull request #13572 from mozilla/FXA-5496-stop-generating-changelogs-in-our-release-script 2022-07-11 16:20:36 -07:00
LICENSE feat(packages): create fxa-event-broker skeleton rust project 2019-04-11 13:16:05 +01:00
README.md feat(event-broker): add email to profile-change event 2021-12-21 16:36:56 -08:00
architecture.md feat(event-broker): add remaining RP events 2019-12-05 14:28:40 -08:00
nest-cli.json feat(event-broker): migrate to NestJS 2020-08-31 12:59:54 -07:00
package.json chore(deps-dev): bump eslint-config-prettier from 8.3.0 to 8.7.0 2023-03-07 07:03:02 +00:00
pm2.config.js task(admin-panel): Instrument event-broker with tracing 2022-09-15 09:50:28 -07:00
tsconfig.build.json feat(fxa-shared): refactor nestjs shared modules 2020-09-15 08:07:43 -07:00
tsconfig.json feat(admin-server): convert to NestJS 2020-09-18 12:04:14 -07:00

README.md

fxa-event-broker

The Firefox Accounts Event Broker (fxa-event-broker) is a webhook event delivery system for events in Firefox Accounts. These events are currently consumed from an internal priviledged queue that FxA services publish to, screened to determine what Relying Parties (RPs) should receive them, then distributed via webhooks using the Security Event Token (SET) format.

This event broker also stores event metadata, tracking which RPs a user has logged into to screen event delivery to relevant RPs.

Relying Party Event Format

A relying party will get webhook calls for events. These events are encoded in SETs with the following formats. See the SET RFC for definitions and other examples.

Password Change

Sent when a user has reset or changed their password. Services receiving this event should terminate user login sessions that were established prior to the event.

  • Event Identifier
    • https://schemas.accounts.firefox.com/event/password-change
  • Event Payload
    • [Password Event Identifier]
      • changeTime
        • Time when the password reset took place. All logins established before this time should be terminated.

Example Password Change Event

{
 "iss": "https://accounts.firefox.com/",
 "sub": "FXA_USER_ID",
 "aud": "REMOTE_SYSTEM",
 "iat": 1565720808,
 "jti": "e19ed6c5-4816-4171-aa43-56ffe80dbda1",
 "events": {
   "https://schemas.accounts.firefox.com/event/password-change": {
       "changeTime": 1565721242227
   }
 }

Profile Change

Sent when a user has changed their profile data in some manner. Updates to their profile may include a new primary email address, display name, or 2FA status. This event does not include what changed and the profile data a service has access to may not show any changes if the data changed was outside the OAuth scope the service was granted.

Services should update any cached profile data they hold about the user.

  • Event Identifier
    • https://schemas.accounts.firefox.com/event/profile-change
  • Event Payload
    • [Profile Event Identifier]
      • email
        • The new primary email address for the given user (sub)
        • This property will be undefined if the email hasn't changed

Example Profile Change Event

{
 "iss": "https://accounts.firefox.com/",
 "sub": "FXA_USER_ID",
 "aud": "REMOTE_SYSTEM",
 "iat": 1565720808,
 "jti": "e19ed6c5-4816-4171-aa43-56ffe80dbda1",
 "events": {
   "https://schemas.accounts.firefox.com/event/profile-change": {
     "email": "example@mozilla.com"
   }
 }

Subscription State Change

Sent when a user's subscription state has changed to RPs that provide the changed subscription capability.

NOTE: There are strict requirements about subscription state change handling based on the changeTime as documented below.

  • Event Identifier
    • https://schemas.accounts.firefox.com/event/subscription-state-change
  • Event Payload
    • [Subscription Event Identifier]
      • capabilities
        • List of subscription capabilities
      • isActive
        • Boolean indicating if the subscription should be considered active or not for the subscription capabilities provided.
      • changeTime
        • Time in seconds when the state change occured in the payment system. This value MUST be tracked by the receiving system, and events with a changeTime older than the last tracked time MUST be discarded.

Example Subscription State Change Event

{
 "iss": "https://accounts.firefox.com/",
 "sub": "FXA_USER_ID",
 "aud": "REMOTE_SYSTEM",
 "iat": 1565720808,
 "jti": "e19ed6c5-4816-4171-aa43-56ffe80dbda1",
 "events": {
   "https://schemas.accounts.firefox.com/event/subscription-state-change": {
       "capabilities": ["capability_1", "capability_2"],
       "isActive": true,
       "changeTime": 1565721242227
   }
 }

Delete User

Sent when a user has been deleted from Firefox Accounts. RPs MUST delete all user records for the given user when receiving this event.

  • Event Identifier
    • https://schemas.accounts.firefox.com/event/delete-user
  • Event Payload
    • [Delete Event Identifier]
      • {}

Example Delete Event

{
 "iss": "https://accounts.firefox.com/",
 "sub": "FXA_USER_ID",
 "aud": "REMOTE_SYSTEM",
 "iat": 1565720810,
 "jti": "1b3d623a-300a-4ab8-9241-855c35586809",
 "events": {
   "https://schemas.accounts.firefox.com/event/delete-user": {}
 }

Deployment

Metrics

The Event Broker emits statsD style metrics. A statsD compatible host should be defined to capture these. All timings are in milliseconds.

Event Name Type Description
message.processing.total Timing Total time spent processing a service notification.
message.queueDelay Timing Time the event was in the service notification queue.
proxy.sub.queueDelay Timing Time the event was in the PubSub proxy queue.
message.sub.eventDelay Timing Time from Stripe subscription event until processed from queue.
proxy.sub.eventDelay Timing Time from Stripe subscription event until delivered to RP.
message.type.subscription Counter Subscription notification events.
message.type.login Counter Login notification events.
message.type.delete Counter Delete notification events.
proxy.success.CID.STATUS Counter Successful event delivery to a RP.
proxy.fail.CID.STATUS Counter Failed event delivery to a RP.

CID - Client Id of Relying Party.

Testing a Webhook URL

To verify that a webhookUrl can process a payload correctly, the simulator can be used to generate a simulated JWT signed payload. The relying party can verify this worked correctly before a full deployment occurs.

Command Syntax:

node dist/scripts/simulate-webhook-call.js CLIENTID WEBHOOKURL CAPABILITIES

Where CAPABILITIES is a comma-seperated string of capabilities to include.

Usage

$ yarn build
$ export LOG_FORMAT=pretty
$ node dist/scripts/simulate-webhook-call.js a9238ba https://example.com/webhook capability_1
fxa-event-broker.simulateWebhookCall.INFO: webhookCall {"statusCode":200,"body":"ok\n"}
$

Software Architecture

The event-broker architecture document describes the overall flow of data from fxa-auth-server via SQS into event-broker and back out to relying parties (RPs). The mermaid diagrams found there can be copied into the mermaid live editor to view. Extensions are available for VS Code and other editors that allow local previews of the diagrams.

This package is built using NestJS and follows module/service/providor patterns as documented for a NestJS project.

Testing

This package uses Jest to test its code. By default yarn test will test all files ending in .spec.ts.

Test commands:

# Test with coverage
yarn test:cov

# Test on file change
yarn test:watch