Skip to content

Commit f0cb25f

Browse files
add authcontxt params to database_event (#270)
Co-authored-by: Kushal Palesha <3357451+kushalpalesha@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent a5f82a7 commit f0cb25f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/firebase_functions/db_fn.py

Lines changed: 15 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 | None
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,7 @@ def _db_endpoint_handler(
104116
**ref_pattern.extract_matches(event_ref),
105117
**instance_pattern.extract_matches(event_instance),
106118
}
119+
107120
database_event = Event(
108121
firebase_database_host=event_attributes["firebasedatabasehost"],
109122
instance=event_instance,
@@ -120,6 +133,8 @@ def _db_endpoint_handler(
120133
data=database_event_data,
121134
subject=event_attributes["subject"],
122135
params=params,
136+
auth_type=event_attributes.get("authtype", "unknown"),
137+
auth_id=event_attributes.get("authid"),
123138
)
124139
_core._with_init(func)(database_event)
125140

tests/test_db.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,46 @@ 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_arg = func.call_args.args[0]
51+
self.assertIsNotNone(event_arg)
52+
self.assertEqual(event_arg.auth_type, "app_user")
53+
self.assertEqual(event_arg.auth_id, "auth-id")
54+
4755
self.assertEqual(hello, "world")
56+
57+
def test_missing_auth_context(self):
58+
func = mock.Mock(__name__="example_func_no_auth")
59+
decorated_func = db_fn.on_value_created(reference="path")(func)
60+
61+
event = CloudEvent(
62+
attributes={
63+
"specversion": "1.0",
64+
"id": "id",
65+
"source": "source",
66+
"subject": "subject",
67+
"type": "type",
68+
"time": "2024-04-10T12:00:00.000Z",
69+
"instance": "instance",
70+
"ref": "ref",
71+
"firebasedatabasehost": "firebasedatabasehost",
72+
"location": "location",
73+
},
74+
data={"delta": "delta"},
75+
)
76+
77+
decorated_func(event)
78+
79+
func.assert_called_once()
80+
event_arg = func.call_args.args[0]
81+
self.assertIsNotNone(event_arg)
82+
self.assertEqual(event_arg.auth_type, "unknown")
83+
self.assertIsNone(event_arg.auth_id)

0 commit comments

Comments
 (0)