@quatrain/auth-firebase 1.2.2 → 1.2.4

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,7 +1,25 @@
1
1
  import { User } from '@quatrain/backend';
2
2
  import { AbstractAuthAdapter, AuthParameters } from '@quatrain/auth';
3
+ import { ApiMiddleware } from '@quatrain/api';
3
4
  import { UpdateRequest } from 'firebase-admin/auth';
5
+ /**
6
+ * Authentication adapter implementing the Google Firebase Auth ecosystem.
7
+ * Handles server-side validation of JWTs and admin functions via `firebase-admin`.
8
+ */
4
9
  export declare class FirebaseAuthAdapter extends AbstractAuthAdapter {
10
+ /**
11
+ * Bootstraps the adapter using Firebase credentials.
12
+ *
13
+ * @param config - Firebase admin SDK configuration block.
14
+ * @returns The adapter instance.
15
+ */
16
+ static factory(config: any): FirebaseAuthAdapter;
17
+ /**
18
+ * Express middleware that intercepts and verifies incoming Firebase JWT Bearer tokens.
19
+ *
20
+ * @returns The validation middleware function.
21
+ */
22
+ middleware(): ApiMiddleware;
5
23
  constructor(params?: AuthParameters);
6
24
  /**
7
25
  * Register new user in authentication
@@ -9,12 +27,58 @@ export declare class FirebaseAuthAdapter extends AbstractAuthAdapter {
9
27
  * @returns user unique id
10
28
  */
11
29
  register(user: User, clearPassword?: string): Promise<string>;
30
+ /**
31
+ * Verifies a Firebase ID token using the admin SDK.
32
+ *
33
+ * @param bearer - The JWT token string.
34
+ * @returns A promise resolving to the decoded token object.
35
+ */
12
36
  getAuthToken(bearer: string): Promise<import("firebase-admin/auth").DecodedIdToken>;
37
+ /**
38
+ * User authentication flow (Not natively supported on Firebase Admin Server SDK).
39
+ * Usually handled on the client.
40
+ *
41
+ * @param login - Email address.
42
+ * @param password - Plain password.
43
+ */
13
44
  signup(login: string, password: string): Promise<void>;
45
+ /**
46
+ * Signs out the user (No-op in server-side stateless Firebase admin).
47
+ *
48
+ * @param user - Target user.
49
+ */
14
50
  signout(user: User): Promise<any>;
51
+ /**
52
+ * Modifies an existing user's attributes in the Firebase Auth registry.
53
+ *
54
+ * @param user - Target user.
55
+ * @param updatable - Properties to modify (e.g. displayName, disabled status).
56
+ */
15
57
  update(user: User, updatable: UpdateRequest): Promise<any>;
58
+ /**
59
+ * Completely removes a user from the Firebase Auth registry.
60
+ *
61
+ * @param user - Target user.
62
+ */
16
63
  delete(user: User): Promise<any>;
64
+ /**
65
+ * Revokes a specific access token.
66
+ *
67
+ * @param token - The token string.
68
+ */
17
69
  revokeAuthToken(token: string): Promise<void>;
70
+ /**
71
+ * Updates the custom JWT claims embedded in a user's Firebase token.
72
+ *
73
+ * @param id - User UID.
74
+ * @param claims - Claims dictionary.
75
+ */
18
76
  setCustomUserClaims(id: string, claims: any): Promise<void>;
77
+ /**
78
+ * Requests a new ID token using a refresh token via Google Secure Token API.
79
+ *
80
+ * @param refreshToken - The active refresh token.
81
+ * @returns The generated token payload.
82
+ */
19
83
  refreshToken(refreshToken: string): Promise<any>;
20
84
  }
@@ -47,10 +47,48 @@ const auth_1 = require("@quatrain/auth");
47
47
  const auth_2 = require("firebase-admin/auth");
48
48
  const app_1 = require("firebase-admin/app");
49
49
  const nativeFetch = __importStar(require("node-fetch-native"));
50
+ /**
51
+ * Authentication adapter implementing the Google Firebase Auth ecosystem.
52
+ * Handles server-side validation of JWTs and admin functions via `firebase-admin`.
53
+ */
50
54
  class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
55
+ /**
56
+ * Bootstraps the adapter using Firebase credentials.
57
+ *
58
+ * @param config - Firebase admin SDK configuration block.
59
+ * @returns The adapter instance.
60
+ */
61
+ static factory(config) {
62
+ return new FirebaseAuthAdapter({ config });
63
+ }
64
+ /**
65
+ * Express middleware that intercepts and verifies incoming Firebase JWT Bearer tokens.
66
+ *
67
+ * @returns The validation middleware function.
68
+ */
69
+ middleware() {
70
+ return (req, res) => __awaiter(this, void 0, void 0, function* () {
71
+ var _a;
72
+ const bearer = (((_a = req.headers) === null || _a === void 0 ? void 0 : _a.authorization) || '').split(' ')[1] || '';
73
+ if (bearer) {
74
+ try {
75
+ const user = yield this.getAuthToken(bearer);
76
+ if (user) {
77
+ return true; // Authorized
78
+ }
79
+ }
80
+ catch (e) {
81
+ auth_1.Auth.error(`[FirebaseAuthAdapter] Middleware token verification failed: ${e.message}`);
82
+ }
83
+ }
84
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"');
85
+ res.status(401).send('Authentication required.');
86
+ return false;
87
+ });
88
+ }
51
89
  constructor(params = {}) {
52
90
  super(params);
53
- if ((0, app_1.getApps)().length === 0) {
91
+ if ((0, app_1.getApps)().length === 0 && params.config) {
54
92
  (0, app_1.initializeApp)(params.config);
55
93
  }
56
94
  }
@@ -80,17 +118,41 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
80
118
  }
81
119
  });
82
120
  }
121
+ /**
122
+ * Verifies a Firebase ID token using the admin SDK.
123
+ *
124
+ * @param bearer - The JWT token string.
125
+ * @returns A promise resolving to the decoded token object.
126
+ */
83
127
  getAuthToken(bearer) {
84
128
  return __awaiter(this, void 0, void 0, function* () {
85
129
  return yield (0, auth_2.getAuth)().verifyIdToken(bearer);
86
130
  });
87
131
  }
132
+ /**
133
+ * User authentication flow (Not natively supported on Firebase Admin Server SDK).
134
+ * Usually handled on the client.
135
+ *
136
+ * @param login - Email address.
137
+ * @param password - Plain password.
138
+ */
88
139
  signup(login, password) {
89
140
  return __awaiter(this, void 0, void 0, function* () { });
90
141
  }
142
+ /**
143
+ * Signs out the user (No-op in server-side stateless Firebase admin).
144
+ *
145
+ * @param user - Target user.
146
+ */
91
147
  signout(user) {
92
148
  return __awaiter(this, void 0, void 0, function* () { });
93
149
  }
150
+ /**
151
+ * Modifies an existing user's attributes in the Firebase Auth registry.
152
+ *
153
+ * @param user - Target user.
154
+ * @param updatable - Properties to modify (e.g. displayName, disabled status).
155
+ */
94
156
  update(user, updatable) {
95
157
  return __awaiter(this, void 0, void 0, function* () {
96
158
  auth_1.Auth.log('auth data to update', JSON.stringify(updatable));
@@ -103,17 +165,39 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
103
165
  catch (e) { }
104
166
  });
105
167
  }
168
+ /**
169
+ * Completely removes a user from the Firebase Auth registry.
170
+ *
171
+ * @param user - Target user.
172
+ */
106
173
  delete(user) {
107
174
  return __awaiter(this, void 0, void 0, function* () {
108
175
  return yield (0, auth_2.getAuth)().deleteUser(user.uid);
109
176
  });
110
177
  }
178
+ /**
179
+ * Revokes a specific access token.
180
+ *
181
+ * @param token - The token string.
182
+ */
111
183
  revokeAuthToken(token) {
112
184
  return __awaiter(this, void 0, void 0, function* () { });
113
185
  }
186
+ /**
187
+ * Updates the custom JWT claims embedded in a user's Firebase token.
188
+ *
189
+ * @param id - User UID.
190
+ * @param claims - Claims dictionary.
191
+ */
114
192
  setCustomUserClaims(id, claims) {
115
193
  return __awaiter(this, void 0, void 0, function* () { });
116
194
  }
195
+ /**
196
+ * Requests a new ID token using a refresh token via Google Secure Token API.
197
+ *
198
+ * @param refreshToken - The active refresh token.
199
+ * @returns The generated token payload.
200
+ */
117
201
  refreshToken(refreshToken) {
118
202
  return __awaiter(this, void 0, void 0, function* () {
119
203
  if (!this._params.config.apiKey) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/auth-firebase",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "license": "AGPL-3.0-only",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,8 +22,9 @@
22
22
  "@quatrain/core": "^1.2.5"
23
23
  },
24
24
  "dependencies": {
25
- "@quatrain/auth": "^1.2.1",
26
- "@quatrain/backend": "^1.2.5",
25
+ "@quatrain/api": "^1.1.5",
26
+ "@quatrain/auth": "^1.2.2",
27
+ "@quatrain/backend": "^1.2.6",
27
28
  "@quatrain/core": "^1.2.5",
28
29
  "firebase-admin": "^13.0.1",
29
30
  "node-fetch-native": "^1.6.4"
@@ -5,14 +5,55 @@ import {
5
5
  AuthenticationError,
6
6
  AuthParameters,
7
7
  } from '@quatrain/auth'
8
+ import { ApiMiddleware, ApiRequest, ApiResponse } from '@quatrain/api'
8
9
  import { CreateRequest, UpdateRequest, getAuth } from 'firebase-admin/auth'
9
10
  import { getApps, initializeApp } from 'firebase-admin/app'
10
11
  import * as nativeFetch from 'node-fetch-native'
11
12
 
13
+ /**
14
+ * Authentication adapter implementing the Google Firebase Auth ecosystem.
15
+ * Handles server-side validation of JWTs and admin functions via `firebase-admin`.
16
+ */
12
17
  export class FirebaseAuthAdapter extends AbstractAuthAdapter {
18
+ /**
19
+ * Bootstraps the adapter using Firebase credentials.
20
+ *
21
+ * @param config - Firebase admin SDK configuration block.
22
+ * @returns The adapter instance.
23
+ */
24
+ static factory(config: any): FirebaseAuthAdapter {
25
+ return new FirebaseAuthAdapter({ config })
26
+ }
27
+
28
+ /**
29
+ * Express middleware that intercepts and verifies incoming Firebase JWT Bearer tokens.
30
+ *
31
+ * @returns The validation middleware function.
32
+ */
33
+ public middleware(): ApiMiddleware {
34
+ return async (req: ApiRequest, res: ApiResponse): Promise<boolean> => {
35
+ const bearer = ((req.headers?.authorization as string) || '').split(' ')[1] || ''
36
+
37
+ if (bearer) {
38
+ try {
39
+ const user = await this.getAuthToken(bearer)
40
+ if (user) {
41
+ return true // Authorized
42
+ }
43
+ } catch(e) {
44
+ Auth.error(`[FirebaseAuthAdapter] Middleware token verification failed: ${(e as Error).message}`)
45
+ }
46
+ }
47
+
48
+ res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
49
+ res.status(401).send('Authentication required.')
50
+ return false
51
+ }
52
+ }
53
+
13
54
  constructor(params: AuthParameters = {}) {
14
55
  super(params)
15
- if (getApps().length === 0) {
56
+ if (getApps().length === 0 && params.config) {
16
57
  initializeApp(params.config)
17
58
  }
18
59
  }
@@ -48,14 +89,38 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
48
89
  }
49
90
  }
50
91
 
92
+ /**
93
+ * Verifies a Firebase ID token using the admin SDK.
94
+ *
95
+ * @param bearer - The JWT token string.
96
+ * @returns A promise resolving to the decoded token object.
97
+ */
51
98
  async getAuthToken(bearer: string) {
52
99
  return await getAuth().verifyIdToken(bearer)
53
100
  }
54
101
 
102
+ /**
103
+ * User authentication flow (Not natively supported on Firebase Admin Server SDK).
104
+ * Usually handled on the client.
105
+ *
106
+ * @param login - Email address.
107
+ * @param password - Plain password.
108
+ */
55
109
  async signup(login: string, password: string) {}
56
110
 
111
+ /**
112
+ * Signs out the user (No-op in server-side stateless Firebase admin).
113
+ *
114
+ * @param user - Target user.
115
+ */
57
116
  async signout(user: User): Promise<any> {}
58
117
 
118
+ /**
119
+ * Modifies an existing user's attributes in the Firebase Auth registry.
120
+ *
121
+ * @param user - Target user.
122
+ * @param updatable - Properties to modify (e.g. displayName, disabled status).
123
+ */
59
124
  async update(user: User, updatable: UpdateRequest): Promise<any> {
60
125
  Auth.log('auth data to update', JSON.stringify(updatable))
61
126
 
@@ -67,14 +132,36 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
67
132
  } catch (e) {}
68
133
  }
69
134
 
135
+ /**
136
+ * Completely removes a user from the Firebase Auth registry.
137
+ *
138
+ * @param user - Target user.
139
+ */
70
140
  async delete(user: User): Promise<any> {
71
141
  return await getAuth().deleteUser(user.uid)
72
142
  }
73
143
 
144
+ /**
145
+ * Revokes a specific access token.
146
+ *
147
+ * @param token - The token string.
148
+ */
74
149
  async revokeAuthToken(token: string) {}
75
150
 
151
+ /**
152
+ * Updates the custom JWT claims embedded in a user's Firebase token.
153
+ *
154
+ * @param id - User UID.
155
+ * @param claims - Claims dictionary.
156
+ */
76
157
  async setCustomUserClaims(id: string, claims: any) {}
77
158
 
159
+ /**
160
+ * Requests a new ID token using a refresh token via Google Secure Token API.
161
+ *
162
+ * @param refreshToken - The active refresh token.
163
+ * @returns The generated token payload.
164
+ */
78
165
  async refreshToken(refreshToken: string): Promise<any> {
79
166
  if (!this._params.config.apiKey) {
80
167
  Auth.warn(`Can't get refresh token, no API key provided`)