Skip to content

Commit aff3cba

Browse files
authored
chore: Added the credentials API signatures and exposed admin.credential (#1035)
* chore: Added the credentials API signatures and exposed admin.credential * fix: Updated ServiceAccountCredential constructor signature
1 parent 601588b commit aff3cba

10 files changed

+182
-195
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ gulp.task('compile', function() {
8383

8484
const configuration = [
8585
'lib/**/*.js',
86+
'lib/credential/index.d.ts',
8687
];
8788

8889
workflow = workflow.pipe(filter(configuration));

src/credential/credential-interfaces.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/credential/credential-internal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Use untyped import syntax for Node built-ins
1817
import fs = require('fs');
1918
import os = require('os');
2019
import path = require('path');
2120

22-
import { GoogleOAuthAccessToken, Credential } from './credential-interfaces';
21+
import { Agent } from 'http';
22+
import { credential, GoogleOAuthAccessToken } from './index';
2323
import { AppErrorCodes, FirebaseAppError } from '../utils/error';
2424
import { HttpClient, HttpRequestConfig, HttpError, HttpResponse } from '../utils/api-request';
25-
import { Agent } from 'http';
2625
import * as util from '../utils/validator';
2726

27+
import Credential = credential.Credential;
28+
2829
const GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token';
2930
const GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com';
3031
const GOOGLE_AUTH_TOKEN_PATH = '/o/oauth2/token';

src/credential/credential.ts

Lines changed: 6 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -14,145 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Use untyped import syntax for Node built-ins
18-
import { Agent } from 'http';
1917
import {
20-
ServiceAccountCredential, RefreshTokenCredential, getApplicationDefault
18+
ServiceAccountCredential, RefreshTokenCredential, getApplicationDefault
2119
} from './credential-internal';
22-
import { Credential } from './credential-interfaces';
20+
import { credential } from './index';
2321

24-
let globalAppDefaultCred: Credential;
22+
let globalAppDefaultCred: credential.Credential;
2523
const globalCertCreds: { [key: string]: ServiceAccountCredential } = {};
2624
const globalRefreshTokenCreds: { [key: string]: RefreshTokenCredential } = {};
2725

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?) => {
6327
if (typeof globalAppDefaultCred === 'undefined') {
6428
globalAppDefaultCred = getApplicationDefault(httpAgent);
6529
}
6630
return globalAppDefaultCred;
6731
}
6832

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?) => {
11534
const stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
11635
if (!(stringifiedServiceAccount in globalCertCreds)) {
11736
globalCertCreds[stringifiedServiceAccount] = new ServiceAccountCredential(serviceAccountPathOrObject, httpAgent);
11837
}
11938
return globalCertCreds[stringifiedServiceAccount];
12039
}
12140

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) => {
15642
const stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
15743
if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {
15844
globalRefreshTokenCreds[stringifiedRefreshToken] = new RefreshTokenCredential(

0 commit comments

Comments
 (0)