Skip to content

Commit 76ca6d6

Browse files
authored
Using ADC when no credentials are specified in options (#214)
* Using ADC when no credentials are specified in options * Updated Changelog
1 parent e87b47c commit 76ca6d6

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Unreleased
22

3+
- [changed] The `admin.initializeApp()` method can now be invoked without an
4+
explicit `credential` option. In that case the SDK will get initialized with
5+
Google application default credentials.
36
- [changed] Upgraded Realtime Database client to v0.1.11.
47
- [changed] Modified the Realtime Database client integration to report the
58
correct user agent header.

src/firebase-app.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Credential, GoogleOAuthAccessToken} from './auth/credential';
17+
import {ApplicationDefaultCredential, Credential, GoogleOAuthAccessToken} from './auth/credential';
1818
import * as validator from './utils/validator';
1919
import {deepCopy, deepExtend} from './utils/deep-copy';
2020
import {FirebaseServiceInterface} from './firebase-service';
@@ -248,27 +248,26 @@ export class FirebaseApp {
248248
this.name_ = name;
249249
this.options_ = deepCopy(options) as FirebaseAppOptions;
250250

251-
if (typeof this.options_ !== 'object' || this.options_ === null) {
252-
// Ensure the options are a non-null object
253-
this.options_ = {};
251+
if (!validator.isNonNullObject(this.options_)) {
252+
throw new FirebaseAppError(
253+
AppErrorCodes.INVALID_APP_OPTIONS,
254+
`Invalid Firebase app options passed as the first argument to initializeApp() for the ` +
255+
`app named "${this.name_}". Options must be a non-null object.`,
256+
);
254257
}
255258

256259
const hasCredential = ('credential' in this.options_);
257-
258-
let errorMessage: string;
259260
if (!hasCredential) {
260-
errorMessage = 'Options must be an object containing at least a "credential" property.';
261+
this.options_.credential = new ApplicationDefaultCredential();
261262
}
263+
262264
const credential = this.options_.credential;
263265
if (typeof credential !== 'object' || credential === null || typeof credential.getAccessToken !== 'function') {
264-
errorMessage = 'The "credential" property must be an object which implements the Credential interface.';
265-
}
266-
267-
if (typeof errorMessage !== 'undefined') {
268266
throw new FirebaseAppError(
269267
AppErrorCodes.INVALID_APP_OPTIONS,
270268
`Invalid Firebase app options passed as the first argument to initializeApp() for the ` +
271-
`app named "${this.name_}". ${errorMessage}`,
269+
`app named "${this.name_}". The "credential" property must be an object which implements ` +
270+
`the Credential interface.`,
272271
);
273272
}
274273

test/unit/firebase.spec.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as utils from './utils';
2828
import * as mocks from '../resources/mocks';
2929

3030
import * as firebaseAdmin from '../../src/index';
31+
import {ApplicationDefaultCredential} from '../../src/auth/credential';
3132

3233
chai.should();
3334
chai.use(chaiAsPromised);
@@ -55,25 +56,19 @@ describe('Firebase', () => {
5556
});
5657

5758
describe('#initializeApp()', () => {
58-
const invalidOptions = [null, NaN, 0, 1, true, false, '', 'a', [], {}, _.noop];
59-
invalidOptions.forEach((invalidOption) => {
59+
const invalidOptions = [null, NaN, 0, 1, true, false, '', 'a', [], _.noop];
60+
invalidOptions.forEach((invalidOption: any) => {
6061
it('should throw given invalid options object: ' + JSON.stringify(invalidOption), () => {
6162
expect(() => {
6263
firebaseAdmin.initializeApp(invalidOption);
6364
}).to.throw('Invalid Firebase app options');
6465
});
6566
});
6667

67-
it('should throw given an options object that does not contain any of the required keys', () => {
68-
expect(() => {
69-
firebaseAdmin.initializeApp({ a: 1, b: true } as any);
70-
}).to.throw('Invalid Firebase app options');
71-
});
72-
73-
it('should throw given an options object containing no "credential" key', () => {
74-
expect(() => {
75-
firebaseAdmin.initializeApp(mocks.appOptionsNoAuth);
76-
}).to.throw('Invalid Firebase app options');
68+
it('should use application default credentials when no credentials are explicitly specified', () => {
69+
const app = firebaseAdmin.initializeApp(mocks.appOptionsNoAuth);
70+
expect(app.options).to.have.property('credential');
71+
expect(app.options.credential).to.be.instanceOf(ApplicationDefaultCredential);
7772
});
7873

7974
it('should not modify the provided options object', () => {

0 commit comments

Comments
 (0)