Skip to content

Commit 3bf4939

Browse files
authored
feat(fdc): Add support for Data Connect Impersonation (#2844)
* Add support for Data Connect Impersonation * Add integration tests for impersonation * fix: export new interfaces and regenerate apidocs * fix: remove link to DecodedIdToken * remove test service id * Export alias type and update test descriptions * Revert accidental rebase * Export alias type `AuthClaims` and update test descriptions * Change token format link * Update tests to check each index values * Equality checks for tests * More equality checks ---------
1 parent 2379e15 commit 3bf4939

File tree

5 files changed

+301
-42
lines changed

5 files changed

+301
-42
lines changed

etc/firebase-admin.data-connect.api.md

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
import { Agent } from 'http';
88

9+
// Warning: (ae-forgotten-export) The symbol "DecodedIdToken" needs to be exported by the entry point index.d.ts
10+
//
11+
// @public
12+
export type AuthClaims = Partial<DecodedIdToken>;
13+
914
// @public
1015
export interface ConnectorConfig {
1116
location: string;
@@ -36,8 +41,21 @@ export function getDataConnect(connectorConfig: ConnectorConfig, app?: App): Dat
3641

3742
// @public
3843
export interface GraphqlOptions<Variables> {
44+
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
3945
operationName?: string;
4046
variables?: Variables;
4147
}
4248

49+
// @public
50+
export interface ImpersonateAuthenticated {
51+
authClaims: AuthClaims;
52+
unauthenticated?: never;
53+
}
54+
55+
// @public
56+
export interface ImpersonateUnauthenticated {
57+
authClaims?: never;
58+
unauthenticated: true;
59+
}
60+
4361
```

src/data-connect/data-connect-api-client-internal.ts

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class DataConnectApiClient {
110110
query,
111111
...(options?.variables && { variables: options?.variables }),
112112
...(options?.operationName && { operationName: options?.operationName }),
113+
...(options?.impersonate && { extensions: { impersonate: options?.impersonate } }),
113114
};
114115
return this.getUrl(API_VERSION, this.connectorConfig.location, this.connectorConfig.serviceId, endpoint)
115116
.then(async (url) => {

src/data-connect/data-connect-api.ts

+50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { DecodedIdToken } from '../auth/token-verifier';
19+
1820
/**
1921
* Interface representing a Data Connect connector configuration.
2022
*/
@@ -53,4 +55,52 @@ export interface GraphqlOptions<Variables> {
5355
* The name of the GraphQL operation. Required only if `query` contains multiple operations.
5456
*/
5557
operationName?: string;
58+
59+
/**
60+
* If set, impersonate a request with given Firebase Auth context and evaluate the auth
61+
* policies on the operation. If omitted, bypass any defined auth policies.
62+
*/
63+
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
64+
}
65+
66+
/**
67+
* Type representing the partial claims of a Firebase Auth token used to evaluate the
68+
* Data Connect auth policy.
69+
*/
70+
export type AuthClaims = Partial<DecodedIdToken>;
71+
72+
/**
73+
* Interface representing the impersonation of an authenticated user.
74+
*/
75+
export interface ImpersonateAuthenticated {
76+
/**
77+
* Evaluate the auth policy with a customized JWT auth token. Should follow the Firebase Auth token format.
78+
* https://firebase.google.com/docs/data-connect/cel-reference#auth-token-contents
79+
*
80+
* @example A verified user may have the following `authClaims`:
81+
* ```json
82+
* { "sub": "uid", "email_verified": true }
83+
* ```
84+
*/
85+
authClaims: AuthClaims;
86+
87+
/**
88+
* Both `authClaims` and `unauthenticated` are mutually exclusive fields and should not be both set.
89+
*/
90+
unauthenticated?: never;
91+
}
92+
93+
/**
94+
* Interface representing the impersonation of an unauthenticated user.
95+
*/
96+
export interface ImpersonateUnauthenticated {
97+
/**
98+
* Both `authClaims` and `unauthenticated` are mutually exclusive fields and should not be both set.
99+
*/
100+
authClaims?: never;
101+
102+
/**
103+
* Evaluates the auth policy as an unauthenticated request. Can only be set to true.
104+
*/
105+
unauthenticated: true;
56106
}

src/data-connect/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export {
3030
GraphqlOptions,
3131
ExecuteGraphqlResponse,
3232
ConnectorConfig,
33+
ImpersonateAuthenticated,
34+
ImpersonateUnauthenticated,
35+
AuthClaims
3336
} from './data-connect-api'
3437
export {
3538
DataConnect,

0 commit comments

Comments
 (0)