UNPKG

google-auth-library

Version:

Google APIs Authentication Client Library for Node.js

191 lines (190 loc) 6.96 kB
/// <reference types="node" /> import * as fs from 'fs'; import * as stream from 'stream'; import { DefaultTransporter, Transporter } from '../transporters'; import { JWTInput } from './credentials'; import { JWT } from './jwtclient'; import { OAuth2Client } from './oauth2client'; import { UserRefreshClient } from './refreshclient'; export interface ProjectIdCallback { (err?: Error | null, projectId?: string | null): void; } export interface CredentialCallback { (err: Error | null, result?: UserRefreshClient | JWT): void; } export interface ADCCallback { (err: Error | null, credential?: OAuth2Client, projectId?: string | null): void; } export interface ADCResponse { credential: OAuth2Client; projectId: string | null; } export interface CredentialBody { client_email?: string; private_key?: string; } export declare class GoogleAuth { transporter: Transporter; /** * Caches a value indicating whether the auth layer is running on Google * Compute Engine. * @private */ private checkIsGCE?; readonly isGCE: boolean | undefined; private _getDefaultProjectIdPromise; private _cachedProjectId; jsonContent: JWTInput | null; cachedCredential: OAuth2Client | null; /** * Export DefaultTransporter as a static property of the class. */ static DefaultTransporter: typeof DefaultTransporter; /** * Obtains the default project ID for the application.. * @param callback Optional callback * @returns Promise that resolves with project Id (if used without callback) */ getDefaultProjectId(): Promise<string>; getDefaultProjectId(callback: ProjectIdCallback): void; private getDefaultProjectIdAsync(); /** * Run the Google Cloud SDK command that prints the default project ID */ _getSDKDefaultProjectId(): Promise<{ stdout: string | null; stderr: string | null; }>; /** * Obtains the default service-level credentials for the application. * @param {function=} callback Optional callback. * @returns Promise that resolves with the ADCResponse (if no callback was * passed). */ getApplicationDefault(): Promise<ADCResponse>; getApplicationDefault(callback: ADCCallback): void; private getApplicationDefaultAsync(); /** * Determines whether the auth layer is running on Google Compute Engine. * @returns A promise that resolves with the boolean. * @api private */ _checkIsGCE(isRetry?: boolean): Promise<boolean>; /** * Attempts to load default credentials from the environment variable path.. * @returns Promise that resolves with the OAuth2Client or null. * @api private */ _tryGetApplicationCredentialsFromEnvironmentVariable(): Promise<JWT | UserRefreshClient | null>; /** * Attempts to load default credentials from a well-known file location * @return Promise that resolves with the OAuth2Client or null. * @api private */ _tryGetApplicationCredentialsFromWellKnownFile(): Promise<JWT | UserRefreshClient | null>; /** * Attempts to load default credentials from a file at the given path.. * @param {string=} filePath The path to the file to read. * @returns Promise that resolves with the OAuth2Client * @api private */ _getApplicationCredentialsFromFilePath(filePath: string): Promise<JWT | UserRefreshClient>; /** * Create a credentials instance using the given input options. * @param {object=} json The input object. * @returns JWT or UserRefresh Client with data */ fromJSON(json: JWTInput): JWT | UserRefreshClient; /** * Create a credentials instance using the given input stream. * @param {object=} inputStream The input stream. * @param {function=} callback Optional callback. */ fromStream(inputStream: stream.Readable): Promise<JWT | UserRefreshClient>; fromStream(inputStream: stream.Readable, callback: CredentialCallback): void; private fromStreamAsync(inputStream); /** * Create a credentials instance using the given API key string. * @param {string} - The API key string * @returns A JWT loaded from the key */ fromAPIKey(apiKey: string): JWT; /** * Determines whether the current operating system is Windows. * @api private */ private _isWindows(); /** * Creates a file stream. Allows mocking. * @api private */ _createReadStream(filePath: string): fs.ReadStream; /** * Gets the value of the environment variable with the given name. Allows * mocking. * @api private */ _getEnv(name: string): string | undefined; /** * Gets the current operating system platform. Allows mocking. * @api private */ _osPlatform(): NodeJS.Platform; /** * Determines whether a file exists. Allows mocking. * @api private */ _fileExists(filePath: string): boolean; /** * Joins two parts of a path. Allows mocking. * @api private */ _pathJoin(item1: string, item2: string): string; /** * Allows mocking of the path to a well-known file. * @api private */ _mockWellKnownFilePath(filePath: string): string; private createError(message, err); /** * Loads the default project of the Google Cloud SDK. * @api private */ private getDefaultServiceProjectId(); /** * Loads the project id from environment variables. * @api private */ private getProductionProjectId(); /** * Loads the project id from the GOOGLE_APPLICATION_CREDENTIALS json file. * @api private */ private getFileProjectId(); /** * Gets the Compute Engine project ID if it can be inferred. * Uses 169.254.169.254 for the metadata server to avoid request * latency from DNS lookup. * See https://cloud.google.com/compute/docs/metadata#metadataserver * for information about this IP address. (This IP is also used for * Amazon EC2 instances, so the metadata flavor is crucial.) * See https://github.com/google/oauth2client/issues/93 for context about * DNS latency. * * @api private */ private getGCEProjectId(); /** * The callback function handles a credential object that contains the * client_email and private_key (if exists). * getCredentials checks for these values from the user JSON at first. * If it doesn't exist, and the environment is on GCE, it gets the * client_email from the cloud metadata server. * @param callback Callback that handles the credential object that contains * a client_email and optional private key, or the error. * returned */ getCredentials(): Promise<CredentialBody>; getCredentials(callback: (err: Error | null, credentials?: CredentialBody) => void): void; private getCredentialsAsync(); }