Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/v2/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const RAW_RTDB_EVENT: database.RawRTDBCloudEvent = {
specversion: "1.0",
time: "time",
type: "type",
authid: "uid",
authtype: "unauthenticated",
};

describe("database", () => {
Expand Down Expand Up @@ -461,6 +463,19 @@ describe("database", () => {
await database.onValueWritten("path", () => null)(event);
expect(hello).to.equal("world");
});

it("should pass auth context into the event", async () => {
const raw = {
...RAW_RTDB_EVENT,
};

const func = database.onValueWritten("foo/bar", (event) => {
expect(event.authid).to.equal("uid");
expect(event.authtype).to.equal("unauthenticated");
});

await func(raw);
});
});

describe("onValueCreated", () => {
Expand Down
21 changes: 21 additions & 0 deletions src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,19 @@ export interface RawRTDBCloudEvent extends CloudEvent<RawRTDBCloudEventData> {
instance: string;
ref: string;
location: string;
authtype: AuthType;
authid?: string;
}

/**
* AuthType defines the possible values for the authType field in a Realtime Database event.
* - app_user: an end user of an application..
* - admin: an admin user of an application. In the context of impersonate endpoints used by the admin SDK, the impersonator.
* - unauthenticated: no credentials were used to authenticate the change that triggered the occurrence.
* - unknown: a general type to capture all other principals not captured in the other auth types.
*/
export type AuthType = "app_user" | "admin" | "unauthenticated" | "unknown";

/** A CloudEvent that contains a DataSnapshot or a Change<DataSnapshot> */
export interface DatabaseEvent<T, Params = Record<string, string>> extends CloudEvent<T> {
/** The domain of the database instance */
Expand All @@ -80,6 +91,14 @@ export interface DatabaseEvent<T, Params = Record<string, string>> extends Cloud
* Only named capture groups will be populated - {key}, {key=*}, {key=**}
*/
params: Params;
/**
* The type of principal that triggered the event.
*/
authtype: AuthType;
/**
* The unique identifier of the principal.
*/
authid?: string;
}

/** ReferenceOptions extend EventHandlerOptions with provided ref and optional instance */
Expand Down Expand Up @@ -384,6 +403,8 @@ function makeDatabaseEvent<Params>(
firebaseDatabaseHost: event.firebasedatabasehost,
data: snapshot,
params,
authtype: event.authtype,
authid: event.authid,
};
delete (databaseEvent as any).firebasedatabasehost;
return databaseEvent;
Expand Down
Loading