@@ -53,6 +53,10 @@ const REFRESH_TOKEN_PATH = '/oauth2/v4/token';
53
53
const ONE_HOUR_IN_SECONDS = 60 * 60 ;
54
54
const JWT_ALGORITHM = 'RS256' ;
55
55
56
+ let globalAppDefaultCred : Credential ;
57
+ const globalCertCreds : { [ key : string ] : ServiceAccountCredential } = { } ;
58
+ const globalRefreshTokenCreds : { [ key : string ] : RefreshTokenCredential } = { } ;
59
+
56
60
/**
57
61
* Interface for Google OAuth 2.0 access tokens.
58
62
*/
@@ -64,12 +68,164 @@ export interface GoogleOAuthAccessToken {
64
68
}
65
69
66
70
/**
67
- * Interface for things that generate access tokens.
71
+ * Interface that provides Google OAuth2 access tokens used to authenticate
72
+ * with Firebase services.
73
+ *
74
+ * In most cases, you will not need to implement this yourself and can instead
75
+ * use the default implementations provided by
76
+ * {@link admin.credential `admin.credential`}.
68
77
*/
69
78
export interface Credential {
79
+ /**
80
+ * Returns a Google OAuth2 access token object used to authenticate with
81
+ * Firebase services.
82
+ *
83
+ * This object contains the following properties:
84
+ * * `access_token` (`string`): The actual Google OAuth2 access token.
85
+ * * `expires_in` (`number`): The number of seconds from when the token was
86
+ * issued that it expires.
87
+ *
88
+ * @return A Google OAuth2 access token object.
89
+ */
70
90
getAccessToken ( ) : Promise < GoogleOAuthAccessToken > ;
71
91
}
72
92
93
+ /**
94
+ * Returns a credential created from the
95
+ * {@link
96
+ * https://developers.google.com/identity/protocols/application-default-credentials
97
+ * Google Application Default Credentials}
98
+ * that grants admin access to Firebase services. This credential can be used
99
+ * in the call to
100
+ * {@link
101
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
102
+ * `admin.initializeApp()`}.
103
+ *
104
+ * Google Application Default Credentials are available on any Google
105
+ * infrastructure, such as Google App Engine and Google Compute Engine.
106
+ *
107
+ * See
108
+ * {@link
109
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
110
+ * Initialize the SDK}
111
+ * for more details.
112
+ *
113
+ * @example
114
+ * ```javascript
115
+ * admin.initializeApp({
116
+ * credential: admin.credential.applicationDefault(),
117
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
118
+ * });
119
+ * ```
120
+ *
121
+ * @param {!Object= } httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
122
+ * to be used when retrieving access tokens from Google token servers.
123
+ *
124
+ * @return {!admin.credential.Credential } A credential authenticated via Google
125
+ * Application Default Credentials that can be used to initialize an app.
126
+ */
127
+ export function applicationDefault ( httpAgent ?: Agent ) : Credential {
128
+ if ( typeof globalAppDefaultCred === 'undefined' ) {
129
+ globalAppDefaultCred = getApplicationDefault ( httpAgent ) ;
130
+ }
131
+ return globalAppDefaultCred ;
132
+ }
133
+
134
+ /**
135
+ * Returns a credential created from the provided service account that grants
136
+ * admin access to Firebase services. This credential can be used in the call
137
+ * to
138
+ * {@link
139
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
140
+ * `admin.initializeApp()`}.
141
+ *
142
+ * See
143
+ * {@link
144
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
145
+ * Initialize the SDK}
146
+ * for more details.
147
+ *
148
+ * @example
149
+ * ```javascript
150
+ * // Providing a path to a service account key JSON file
151
+ * var serviceAccount = require("path/to/serviceAccountKey.json");
152
+ * admin.initializeApp({
153
+ * credential: admin.credential.cert(serviceAccount),
154
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
155
+ * });
156
+ * ```
157
+ *
158
+ * @example
159
+ * ```javascript
160
+ * // Providing a service account object inline
161
+ * admin.initializeApp({
162
+ * credential: admin.credential.cert({
163
+ * projectId: "<PROJECT_ID>",
164
+ * clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
165
+ * privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n"
166
+ * }),
167
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
168
+ * });
169
+ * ```
170
+ *
171
+ * @param serviceAccountPathOrObject The path to a service
172
+ * account key JSON file or an object representing a service account key.
173
+ * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
174
+ * to be used when retrieving access tokens from Google token servers.
175
+ *
176
+ * @return A credential authenticated via the
177
+ * provided service account that can be used to initialize an app.
178
+ */
179
+ export function cert ( serviceAccountPathOrObject : string | object , httpAgent ?: Agent ) : Credential {
180
+ const stringifiedServiceAccount = JSON . stringify ( serviceAccountPathOrObject ) ;
181
+ if ( ! ( stringifiedServiceAccount in globalCertCreds ) ) {
182
+ globalCertCreds [ stringifiedServiceAccount ] = new ServiceAccountCredential ( serviceAccountPathOrObject , httpAgent ) ;
183
+ }
184
+ return globalCertCreds [ stringifiedServiceAccount ] ;
185
+ }
186
+
187
+ /**
188
+ * Returns a credential created from the provided refresh token that grants
189
+ * admin access to Firebase services. This credential can be used in the call
190
+ * to
191
+ * {@link
192
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
193
+ * `admin.initializeApp()`}.
194
+ *
195
+ * See
196
+ * {@link
197
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
198
+ * Initialize the SDK}
199
+ * for more details.
200
+ *
201
+ * @example
202
+ * ```javascript
203
+ * // Providing a path to a refresh token JSON file
204
+ * var refreshToken = require("path/to/refreshToken.json");
205
+ * admin.initializeApp({
206
+ * credential: admin.credential.refreshToken(refreshToken),
207
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
208
+ * });
209
+ * ```
210
+ *
211
+ * @param refreshTokenPathOrObject The path to a Google
212
+ * OAuth2 refresh token JSON file or an object representing a Google OAuth2
213
+ * refresh token.
214
+ * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
215
+ * to be used when retrieving access tokens from Google token servers.
216
+ *
217
+ * @return A credential authenticated via the
218
+ * provided service account that can be used to initialize an app.
219
+ */
220
+ export function refreshToken ( refreshTokenPathOrObject : string | object , httpAgent ?: Agent ) : Credential {
221
+ const stringifiedRefreshToken = JSON . stringify ( refreshTokenPathOrObject ) ;
222
+ if ( ! ( stringifiedRefreshToken in globalRefreshTokenCreds ) ) {
223
+ globalRefreshTokenCreds [ stringifiedRefreshToken ] = new RefreshTokenCredential (
224
+ refreshTokenPathOrObject , httpAgent ) ;
225
+ }
226
+ return globalRefreshTokenCreds [ stringifiedRefreshToken ] ;
227
+ }
228
+
73
229
/**
74
230
* Implementation of Credential that uses a service account.
75
231
*/
0 commit comments