UNPKG

2.89 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 */
7export declare type GetTokenCallback = (err: Error | null, token?: TokenData) => void;
8export interface Credentials {
9 privateKey: string;
10 clientEmail?: string;
11}
12export interface TokenData {
13 refresh_token?: string;
14 expires_in?: number;
15 access_token?: string;
16 token_type?: string;
17 id_token?: string;
18}
19export interface TokenOptions {
20 keyFile?: string;
21 key?: string;
22 email?: string;
23 iss?: string;
24 sub?: string;
25 scope?: string | string[];
26 additionalClaims?: {};
27 eagerRefreshThresholdMillis?: number;
28}
29export interface GetTokenOptions {
30 forceRefresh?: boolean;
31}
32export declare class GoogleToken {
33 get accessToken(): string | undefined;
34 get idToken(): string | undefined;
35 get tokenType(): string | undefined;
36 get refreshToken(): string | undefined;
37 expiresAt?: number;
38 key?: string;
39 keyFile?: string;
40 iss?: string;
41 sub?: string;
42 scope?: string;
43 rawToken?: TokenData;
44 tokenExpires?: number;
45 email?: string;
46 additionalClaims?: {};
47 eagerRefreshThresholdMillis?: number;
48 private inFlightRequest?;
49 /**
50 * Create a GoogleToken.
51 *
52 * @param options Configuration object.
53 */
54 constructor(options?: TokenOptions);
55 /**
56 * Returns whether the token has expired.
57 *
58 * @return true if the token has expired, false otherwise.
59 */
60 hasExpired(): boolean;
61 /**
62 * Returns whether the token will expire within eagerRefreshThresholdMillis
63 *
64 * @return true if the token will be expired within eagerRefreshThresholdMillis, false otherwise.
65 */
66 isTokenExpiring(): boolean;
67 /**
68 * Returns a cached token or retrieves a new one from Google.
69 *
70 * @param callback The callback function.
71 */
72 getToken(opts?: GetTokenOptions): Promise<TokenData>;
73 getToken(callback: GetTokenCallback, opts?: GetTokenOptions): void;
74 /**
75 * Given a keyFile, extract the key and client email if available
76 * @param keyFile Path to a json, pem, or p12 file that contains the key.
77 * @returns an object with privateKey and clientEmail properties
78 */
79 getCredentials(keyFile: string): Promise<Credentials>;
80 private getTokenAsync;
81 private getTokenAsyncInner;
82 private ensureEmail;
83 /**
84 * Revoke the token if one is set.
85 *
86 * @param callback The callback function.
87 */
88 revokeToken(): Promise<void>;
89 revokeToken(callback: (err?: Error) => void): void;
90 private revokeTokenAsync;
91 /**
92 * Configure the GoogleToken for re-use.
93 * @param {object} options Configuration object.
94 */
95 private configure;
96 /**
97 * Request the token from Google.
98 */
99 private requestToken;
100}