Skip to content

Commit a2595ab

Browse files
committed
Add auth context
1 parent 733bd05 commit a2595ab

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/firebase_functions/dataconnect_fn.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
_event_type_mutation_executed = "google.firebase.dataconnect.connector.v1.mutationExecuted"
3232

33+
AuthType = _typing.Literal["app_user", "admin", "unknown"]
3334

3435
@_dataclass.dataclass(frozen=True)
3536
class Event(_core.CloudEvent[_core.T]):
@@ -53,6 +54,15 @@ class Event(_core.CloudEvent[_core.T]):
5354
Only named capture groups are populated - {key}, {key=*}, {key=**}
5455
"""
5556

57+
auth_type: AuthType
58+
"""
59+
The type of principal that triggered the event.
60+
"""
61+
62+
auth_id: str
63+
"""
64+
The unique identifier for the principal.
65+
"""
5666

5767
@_dataclass.dataclass(frozen=True)
5868
class GraphqlErrorExtensions:
@@ -210,6 +220,9 @@ def _dataconnect_endpoint_handler(
210220
**operation_pattern.extract_matches(event_operation),
211221
}
212222

223+
event_auth_type = event_attributes["authtype"]
224+
event_auth_id = event_attributes["authid"]
225+
213226
dataconnect_event = Event(
214227
specversion=event_attributes["specversion"],
215228
id=event_attributes["id"],
@@ -224,6 +237,8 @@ def _dataconnect_endpoint_handler(
224237
project=event_attributes["project"],
225238
params=params,
226239
data=dataconnect_event_data,
240+
auth_type=event_auth_type,
241+
auth_id=event_auth_id,
227242
)
228243
_core._with_init(func)(dataconnect_event)
229244

tests/test_dataconnect_fn.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def init():
114114
"service": "service-id",
115115
"connector": "connector-id",
116116
"operation": "mutation-name",
117+
"authtype": "app_user",
118+
"authid": "auth-id"
117119
},
118120
data=json.dumps({}),
119121
)
@@ -124,4 +126,12 @@ def init():
124126
)(func)
125127
decorated_func(event)
126128

129+
func.assert_called_once()
130+
event = func.call_args.args[0]
131+
self.assertIsNotNone(event)
132+
self.assertEqual(event.project, "project-id")
133+
self.assertEqual(event.location, "location-id")
134+
self.assertEqual(event.auth_type, "app_user")
135+
self.assertEqual(event.auth_id, "auth-id")
136+
127137
self.assertEqual(hello, "world")

0 commit comments

Comments
 (0)