UNPKG

6.54 kBTypeScriptView Raw
1import { GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
2import { BodyResponseCallback } from '../transporters';
3import { Credentials } from './credentials';
4import { AuthClient } from './authclient';
5import { GetAccessTokenResponse, Headers, RefreshOptions } from './oauth2client';
6/**
7 * The maximum number of access boundary rules a Credential Access Boundary
8 * can contain.
9 */
10export declare const MAX_ACCESS_BOUNDARY_RULES_COUNT = 10;
11/**
12 * Offset to take into account network delays and server clock skews.
13 */
14export declare const EXPIRATION_TIME_OFFSET: number;
15/**
16 * Internal interface for tracking the access token expiration time.
17 */
18interface CredentialsWithResponse extends Credentials {
19 res?: GaxiosResponse | null;
20}
21/**
22 * Internal interface for tracking and returning the Downscoped access token
23 * expiration time in epoch time (seconds).
24 */
25interface DownscopedAccessTokenResponse extends GetAccessTokenResponse {
26 expirationTime?: number | null;
27}
28/**
29 * Defines an upper bound of permissions available for a GCP credential.
30 */
31export interface CredentialAccessBoundary {
32 accessBoundary: {
33 accessBoundaryRules: AccessBoundaryRule[];
34 };
35}
36/** Defines an upper bound of permissions on a particular resource. */
37interface AccessBoundaryRule {
38 availablePermissions: string[];
39 availableResource: string;
40 availabilityCondition?: AvailabilityCondition;
41}
42/**
43 * An optional condition that can be used as part of a
44 * CredentialAccessBoundary to further restrict permissions.
45 */
46interface AvailabilityCondition {
47 expression: string;
48 title?: string;
49 description?: string;
50}
51/**
52 * Defines a set of Google credentials that are downscoped from an existing set
53 * of Google OAuth2 credentials. This is useful to restrict the Identity and
54 * Access Management (IAM) permissions that a short-lived credential can use.
55 * The common pattern of usage is to have a token broker with elevated access
56 * generate these downscoped credentials from higher access source credentials
57 * and pass the downscoped short-lived access tokens to a token consumer via
58 * some secure authenticated channel for limited access to Google Cloud Storage
59 * resources.
60 */
61export declare class DownscopedClient extends AuthClient {
62 private readonly authClient;
63 private readonly credentialAccessBoundary;
64 private cachedDownscopedAccessToken;
65 private readonly stsCredential;
66 readonly eagerRefreshThresholdMillis: number;
67 readonly forceRefreshOnFailure: boolean;
68 /**
69 * Instantiates a downscoped client object using the provided source
70 * AuthClient and credential access boundary rules.
71 * To downscope permissions of a source AuthClient, a Credential Access
72 * Boundary that specifies which resources the new credential can access, as
73 * well as an upper bound on the permissions that are available on each
74 * resource, has to be defined. A downscoped client can then be instantiated
75 * using the source AuthClient and the Credential Access Boundary.
76 * @param authClient The source AuthClient to be downscoped based on the
77 * provided Credential Access Boundary rules.
78 * @param credentialAccessBoundary The Credential Access Boundary which
79 * contains a list of access boundary rules. Each rule contains information
80 * on the resource that the rule applies to, the upper bound of the
81 * permissions that are available on that resource and an optional
82 * condition to further restrict permissions.
83 * @param additionalOptions Optional additional behavior customization
84 * options. These currently customize expiration threshold time and
85 * whether to retry on 401/403 API request errors.
86 * @param quotaProjectId Optional quota project id for setting up in the
87 * x-goog-user-project header.
88 */
89 constructor(authClient: AuthClient, credentialAccessBoundary: CredentialAccessBoundary, additionalOptions?: RefreshOptions, quotaProjectId?: string);
90 /**
91 * Provides a mechanism to inject Downscoped access tokens directly.
92 * The expiry_date field is required to facilitate determination of the token
93 * expiration which would make it easier for the token consumer to handle.
94 * @param credentials The Credentials object to set on the current client.
95 */
96 setCredentials(credentials: Credentials): void;
97 getAccessToken(): Promise<DownscopedAccessTokenResponse>;
98 /**
99 * The main authentication interface. It takes an optional url which when
100 * present is the endpoint being accessed, and returns a Promise which
101 * resolves with authorization header fields.
102 *
103 * The result has the form:
104 * { Authorization: 'Bearer <access_token_value>' }
105 */
106 getRequestHeaders(): Promise<Headers>;
107 /**
108 * Provides a request implementation with OAuth 2.0 flow. In cases of
109 * HTTP 401 and 403 responses, it automatically asks for a new access token
110 * and replays the unsuccessful request.
111 * @param opts Request options.
112 * @param callback callback.
113 * @return A promise that resolves with the HTTP response when no callback
114 * is provided.
115 */
116 request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
117 request<T>(opts: GaxiosOptions, callback: BodyResponseCallback<T>): void;
118 /**
119 * Authenticates the provided HTTP request, processes it and resolves with the
120 * returned response.
121 * @param opts The HTTP request options.
122 * @param retry Whether the current attempt is a retry after a failed attempt.
123 * @return A promise that resolves with the successful response.
124 */
125 protected requestAsync<T>(opts: GaxiosOptions, retry?: boolean): Promise<GaxiosResponse<T>>;
126 /**
127 * Forces token refresh, even if unexpired tokens are currently cached.
128 * GCP access tokens are retrieved from authclient object/source credential.
129 * Then GCP access tokens are exchanged for downscoped access tokens via the
130 * token exchange endpoint.
131 * @return A promise that resolves with the fresh downscoped access token.
132 */
133 protected refreshAccessTokenAsync(): Promise<CredentialsWithResponse>;
134 /**
135 * Returns whether the provided credentials are expired or not.
136 * If there is no expiry time, assumes the token is not expired or expiring.
137 * @param downscopedAccessToken The credentials to check for expiration.
138 * @return Whether the credentials are expired or not.
139 */
140 private isExpired;
141}
142export {};