File

src/auth/authclient.ts

Description

Defines the root interface for all clients that generate credentials for calling Google APIs. All clients should implement this interface.

Index

Properties
Methods

Methods

getAccessToken
getAccessToken()
Returns : Promise<literal type>

A promise that resolves with the current GCP access token response. If the current credential is expired, a new one is retrieved.

getRequestHeaders
getRequestHeaders(url?: string)

The main authentication interface. It takes an optional url which when present is the endpoint being accessed, and returns a Promise which resolves with authorization header fields.

The result has the form: { Authorization: 'Bearer ' }

Parameters :
Name Type Optional Description
url string Yes

The URI being authorized.

Returns : Promise<Headers>
on
on(event, listener: (tokens: Credentials) => void)

Subscribes a listener to the tokens event triggered when a token is generated.

Parameters :
Name Type Optional Description
event No

The tokens event to subscribe to.

listener function No

The listener that triggers on event trigger.

request
request(opts: GaxiosOptions)
Type parameters :
  • T

Provides an alternative Gaxios request implementation with auth credentials

Parameters :
Name Type Optional
opts GaxiosOptions No
Returns : GaxiosPromise<T>
setCredentials
setCredentials(credentials: Credentials)

Sets the auth credentials.

Parameters :
Name Type Optional
credentials Credentials No
Returns : void

Properties

eagerRefreshThresholdMillis
eagerRefreshThresholdMillis: number
Type : number

The expiration threshold in milliseconds before forcing token refresh.

forceRefreshOnFailure
forceRefreshOnFailure: boolean
Type : boolean

Whether to force refresh on failure when making an authorization request.

projectId
projectId: string | null
Type : string | null
Optional

The project ID corresponding to the current credentials if available.

import {EventEmitter} from 'events';
import {GaxiosOptions, GaxiosPromise, GaxiosResponse} from 'gaxios';

import {DefaultTransporter} from '../transporters';
import {Credentials} from './credentials';
import {Headers} from './oauth2client';

/**
 * Defines the root interface for all clients that generate credentials
 * for calling Google APIs. All clients should implement this interface.
 */
export interface CredentialsClient {
  /**
   * The project ID corresponding to the current credentials if available.
   */
  projectId?: string | null;

  /**
   * The expiration threshold in milliseconds before forcing token refresh.
   */
  eagerRefreshThresholdMillis: number;

  /**
   * Whether to force refresh on failure when making an authorization request.
   */
  forceRefreshOnFailure: boolean;

  /**
   * @return A promise that resolves with the current GCP access token
   *   response. If the current credential is expired, a new one is retrieved.
   */
  getAccessToken(): Promise<{
    token?: string | null;
    res?: GaxiosResponse | null;
  }>;

  /**
   * The main authentication interface. It takes an optional url which when
   * present is the endpoint being accessed, and returns a Promise which
   * resolves with authorization header fields.
   *
   * The result has the form:
   * { Authorization: 'Bearer <access_token_value>' }
   * @param url The URI being authorized.
   */
  getRequestHeaders(url?: string): Promise<Headers>;

  /**
   * Provides an alternative Gaxios request implementation with auth credentials
   */
  request<T>(opts: GaxiosOptions): GaxiosPromise<T>;

  /**
   * Sets the auth credentials.
   */
  setCredentials(credentials: Credentials): void;

  /**
   * Subscribes a listener to the tokens event triggered when a token is
   * generated.
   *
   * @param event The tokens event to subscribe to.
   * @param listener The listener that triggers on event trigger.
   * @return The current client instance.
   */
  on(event: 'tokens', listener: (tokens: Credentials) => void): this;
}

export declare interface AuthClient {
  on(event: 'tokens', listener: (tokens: Credentials) => void): this;
}

export abstract class AuthClient
  extends EventEmitter
  implements CredentialsClient
{
  protected quotaProjectId?: string;
  transporter = new DefaultTransporter();
  credentials: Credentials = {};
  projectId?: string | null;
  eagerRefreshThresholdMillis = 5 * 60 * 1000;
  forceRefreshOnFailure = false;

  /**
   * Provides an alternative Gaxios request implementation with auth credentials
   */
  abstract request<T>(opts: GaxiosOptions): GaxiosPromise<T>;

  /**
   * The main authentication interface. It takes an optional url which when
   * present is the endpoint being accessed, and returns a Promise which
   * resolves with authorization header fields.
   *
   * The result has the form:
   * { Authorization: 'Bearer <access_token_value>' }
   * @param url The URI being authorized.
   */
  abstract getRequestHeaders(url?: string): Promise<Headers>;

  /**
   * @return A promise that resolves with the current GCP access token
   *   response. If the current credential is expired, a new one is retrieved.
   */
  abstract getAccessToken(): Promise<{
    token?: string | null;
    res?: GaxiosResponse | null;
  }>;

  /**
   * Sets the auth credentials.
   */
  setCredentials(credentials: Credentials) {
    this.credentials = credentials;
  }

  /**
   * Append additional headers, e.g., x-goog-user-project, shared across the
   * classes inheriting AuthClient. This method should be used by any method
   * that overrides getRequestMetadataAsync(), which is a shared helper for
   * setting request information in both gRPC and HTTP API calls.
   *
   * @param headers objedcdt to append additional headers to.
   */
  protected addSharedMetadataHeaders(headers: Headers): Headers {
    // quota_project_id, stored in application_default_credentials.json, is set in
    // the x-goog-user-project header, to indicate an alternate account for
    // billing and quota:
    if (
      !headers['x-goog-user-project'] && // don't override a value the user sets.
      this.quotaProjectId
    ) {
      headers['x-goog-user-project'] = this.quotaProjectId;
    }
    return headers;
  }
}

result-matching ""

    No results matching ""