Skip to content

Commit 564d433

Browse files
committed
add authcontxt params to the Event class for Realtime Database
1 parent a5f82a7 commit 564d433

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/firebase_functions/db_fn.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
_event_type_updated = "google.firebase.database.ref.v1.updated"
3535
_event_type_deleted = "google.firebase.database.ref.v1.deleted"
3636

37+
AuthType = _typing.Literal["app_user", "admin", "unauthenticated", "unknown"]
38+
3739

3840
@_dataclass.dataclass(frozen=True)
3941
class Event(_core.CloudEvent[T]):
@@ -67,6 +69,16 @@ class Event(_core.CloudEvent[T]):
6769
Only named capture groups are populated - {key}, {key=*}, {key=**}
6870
"""
6971

72+
auth_type: AuthType
73+
"""
74+
The type of principal that triggered the event.
75+
"""
76+
77+
auth_id: str
78+
"""
79+
The unique identifier for the principal.
80+
"""
81+
7082

7183
_E1 = Event[Change[_typing.Any | None]]
7284
_E2 = Event[_typing.Any | None]
@@ -104,6 +116,10 @@ def _db_endpoint_handler(
104116
**ref_pattern.extract_matches(event_ref),
105117
**instance_pattern.extract_matches(event_instance),
106118
}
119+
120+
event_auth_type = event_attributes["authtype"]
121+
event_auth_id = event_attributes["authid"]
122+
107123
database_event = Event(
108124
firebase_database_host=event_attributes["firebasedatabasehost"],
109125
instance=event_instance,
@@ -120,6 +136,8 @@ def _db_endpoint_handler(
120136
data=database_event_data,
121137
subject=event_attributes["subject"],
122138
params=params,
139+
auth_type=event_auth_type,
140+
auth_id=event_auth_id,
123141
)
124142
_core._with_init(func)(database_event)
125143

tests/test_db.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ def init():
3838
"ref": "ref",
3939
"firebasedatabasehost": "firebasedatabasehost",
4040
"location": "location",
41+
"authtype": "app_user",
42+
"authid": "auth-id",
4143
},
4244
data={"delta": "delta"},
4345
)
4446

4547
decorated_func(event)
4648

49+
func.assert_called_once()
50+
event = func.call_args.args[0]
51+
self.assertIsNotNone(event)
52+
self.assertEqual(event.auth_type, "app_user")
53+
self.assertEqual(event.auth_id, "auth-id")
54+
4755
self.assertEqual(hello, "world")

0 commit comments

Comments
 (0)