firebase-admin 4.1.2 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -69,4 +69,3 @@ and for their longtime use and support of the Firebase platform.
69
69
 
70
70
  The Firebase Admin Node.js SDK is covered by the
71
71
  [Terms of Service for Firebase Services](https://firebase.google.com/terms/).
72
-
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -16,7 +16,7 @@ var FIREBASE_AUTH_PATH = '/identitytoolkit/v3/relyingparty/';
16
16
  /** Firebase Auth request header. */
17
17
  var FIREBASE_AUTH_HEADER = {
18
18
  'Content-Type': 'application/json',
19
- 'X-Client-Version': 'Node/Admin/4.1.2',
19
+ 'X-Client-Version': 'Node/Admin/4.2.1',
20
20
  };
21
21
  /** Firebase Auth request timeout duration in milliseconds. */
22
22
  var FIREBASE_AUTH_TIMEOUT = 10000;
package/lib/auth/auth.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -9,6 +9,7 @@ var os = require("os");
9
9
  var http = require("http");
10
10
  var path = require("path");
11
11
  var https = require("https");
12
+ var error_1 = require("../utils/error");
12
13
  var GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token';
13
14
  var GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com';
14
15
  var GOOGLE_AUTH_TOKEN_PATH = '/o/oauth2/token';
@@ -44,17 +45,21 @@ var RefreshToken = (function () {
44
45
  copyAttr(this, json, 'clientSecret', 'client_secret');
45
46
  copyAttr(this, json, 'refreshToken', 'refresh_token');
46
47
  copyAttr(this, json, 'type', 'type');
48
+ var errorMessage;
47
49
  if (typeof this.clientId !== 'string' || !this.clientId) {
48
- throw new Error('Refresh token must contain a "client_id" property');
50
+ errorMessage = 'Refresh token must contain a "client_id" property.';
49
51
  }
50
52
  else if (typeof this.clientSecret !== 'string' || !this.clientSecret) {
51
- throw new Error('Refresh token must contain a "client_secret" property');
53
+ errorMessage = 'Refresh token must contain a "client_secret" property.';
52
54
  }
53
55
  else if (typeof this.refreshToken !== 'string' || !this.refreshToken) {
54
- throw new Error('Refresh token must contain a "refresh_token" property');
56
+ errorMessage = 'Refresh token must contain a "refresh_token" property.';
55
57
  }
56
58
  else if (typeof this.type !== 'string' || !this.type) {
57
- throw new Error('Refresh token must contain a "type" property');
59
+ errorMessage = 'Refresh token must contain a "type" property.';
60
+ }
61
+ if (typeof errorMessage !== 'undefined') {
62
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
58
63
  }
59
64
  }
60
65
  /*
@@ -75,7 +80,7 @@ var RefreshToken = (function () {
75
80
  }
76
81
  catch (error) {
77
82
  // Throw a nicely formed error message if the file contents cannot be parsed
78
- throw new Error('Failed to parse refresh token file: ' + error);
83
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse refresh token file: ' + error);
79
84
  }
80
85
  };
81
86
  return RefreshToken;
@@ -87,29 +92,33 @@ exports.RefreshToken = RefreshToken;
87
92
  var Certificate = (function () {
88
93
  function Certificate(json) {
89
94
  if (typeof json !== 'object' || json === null) {
90
- throw new Error('Certificate object must be an object.');
95
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Certificate object must be an object.');
91
96
  }
92
97
  copyAttr(this, json, 'projectId', 'project_id');
93
98
  copyAttr(this, json, 'privateKey', 'private_key');
94
99
  copyAttr(this, json, 'clientEmail', 'client_email');
100
+ var errorMessage;
95
101
  if (typeof this.privateKey !== 'string' || !this.privateKey) {
96
- throw new Error('Certificate object must contain a string "private_key" property');
102
+ errorMessage = 'Certificate object must contain a string "private_key" property.';
97
103
  }
98
104
  else if (typeof this.clientEmail !== 'string' || !this.clientEmail) {
99
- throw new Error('Certificate object must contain a string "client_email" property');
105
+ errorMessage = 'Certificate object must contain a string "client_email" property.';
106
+ }
107
+ if (typeof errorMessage !== 'undefined') {
108
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
100
109
  }
101
110
  }
102
111
  Certificate.fromPath = function (path) {
103
112
  // Node bug encountered in v6.x. fs.readFileSync hangs when path is a 0 or 1.
104
113
  if (typeof path !== 'string') {
105
- throw new Error('Failed to parse certificate key file: TypeError: path must be a string');
114
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse certificate key file: TypeError: path must be a string');
106
115
  }
107
116
  try {
108
117
  return new Certificate(JSON.parse(fs.readFileSync(path, 'utf8')));
109
118
  }
110
119
  catch (error) {
111
120
  // Throw a nicely formed error message if the file contents cannot be parsed
112
- throw new Error('Failed to parse certificate key file: ' + error);
121
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse certificate key file: ' + error);
113
122
  }
114
123
  };
115
124
  return Certificate;
@@ -128,21 +137,21 @@ function requestAccessToken(transit, options, data) {
128
137
  try {
129
138
  var json = JSON.parse(Buffer.concat(buffers).toString());
130
139
  if (json.error) {
131
- var msg = 'Error fetching access token: ' + json.error;
140
+ var errorMessage = 'Error fetching access token: ' + json.error;
132
141
  if (json.error_description) {
133
- msg += ' (' + json.error_description + ')';
142
+ errorMessage += ' (' + json.error_description + ')';
134
143
  }
135
- reject(new Error(msg));
144
+ reject(new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage));
136
145
  }
137
146
  else if (!json.access_token || !json.expires_in) {
138
- reject(new Error('Unexpected response from server'));
147
+ reject(new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, "Unexpected response while fetching access token: " + JSON.stringify(json)));
139
148
  }
140
149
  else {
141
150
  resolve(json);
142
151
  }
143
152
  }
144
153
  catch (err) {
145
- reject(err);
154
+ reject(new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, "Failed to parse access token response: " + err.toString()));
146
155
  }
147
156
  });
148
157
  });
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,4 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -152,14 +152,13 @@ var FirebaseTokenGenerator = (function () {
152
152
  verifyIdTokenDocsMessage;
153
153
  }
154
154
  if (typeof errorMessage !== 'undefined') {
155
- return Promise.reject(new Error(errorMessage));
155
+ return Promise.reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, errorMessage));
156
156
  }
157
157
  return this.fetchPublicKeys_().then(function (publicKeys) {
158
158
  if (!publicKeys.hasOwnProperty(header.kid)) {
159
- errorMessage = 'Firebase ID token has "kid" claim which does not correspond to a known ' +
160
- 'public key. Most likely the ID token is expired, so get a fresh token from your client ' +
161
- 'app and try again.' + verifyIdTokenDocsMessage;
162
- return Promise.reject(new Error(errorMessage));
159
+ return Promise.reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, 'Firebase ID token has "kid" claim which does not correspond to a known public key. ' +
160
+ 'Most likely the ID token is expired, so get a fresh token from your client app and ' +
161
+ 'try again.' + verifyIdTokenDocsMessage));
163
162
  }
164
163
  return new Promise(function (resolve, reject) {
165
164
  jwt.verify(idToken, publicKeys[header.kid], {
@@ -173,7 +172,7 @@ var FirebaseTokenGenerator = (function () {
173
172
  else if (error.name === 'JsonWebTokenError') {
174
173
  errorMessage = 'Firebase ID token has invalid signature.' + verifyIdTokenDocsMessage;
175
174
  }
176
- reject(new Error(errorMessage));
175
+ return reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, errorMessage));
177
176
  }
178
177
  else {
179
178
  decodedToken.uid = decodedToken.sub;
@@ -224,7 +223,7 @@ var FirebaseTokenGenerator = (function () {
224
223
  if (response.error_description) {
225
224
  errorMessage += ' (' + response.error_description + ')';
226
225
  }
227
- reject(new Error(errorMessage));
226
+ reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, errorMessage));
228
227
  }
229
228
  else {
230
229
  /* istanbul ignore else */
@@ -1,7 +1,8 @@
1
- /*! firebase-admin v4.1.2
1
+ /*! firebase-admin v4.2.1
2
2
  https://firebase.google.com/terms/ */
3
3
  "use strict";
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
+ var utils = require("../utils");
5
6
  var error_1 = require("../utils/error");
6
7
  /**
7
8
  * Parses a time stamp string or number and returns the corresponding date if valid.
@@ -35,25 +36,9 @@ var UserMetadata = (function () {
35
36
  // were cases in the past where users did not have creation date properly set.
36
37
  // This included legacy Firebase migrating project users and some anonymous users.
37
38
  // These bugs have already been addressed since then.
38
- this.createdAtInternal = parseDate(response.createdAt);
39
- this.lastSignedInAtInternal = parseDate(response.lastLoginAt);
39
+ utils.addReadonlyGetter(this, 'createdAt', parseDate(response.createdAt));
40
+ utils.addReadonlyGetter(this, 'lastSignedInAt', parseDate(response.lastLoginAt));
40
41
  }
41
- Object.defineProperty(UserMetadata.prototype, "lastSignedInAt", {
42
- /** @return {Date} The user's last sign-in date. */
43
- get: function () {
44
- return this.lastSignedInAtInternal;
45
- },
46
- enumerable: true,
47
- configurable: true
48
- });
49
- Object.defineProperty(UserMetadata.prototype, "createdAt", {
50
- /** @return {Date} The user's account creation date. */
51
- get: function () {
52
- return this.createdAtInternal;
53
- },
54
- enumerable: true,
55
- configurable: true
56
- });
57
42
  /** @return {Object} The plain object representation of the user's metadata. */
58
43
  UserMetadata.prototype.toJSON = function () {
59
44
  return {
@@ -78,52 +63,12 @@ var UserInfo = (function () {
78
63
  if (!response.rawId || !response.providerId) {
79
64
  throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid user info response');
80
65
  }
81
- this.uidInternal = response.rawId;
82
- this.displayNameInternal = response.displayName;
83
- this.emailInternal = response.email;
84
- this.photoURLInternal = response.photoUrl;
85
- this.providerIdInternal = response.providerId;
66
+ utils.addReadonlyGetter(this, 'uid', response.rawId);
67
+ utils.addReadonlyGetter(this, 'displayName', response.displayName);
68
+ utils.addReadonlyGetter(this, 'email', response.email);
69
+ utils.addReadonlyGetter(this, 'photoURL', response.photoUrl);
70
+ utils.addReadonlyGetter(this, 'providerId', response.providerId);
86
71
  }
87
- Object.defineProperty(UserInfo.prototype, "uid", {
88
- /** @return {string} The provider user id. */
89
- get: function () {
90
- return this.uidInternal;
91
- },
92
- enumerable: true,
93
- configurable: true
94
- });
95
- Object.defineProperty(UserInfo.prototype, "displayName", {
96
- /** @return {string} The provider display name. */
97
- get: function () {
98
- return this.displayNameInternal;
99
- },
100
- enumerable: true,
101
- configurable: true
102
- });
103
- Object.defineProperty(UserInfo.prototype, "email", {
104
- /** @return {string} The provider email. */
105
- get: function () {
106
- return this.emailInternal;
107
- },
108
- enumerable: true,
109
- configurable: true
110
- });
111
- Object.defineProperty(UserInfo.prototype, "photoURL", {
112
- /** @return {string} The provider photo URL. */
113
- get: function () {
114
- return this.photoURLInternal;
115
- },
116
- enumerable: true,
117
- configurable: true
118
- });
119
- Object.defineProperty(UserInfo.prototype, "providerId", {
120
- /** @return {string} The provider Firebase ID. */
121
- get: function () {
122
- return this.providerIdInternal;
123
- },
124
- enumerable: true,
125
- configurable: true
126
- });
127
72
  /** @return {Object} The plain object representation of the current provider data. */
128
73
  UserInfo.prototype.toJSON = function () {
129
74
  return {
@@ -151,96 +96,33 @@ var UserRecord = (function () {
151
96
  if (!response.localId) {
152
97
  throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid user response');
153
98
  }
154
- this.uidInternal = response.localId;
155
- this.emailInternal = response.email;
156
- this.emailVerifiedInternal = !!response.emailVerified;
157
- this.displayNameInternal = response.displayName;
158
- this.photoURLInternal = response.photoUrl;
99
+ utils.addReadonlyGetter(this, 'uid', response.localId);
100
+ utils.addReadonlyGetter(this, 'email', response.email);
101
+ utils.addReadonlyGetter(this, 'emailVerified', !!response.emailVerified);
102
+ utils.addReadonlyGetter(this, 'displayName', response.displayName);
103
+ utils.addReadonlyGetter(this, 'photoURL', response.photoUrl);
159
104
  // If disabled is not provided, the account is enabled by default.
160
- this.disabledInternal = response.disabled || false;
161
- this.metadataInternal = new UserMetadata(response);
162
- var providerData = response.providerUserInfo || [];
163
- this.providerDataInternal = [];
164
- for (var _i = 0, providerData_1 = providerData; _i < providerData_1.length; _i++) {
165
- var entry = providerData_1[_i];
166
- this.providerData.push(new UserInfo(entry));
105
+ utils.addReadonlyGetter(this, 'disabled', response.disabled || false);
106
+ utils.addReadonlyGetter(this, 'metadata', new UserMetadata(response));
107
+ var providerData = [];
108
+ for (var _i = 0, _a = (response.providerUserInfo || []); _i < _a.length; _i++) {
109
+ var entry = _a[_i];
110
+ providerData.push(new UserInfo(entry));
167
111
  }
112
+ utils.addReadonlyGetter(this, 'providerData', providerData);
168
113
  }
169
- Object.defineProperty(UserRecord.prototype, "uid", {
170
- /** @return {string} The Firebase user id corresponding to the current user record. */
171
- get: function () {
172
- return this.uidInternal;
173
- },
174
- enumerable: true,
175
- configurable: true
176
- });
177
- Object.defineProperty(UserRecord.prototype, "email", {
178
- /** @return {string} The primary email corresponding to the current user record. */
179
- get: function () {
180
- return this.emailInternal;
181
- },
182
- enumerable: true,
183
- configurable: true
184
- });
185
- Object.defineProperty(UserRecord.prototype, "emailVerified", {
186
- /** @return {boolean} Whether the primary email is verified. */
187
- get: function () {
188
- return this.emailVerifiedInternal;
189
- },
190
- enumerable: true,
191
- configurable: true
192
- });
193
- Object.defineProperty(UserRecord.prototype, "displayName", {
194
- /** @return {string} The display name corresponding to the current user record. */
195
- get: function () {
196
- return this.displayNameInternal;
197
- },
198
- enumerable: true,
199
- configurable: true
200
- });
201
- Object.defineProperty(UserRecord.prototype, "photoURL", {
202
- /** @return {string} The photo URL corresponding to the current user record. */
203
- get: function () {
204
- return this.photoURLInternal;
205
- },
206
- enumerable: true,
207
- configurable: true
208
- });
209
- Object.defineProperty(UserRecord.prototype, "disabled", {
210
- /** @return {boolean} Whether the current user is disabled or not. */
211
- get: function () {
212
- return this.disabledInternal;
213
- },
214
- enumerable: true,
215
- configurable: true
216
- });
217
- Object.defineProperty(UserRecord.prototype, "metadata", {
218
- /** @return {UserMetadata} The user record's metadata. */
219
- get: function () {
220
- return this.metadataInternal;
221
- },
222
- enumerable: true,
223
- configurable: true
224
- });
225
- Object.defineProperty(UserRecord.prototype, "providerData", {
226
- /** @return {UserInfo[]} The list of providers linked to the current record. */
227
- get: function () {
228
- return this.providerDataInternal;
229
- },
230
- enumerable: true,
231
- configurable: true
232
- });
233
114
  /** @return {Object} The plain object representation of the user record. */
234
115
  UserRecord.prototype.toJSON = function () {
235
- var json = {};
236
- json.uid = this.uid;
237
- json.email = this.email;
238
- json.emailVerified = this.emailVerified;
239
- json.displayName = this.displayName;
240
- json.photoURL = this.photoURL;
241
- json.disabled = this.disabled;
242
- // Convert metadata to json.
243
- json.metadata = this.metadata.toJSON();
116
+ var json = {
117
+ uid: this.uid,
118
+ email: this.email,
119
+ emailVerified: this.emailVerified,
120
+ displayName: this.displayName,
121
+ photoURL: this.photoURL,
122
+ disabled: this.disabled,
123
+ // Convert metadata to json.
124
+ metadata: this.metadata.toJSON(),
125
+ };
244
126
  json.providerData = [];
245
127
  for (var _i = 0, _a = this.providerData; _i < _a.length; _i++) {
246
128
  var entry = _a[_i];