UNPKG

2.74 kBTypeScriptView Raw
1/**
2 * Copyright 2018 Google LLC
3 *
4 * Distributed under MIT license.
5 * See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6 */
7import { GaxiosOptions, GaxiosPromise } from 'gaxios';
8export interface Transporter {
9 request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
10}
11export type GetTokenCallback = (err: Error | null, token?: TokenData) => void;
12export interface Credentials {
13 privateKey: string;
14 clientEmail?: string;
15}
16export interface TokenData {
17 refresh_token?: string;
18 expires_in?: number;
19 access_token?: string;
20 token_type?: string;
21 id_token?: string;
22}
23export interface TokenOptions {
24 keyFile?: string;
25 key?: string;
26 email?: string;
27 iss?: string;
28 sub?: string;
29 scope?: string | string[];
30 additionalClaims?: {};
31 eagerRefreshThresholdMillis?: number;
32 transporter?: Transporter;
33}
34export interface GetTokenOptions {
35 forceRefresh?: boolean;
36}
37export declare class GoogleToken {
38 #private;
39 get accessToken(): string | undefined;
40 get idToken(): string | undefined;
41 get tokenType(): string | undefined;
42 get refreshToken(): string | undefined;
43 expiresAt?: number;
44 key?: string;
45 keyFile?: string;
46 iss?: string;
47 sub?: string;
48 scope?: string;
49 rawToken?: TokenData;
50 tokenExpires?: number;
51 email?: string;
52 additionalClaims?: {};
53 eagerRefreshThresholdMillis?: number;
54 transporter: Transporter;
55 /**
56 * Create a GoogleToken.
57 *
58 * @param options Configuration object.
59 */
60 constructor(options?: TokenOptions);
61 /**
62 * Returns whether the token has expired.
63 *
64 * @return true if the token has expired, false otherwise.
65 */
66 hasExpired(): boolean;
67 /**
68 * Returns whether the token will expire within eagerRefreshThresholdMillis
69 *
70 * @return true if the token will be expired within eagerRefreshThresholdMillis, false otherwise.
71 */
72 isTokenExpiring(): boolean;
73 /**
74 * Returns a cached token or retrieves a new one from Google.
75 *
76 * @param callback The callback function.
77 */
78 getToken(opts?: GetTokenOptions): Promise<TokenData>;
79 getToken(callback: GetTokenCallback, opts?: GetTokenOptions): void;
80 /**
81 * Given a keyFile, extract the key and client email if available
82 * @param keyFile Path to a json, pem, or p12 file that contains the key.
83 * @returns an object with privateKey and clientEmail properties
84 */
85 getCredentials(keyFile: string): Promise<Credentials>;
86 /**
87 * Revoke the token if one is set.
88 *
89 * @param callback The callback function.
90 */
91 revokeToken(): Promise<void>;
92 revokeToken(callback: (err?: Error) => void): void;
93}