@quatrain/auth-firebase 1.2.6 → 1.2.9-pr25.6628

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.
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,7 +13,6 @@ exports.FirebaseAuthAdapter = void 0;
46
13
  const auth_1 = require("@quatrain/auth");
47
14
  const auth_2 = require("firebase-admin/auth");
48
15
  const app_1 = require("firebase-admin/app");
49
- const nativeFetch = __importStar(require("node-fetch-native"));
50
16
  /**
51
17
  * Authentication adapter implementing the Google Firebase Auth ecosystem.
52
18
  * Handles server-side validation of JWTs and admin functions via `firebase-admin`.
@@ -130,6 +96,7 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
130
96
  */
131
97
  update(user, updatable) {
132
98
  return __awaiter(this, void 0, void 0, function* () {
99
+ var _a;
133
100
  auth_1.Auth.log('auth data to update', JSON.stringify(updatable));
134
101
  try {
135
102
  if (Object.keys(updatable).length > 0) {
@@ -137,7 +104,13 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
137
104
  yield (0, auth_2.getAuth)().updateUser(user.uid, updatable);
138
105
  }
139
106
  }
140
- catch (e) { }
107
+ catch (e) {
108
+ auth_1.Auth.error(e);
109
+ if (e.code === 'auth/weak-password' || ((_a = e.message) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes('password'))) {
110
+ throw new auth_1.AuthenticationError(auth_1.Auth.ERROR_WEAK_PASSWORD);
111
+ }
112
+ throw new auth_1.AuthenticationError(e.message);
113
+ }
141
114
  });
142
115
  }
143
116
  /**
@@ -179,7 +152,7 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
179
152
  auth_1.Auth.warn(`Can't get refresh token, no API key provided`);
180
153
  return {};
181
154
  }
182
- const response = yield nativeFetch.fetch(`https://securetoken.googleapis.com/v1/token?key=${this._params.config.apiKey}`, {
155
+ const response = yield fetch(`https://securetoken.googleapis.com/v1/token?key=${this._params.config.apiKey}`, {
183
156
  method: 'POST',
184
157
  body: JSON.stringify({
185
158
  grant_type: 'refresh_token',
@@ -214,7 +187,7 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
214
187
  handleCodeInApp: true,
215
188
  };
216
189
  }
217
- const response = yield nativeFetch.fetch(`https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${this._params.config.apiKey}`, {
190
+ const response = yield fetch(`https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${this._params.config.apiKey}`, {
218
191
  method: 'POST',
219
192
  body: JSON.stringify(body),
220
193
  headers: { 'Content-Type': 'application/json' },
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,17 +13,16 @@ const FirebaseAuthAdapter_1 = require("./FirebaseAuthAdapter");
46
13
  const auth_1 = require("@quatrain/auth");
47
14
  const auth_2 = require("firebase-admin/auth");
48
15
  const app_1 = require("firebase-admin/app");
49
- const nativeFetch = __importStar(require("node-fetch-native"));
50
16
  // Mock the dependencies
51
17
  jest.mock('firebase-admin/auth');
52
18
  jest.mock('firebase-admin/app');
53
- jest.mock('node-fetch-native');
54
19
  describe('FirebaseAuthAdapter', () => {
55
20
  let adapter;
56
21
  let mockAuth;
57
22
  beforeEach(() => {
58
23
  // Clear all mocks before each test
59
24
  jest.clearAllMocks();
25
+ global.fetch = jest.fn();
60
26
  // Mock Firebase Admin SDK
61
27
  mockAuth = {
62
28
  createUser: jest.fn(),
@@ -199,13 +165,19 @@ describe('FirebaseAuthAdapter', () => {
199
165
  yield adapter.update(mockUser, {});
200
166
  expect(mockAuth.updateUser).not.toHaveBeenCalled();
201
167
  }));
202
- it('should silently handle errors during update', () => __awaiter(void 0, void 0, void 0, function* () {
168
+ it('should throw AuthenticationError on update error', () => __awaiter(void 0, void 0, void 0, function* () {
203
169
  const updatable = { displayName: 'New Name' };
204
170
  mockAuth.updateUser.mockRejectedValue(new Error('Update failed'));
205
- // Should not throw
206
- yield expect(adapter.update(mockUser, updatable)).resolves.toBeUndefined();
171
+ yield expect(adapter.update(mockUser, updatable)).rejects.toThrow(auth_1.AuthenticationError);
207
172
  expect(mockAuth.updateUser).toHaveBeenCalledWith('user-123', updatable);
208
173
  }));
174
+ it('should throw Auth.ERROR_WEAK_PASSWORD on weak password error', () => __awaiter(void 0, void 0, void 0, function* () {
175
+ const updatable = { password: '123' };
176
+ const weakError = new Error('Password should be stronger');
177
+ weakError.code = 'auth/weak-password';
178
+ mockAuth.updateUser.mockRejectedValue(weakError);
179
+ yield expect(adapter.update(mockUser, updatable)).rejects.toThrow('weak_password');
180
+ }));
209
181
  });
210
182
  describe('delete()', () => {
211
183
  const mockUser = {
@@ -231,9 +203,9 @@ describe('FirebaseAuthAdapter', () => {
231
203
  const mockFetchResponse = {
232
204
  json: jest.fn().mockResolvedValue(mockResponse),
233
205
  };
234
- nativeFetch.fetch.mockResolvedValue(mockFetchResponse);
206
+ global.fetch.mockResolvedValue(mockFetchResponse);
235
207
  const result = yield adapter.refreshToken('old-refresh-token');
236
- expect(nativeFetch.fetch).toHaveBeenCalledWith('https://securetoken.googleapis.com/v1/token?key=test-api-key', {
208
+ expect(global.fetch).toHaveBeenCalledWith('https://securetoken.googleapis.com/v1/token?key=test-api-key', {
237
209
  method: 'POST',
238
210
  body: JSON.stringify({
239
211
  grant_type: 'refresh_token',
@@ -253,15 +225,15 @@ describe('FirebaseAuthAdapter', () => {
253
225
  });
254
226
  const result = yield adapterWithoutKey.refreshToken('refresh-token');
255
227
  expect(result).toEqual({});
256
- expect(nativeFetch.fetch).not.toHaveBeenCalled();
228
+ expect(global.fetch).not.toHaveBeenCalled();
257
229
  }));
258
230
  it('should construct correct URL with API key', () => __awaiter(void 0, void 0, void 0, function* () {
259
231
  const mockFetchResponse = {
260
232
  json: jest.fn().mockResolvedValue({}),
261
233
  };
262
- nativeFetch.fetch.mockResolvedValue(mockFetchResponse);
234
+ global.fetch.mockResolvedValue(mockFetchResponse);
263
235
  yield adapter.refreshToken('refresh-token');
264
- const callArgs = nativeFetch.fetch.mock.calls[0];
236
+ const callArgs = global.fetch.mock.calls[0];
265
237
  expect(callArgs[0]).toBe('https://securetoken.googleapis.com/v1/token?key=test-api-key');
266
238
  }));
267
239
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/auth-firebase",
3
- "version": "1.2.6",
3
+ "version": "1.2.9-pr25.6628",
4
4
  "license": "AGPL-3.0-only",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,15 +19,14 @@
19
19
  },
20
20
  "author": "Quatrain Développement SAS <developers@quatrain.com>",
21
21
  "peerDependencies": {
22
- "@quatrain/core": "^1.2.16"
22
+ "@quatrain/core": "^1.2.18"
23
23
  },
24
24
  "dependencies": {
25
- "@quatrain/api": "^1.1.6",
26
- "@quatrain/auth": "^1.2.4",
27
- "@quatrain/backend": "^1.2.13",
28
- "@quatrain/core": "^1.2.16",
29
- "firebase-admin": "^13.0.1",
30
- "node-fetch-native": "^1.6.4"
25
+ "@quatrain/api": "^1.1.7",
26
+ "@quatrain/auth": "^1.2.11",
27
+ "@quatrain/backend": "^1.2.17",
28
+ "@quatrain/core": "^1.2.18",
29
+ "firebase-admin": "^13.0.1"
31
30
  },
32
31
  "devDependencies": {
33
32
  "@tsconfig/recommended": "^1.0.1",
@@ -3,12 +3,10 @@ import { User } from '@quatrain/backend'
3
3
  import { AuthenticationError } from '@quatrain/auth'
4
4
  import { getAuth } from 'firebase-admin/auth'
5
5
  import { getApps, initializeApp } from 'firebase-admin/app'
6
- import * as nativeFetch from 'node-fetch-native'
7
6
 
8
7
  // Mock the dependencies
9
8
  jest.mock('firebase-admin/auth')
10
9
  jest.mock('firebase-admin/app')
11
- jest.mock('node-fetch-native')
12
10
 
13
11
  describe('FirebaseAuthAdapter', () => {
14
12
  let adapter: FirebaseAuthAdapter
@@ -17,6 +15,7 @@ describe('FirebaseAuthAdapter', () => {
17
15
  beforeEach(() => {
18
16
  // Clear all mocks before each test
19
17
  jest.clearAllMocks()
18
+ global.fetch = jest.fn()
20
19
 
21
20
  // Mock Firebase Admin SDK
22
21
  mockAuth = {
@@ -204,18 +203,29 @@ describe('FirebaseAuthAdapter', () => {
204
203
  expect(mockAuth.updateUser).not.toHaveBeenCalled()
205
204
  })
206
205
 
207
- it('should silently handle errors during update', async () => {
206
+ it('should throw AuthenticationError on update error', async () => {
208
207
  const updatable = { displayName: 'New Name' }
209
208
 
210
209
  mockAuth.updateUser.mockRejectedValue(new Error('Update failed'))
211
210
 
212
- // Should not throw
213
211
  await expect(
214
212
  adapter.update(mockUser, updatable)
215
- ).resolves.toBeUndefined()
213
+ ).rejects.toThrow(AuthenticationError)
216
214
 
217
215
  expect(mockAuth.updateUser).toHaveBeenCalledWith('user-123', updatable)
218
216
  })
217
+
218
+ it('should throw Auth.ERROR_WEAK_PASSWORD on weak password error', async () => {
219
+ const updatable = { password: '123' }
220
+ const weakError = new Error('Password should be stronger')
221
+ ;(weakError as any).code = 'auth/weak-password'
222
+
223
+ mockAuth.updateUser.mockRejectedValue(weakError)
224
+
225
+ await expect(
226
+ adapter.update(mockUser, updatable)
227
+ ).rejects.toThrow('weak_password')
228
+ })
219
229
  })
220
230
 
221
231
  describe('delete()', () => {
@@ -252,11 +262,11 @@ describe('FirebaseAuthAdapter', () => {
252
262
  json: jest.fn().mockResolvedValue(mockResponse),
253
263
  }
254
264
 
255
- ;(nativeFetch.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
265
+ ;(global.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
256
266
 
257
267
  const result = await adapter.refreshToken('old-refresh-token')
258
268
 
259
- expect(nativeFetch.fetch).toHaveBeenCalledWith(
269
+ expect(global.fetch).toHaveBeenCalledWith(
260
270
  'https://securetoken.googleapis.com/v1/token?key=test-api-key',
261
271
  {
262
272
  method: 'POST',
@@ -283,7 +293,7 @@ describe('FirebaseAuthAdapter', () => {
283
293
  const result = await adapterWithoutKey.refreshToken('refresh-token')
284
294
 
285
295
  expect(result).toEqual({})
286
- expect(nativeFetch.fetch).not.toHaveBeenCalled()
296
+ expect(global.fetch).not.toHaveBeenCalled()
287
297
  })
288
298
 
289
299
  it('should construct correct URL with API key', async () => {
@@ -291,11 +301,11 @@ describe('FirebaseAuthAdapter', () => {
291
301
  json: jest.fn().mockResolvedValue({}),
292
302
  }
293
303
 
294
- ;(nativeFetch.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
304
+ ;(global.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
295
305
 
296
306
  await adapter.refreshToken('refresh-token')
297
307
 
298
- const callArgs = (nativeFetch.fetch as unknown as jest.Mock).mock.calls[0]
308
+ const callArgs = (global.fetch as unknown as jest.Mock).mock.calls[0]
299
309
  expect(callArgs[0]).toBe(
300
310
  'https://securetoken.googleapis.com/v1/token?key=test-api-key'
301
311
  )
@@ -8,7 +8,6 @@ import {
8
8
 
9
9
  import { CreateRequest, UpdateRequest, getAuth } from 'firebase-admin/auth'
10
10
  import { getApps, initializeApp } from 'firebase-admin/app'
11
- import * as nativeFetch from 'node-fetch-native'
12
11
 
13
12
  /**
14
13
  * Authentication adapter implementing the Google Firebase Auth ecosystem.
@@ -105,7 +104,13 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
105
104
  Auth.log(`Updating ${updatable.displayName} Auth record`)
106
105
  await getAuth().updateUser(user.uid, updatable)
107
106
  }
108
- } catch (e) {}
107
+ } catch (e: any) {
108
+ Auth.error(e)
109
+ if (e.code === 'auth/weak-password' || e.message?.toLowerCase().includes('password')) {
110
+ throw new AuthenticationError(Auth.ERROR_WEAK_PASSWORD)
111
+ }
112
+ throw new AuthenticationError((e as Error).message)
113
+ }
109
114
  }
110
115
 
111
116
  /**
@@ -143,7 +148,7 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
143
148
  Auth.warn(`Can't get refresh token, no API key provided`)
144
149
  return {}
145
150
  }
146
- const response = await nativeFetch.fetch(
151
+ const response = await fetch(
147
152
  `https://securetoken.googleapis.com/v1/token?key=${this._params.config.apiKey}`,
148
153
  {
149
154
  method: 'POST',
@@ -184,7 +189,7 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
184
189
  }
185
190
  }
186
191
 
187
- const response = await nativeFetch.fetch(
192
+ const response = await fetch(
188
193
  `https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${this._params.config.apiKey}`,
189
194
  {
190
195
  method: 'POST',