Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fdc): Add support for Data Connect Impersonation #2844

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions etc/firebase-admin.data-connect.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

import { Agent } from 'http';

// Warning: (ae-forgotten-export) The symbol "DecodedIdToken" needs to be exported by the entry point index.d.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to re-export the DecodedIdToken here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offline notes:

  • Exporting DecodedIdToken here would require more auth types to be exported in src/data-connect/index.ts to satisfy api doc generation which isn't ideal.
  • Exporting an aliased type works around this and better documents the type in the data connect context.

Exported the aliased type AuthClaims.

From my understanding it isn't necessary and the AuthClaims export alias should provide the necessary types. @lahirumaramba Correct me if i'm mistaken here.

//
// @public
export type AuthClaims = Partial<DecodedIdToken>;

// @public
export interface ConnectorConfig {
location: string;
Expand Down Expand Up @@ -36,8 +41,21 @@ export function getDataConnect(connectorConfig: ConnectorConfig, app?: App): Dat

// @public
export interface GraphqlOptions<Variables> {
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
operationName?: string;
variables?: Variables;
}

// @public
export interface ImpersonateAuthenticated {
authClaims: AuthClaims;
unauthenticated?: never;
}

// @public
export interface ImpersonateUnauthenticated {
authClaims?: never;
unauthenticated: true;
}

```
1 change: 1 addition & 0 deletions src/data-connect/data-connect-api-client-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class DataConnectApiClient {
query,
...(options?.variables && { variables: options?.variables }),
...(options?.operationName && { operationName: options?.operationName }),
...(options?.impersonate && { extensions: { impersonate: options?.impersonate } }),
};
return this.getUrl(API_VERSION, this.connectorConfig.location, this.connectorConfig.serviceId, endpoint)
.then(async (url) => {
Expand Down
50 changes: 50 additions & 0 deletions src/data-connect/data-connect-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import { DecodedIdToken } from '../auth/token-verifier';

/**
* Interface representing a Data Connect connector configuration.
*/
Expand Down Expand Up @@ -53,4 +55,52 @@ export interface GraphqlOptions<Variables> {
* The name of the GraphQL operation. Required only if `query` contains multiple operations.
*/
operationName?: string;

/**
* If set, impersonate a request with given Firebase Auth context and evaluate the auth
* policies on the operation. If omitted, bypass any defined auth policies.
*/
impersonate?: ImpersonateAuthenticated | ImpersonateUnauthenticated;
}

/**
* Type representing the partial claims of a Firebase Auth token used to evaluate the
* Data Connect auth policy.
*/
export type AuthClaims = Partial<DecodedIdToken>;

/**
* Interface representing the impersonation of an authenticated user.
*/
export interface ImpersonateAuthenticated {
/**
* Evaluate the auth policy with a customized JWT auth token. Should follow the Firebase Auth token format.
* https://firebase.google.com/docs/data-connect/cel-reference#auth-token-contents
*
* @example A verified user may have the following `authClaims`:
* ```json
* { "sub": "uid", "email_verified": true }
* ```
*/
authClaims: AuthClaims;

/**
* Both `authClaims` and `unauthenticated` are mutually exclusive fields and should not be both set.
*/
unauthenticated?: never;
}

/**
* Interface representing the impersonation of an unauthenticated user.
*/
export interface ImpersonateUnauthenticated {
/**
* Both `authClaims` and `unauthenticated` are mutually exclusive fields and should not be both set.
*/
authClaims?: never;

/**
* Evaluates the auth policy as an unauthenticated request. Can only be set to true.
*/
unauthenticated: true;
}
3 changes: 3 additions & 0 deletions src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export {
GraphqlOptions,
ExecuteGraphqlResponse,
ConnectorConfig,
ImpersonateAuthenticated,
ImpersonateUnauthenticated,
AuthClaims
} from './data-connect-api'
export {
DataConnect,
Expand Down
Loading