index.d.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. */
  7. import { GaxiosOptions, GaxiosPromise } from 'gaxios';
  8. export interface Transporter {
  9. request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
  10. }
  11. export type GetTokenCallback = (err: Error | null, token?: TokenData) => void;
  12. export interface Credentials {
  13. privateKey: string;
  14. clientEmail?: string;
  15. }
  16. export interface TokenData {
  17. refresh_token?: string;
  18. expires_in?: number;
  19. access_token?: string;
  20. token_type?: string;
  21. id_token?: string;
  22. }
  23. export 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. }
  34. export interface GetTokenOptions {
  35. forceRefresh?: boolean;
  36. }
  37. export 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. }