|
1 | 1 | import '../common/cloud_event.dart'; |
2 | 2 |
|
| 3 | +/// The type of principal that triggered the event. |
| 4 | +/// |
| 5 | +/// Matches the Node.js SDK's `AuthType` type. |
| 6 | +enum AuthType { |
| 7 | + /// A non-user principal used to identify a workload or machine user. |
| 8 | + serviceAccount('service_account'), |
| 9 | + |
| 10 | + /// A non-user client API key. |
| 11 | + apiKey('api_key'), |
| 12 | + |
| 13 | + /// An obscured identity used when Cloud Platform or another system |
| 14 | + /// triggered the event (e.g., TTL-based database deletion). |
| 15 | + system('system'), |
| 16 | + |
| 17 | + /// An unauthenticated action. |
| 18 | + unauthenticated('unauthenticated'), |
| 19 | + |
| 20 | + /// A general type to capture all other principals not captured |
| 21 | + /// in other auth types. |
| 22 | + unknown('unknown'); |
| 23 | + |
| 24 | + const AuthType(this.value); |
| 25 | + |
| 26 | + /// The wire value as sent in CloudEvent headers. |
| 27 | + final String value; |
| 28 | + |
| 29 | + /// Parses an [AuthType] from the wire value string. |
| 30 | + /// |
| 31 | + /// Returns [AuthType.unknown] if the value is not recognized. |
| 32 | + static AuthType fromString(String value) => switch (value) { |
| 33 | + 'service_account' => AuthType.serviceAccount, |
| 34 | + 'api_key' => AuthType.apiKey, |
| 35 | + 'system' => AuthType.system, |
| 36 | + 'unauthenticated' => AuthType.unauthenticated, |
| 37 | + _ => AuthType.unknown, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
3 | 41 | /// A CloudEvent that contains a DocumentSnapshot or a `Change<DocumentSnapshot>`. |
4 | 42 | /// |
5 | 43 | /// This event type extends the base [CloudEvent] with Firestore-specific fields. |
@@ -93,3 +131,58 @@ class FirestoreEvent<T extends Object?> extends CloudEvent<T> { |
93 | 131 | return json; |
94 | 132 | } |
95 | 133 | } |
| 134 | + |
| 135 | +/// A [FirestoreEvent] that includes authentication context. |
| 136 | +/// |
| 137 | +/// This event type is used by the `WithAuthContext` trigger variants |
| 138 | +/// (e.g., [FirestoreNamespace.onDocumentCreatedWithAuthContext]). |
| 139 | +/// |
| 140 | +/// The auth context identifies which principal triggered the Firestore write. |
| 141 | +/// Note: this is different from HTTPS callable auth — it identifies the |
| 142 | +/// principal type (service account, API key, etc.), not a Firebase Auth user. |
| 143 | +/// |
| 144 | +/// Example: |
| 145 | +/// ```dart |
| 146 | +/// firebase.firestore.onDocumentCreatedWithAuthContext( |
| 147 | +/// document: 'users/{userId}', |
| 148 | +/// (event) async { |
| 149 | +/// print('Auth type: ${event.authType}'); |
| 150 | +/// print('Auth ID: ${event.authId}'); |
| 151 | +/// }, |
| 152 | +/// ); |
| 153 | +/// ``` |
| 154 | +class FirestoreAuthEvent<T extends Object?> extends FirestoreEvent<T> { |
| 155 | + const FirestoreAuthEvent({ |
| 156 | + super.data, |
| 157 | + required super.id, |
| 158 | + required super.source, |
| 159 | + required super.specversion, |
| 160 | + super.subject, |
| 161 | + required super.time, |
| 162 | + required super.type, |
| 163 | + required super.location, |
| 164 | + required super.project, |
| 165 | + required super.database, |
| 166 | + required super.namespace, |
| 167 | + required super.document, |
| 168 | + required super.params, |
| 169 | + required this.authType, |
| 170 | + this.authId, |
| 171 | + }); |
| 172 | + |
| 173 | + /// The type of principal that triggered the event. |
| 174 | + final AuthType authType; |
| 175 | + |
| 176 | + /// The unique identifier for the principal. |
| 177 | + /// |
| 178 | + /// May be `null` for system-triggered or unauthenticated events. |
| 179 | + final String? authId; |
| 180 | + |
| 181 | + @override |
| 182 | + Map<String, dynamic> toJson(Map<String, dynamic> Function(T) dataEncoder) { |
| 183 | + final json = super.toJson(dataEncoder); |
| 184 | + json['authType'] = authType.value; |
| 185 | + if (authId != null) json['authId'] = authId; |
| 186 | + return json; |
| 187 | + } |
| 188 | +} |
0 commit comments