|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -// Use untyped import syntax for Node built-ins |
18 |
| -import { Agent } from 'http'; |
19 | 17 | import {
|
20 |
| - ServiceAccountCredential, RefreshTokenCredential, getApplicationDefault |
| 18 | + ServiceAccountCredential, RefreshTokenCredential, getApplicationDefault |
21 | 19 | } from './credential-internal';
|
22 |
| -import { Credential } from './credential-interfaces'; |
| 20 | +import { credential } from './index'; |
23 | 21 |
|
24 |
| -let globalAppDefaultCred: Credential; |
| 22 | +let globalAppDefaultCred: credential.Credential; |
25 | 23 | const globalCertCreds: { [key: string]: ServiceAccountCredential } = {};
|
26 | 24 | const globalRefreshTokenCreds: { [key: string]: RefreshTokenCredential } = {};
|
27 | 25 |
|
28 |
| -/** |
29 |
| - * Returns a credential created from the |
30 |
| - * {@link |
31 |
| - * https://developers.google.com/identity/protocols/application-default-credentials |
32 |
| - * Google Application Default Credentials} |
33 |
| - * that grants admin access to Firebase services. This credential can be used |
34 |
| - * in the call to |
35 |
| - * {@link |
36 |
| - * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp |
37 |
| - * `admin.initializeApp()`}. |
38 |
| - * |
39 |
| - * Google Application Default Credentials are available on any Google |
40 |
| - * infrastructure, such as Google App Engine and Google Compute Engine. |
41 |
| - * |
42 |
| - * See |
43 |
| - * {@link |
44 |
| - * https://firebase.google.com/docs/admin/setup#initialize_the_sdk |
45 |
| - * Initialize the SDK} |
46 |
| - * for more details. |
47 |
| - * |
48 |
| - * @example |
49 |
| - * ```javascript |
50 |
| - * admin.initializeApp({ |
51 |
| - * credential: admin.credential.applicationDefault(), |
52 |
| - * databaseURL: "https://<DATABASE_NAME>.firebaseio.com" |
53 |
| - * }); |
54 |
| - * ``` |
55 |
| - * |
56 |
| - * @param {!Object=} httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent) |
57 |
| - * to be used when retrieving access tokens from Google token servers. |
58 |
| - * |
59 |
| - * @return {!admin.credential.Credential} A credential authenticated via Google |
60 |
| - * Application Default Credentials that can be used to initialize an app. |
61 |
| - */ |
62 |
| -export function applicationDefault(httpAgent?: Agent): Credential { |
| 26 | +export const applicationDefault: typeof credential.applicationDefault = (httpAgent?) => { |
63 | 27 | if (typeof globalAppDefaultCred === 'undefined') {
|
64 | 28 | globalAppDefaultCred = getApplicationDefault(httpAgent);
|
65 | 29 | }
|
66 | 30 | return globalAppDefaultCred;
|
67 | 31 | }
|
68 | 32 |
|
69 |
| -/** |
70 |
| - * Returns a credential created from the provided service account that grants |
71 |
| - * admin access to Firebase services. This credential can be used in the call |
72 |
| - * to |
73 |
| - * {@link |
74 |
| - * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp |
75 |
| - * `admin.initializeApp()`}. |
76 |
| - * |
77 |
| - * See |
78 |
| - * {@link |
79 |
| - * https://firebase.google.com/docs/admin/setup#initialize_the_sdk |
80 |
| - * Initialize the SDK} |
81 |
| - * for more details. |
82 |
| - * |
83 |
| - * @example |
84 |
| - * ```javascript |
85 |
| - * // Providing a path to a service account key JSON file |
86 |
| - * var serviceAccount = require("path/to/serviceAccountKey.json"); |
87 |
| - * admin.initializeApp({ |
88 |
| - * credential: admin.credential.cert(serviceAccount), |
89 |
| - * databaseURL: "https://<DATABASE_NAME>.firebaseio.com" |
90 |
| - * }); |
91 |
| - * ``` |
92 |
| - * |
93 |
| - * @example |
94 |
| - * ```javascript |
95 |
| - * // Providing a service account object inline |
96 |
| - * admin.initializeApp({ |
97 |
| - * credential: admin.credential.cert({ |
98 |
| - * projectId: "<PROJECT_ID>", |
99 |
| - * clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com", |
100 |
| - * privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n" |
101 |
| - * }), |
102 |
| - * databaseURL: "https://<DATABASE_NAME>.firebaseio.com" |
103 |
| - * }); |
104 |
| - * ``` |
105 |
| - * |
106 |
| - * @param serviceAccountPathOrObject The path to a service |
107 |
| - * account key JSON file or an object representing a service account key. |
108 |
| - * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent) |
109 |
| - * to be used when retrieving access tokens from Google token servers. |
110 |
| - * |
111 |
| - * @return A credential authenticated via the |
112 |
| - * provided service account that can be used to initialize an app. |
113 |
| - */ |
114 |
| -export function cert(serviceAccountPathOrObject: string | object, httpAgent?: Agent): Credential { |
| 33 | +export const cert: typeof credential.cert = (serviceAccountPathOrObject, httpAgent?) => { |
115 | 34 | const stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
|
116 | 35 | if (!(stringifiedServiceAccount in globalCertCreds)) {
|
117 | 36 | globalCertCreds[stringifiedServiceAccount] = new ServiceAccountCredential(serviceAccountPathOrObject, httpAgent);
|
118 | 37 | }
|
119 | 38 | return globalCertCreds[stringifiedServiceAccount];
|
120 | 39 | }
|
121 | 40 |
|
122 |
| -/** |
123 |
| - * Returns a credential created from the provided refresh token that grants |
124 |
| - * admin access to Firebase services. This credential can be used in the call |
125 |
| - * to |
126 |
| - * {@link |
127 |
| - * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp |
128 |
| - * `admin.initializeApp()`}. |
129 |
| - * |
130 |
| - * See |
131 |
| - * {@link |
132 |
| - * https://firebase.google.com/docs/admin/setup#initialize_the_sdk |
133 |
| - * Initialize the SDK} |
134 |
| - * for more details. |
135 |
| - * |
136 |
| - * @example |
137 |
| - * ```javascript |
138 |
| - * // Providing a path to a refresh token JSON file |
139 |
| - * var refreshToken = require("path/to/refreshToken.json"); |
140 |
| - * admin.initializeApp({ |
141 |
| - * credential: admin.credential.refreshToken(refreshToken), |
142 |
| - * databaseURL: "https://<DATABASE_NAME>.firebaseio.com" |
143 |
| - * }); |
144 |
| - * ``` |
145 |
| - * |
146 |
| - * @param refreshTokenPathOrObject The path to a Google |
147 |
| - * OAuth2 refresh token JSON file or an object representing a Google OAuth2 |
148 |
| - * refresh token. |
149 |
| - * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent) |
150 |
| - * to be used when retrieving access tokens from Google token servers. |
151 |
| - * |
152 |
| - * @return A credential authenticated via the |
153 |
| - * provided service account that can be used to initialize an app. |
154 |
| - */ |
155 |
| -export function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential { |
| 41 | +export const refreshToken: typeof credential.refreshToken = (refreshTokenPathOrObject, httpAgent) => { |
156 | 42 | const stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
|
157 | 43 | if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {
|
158 | 44 | globalRefreshTokenCreds[stringifiedRefreshToken] = new RefreshTokenCredential(
|
|
0 commit comments