@quatrain/auth-firebase 1.2.4 → 1.2.6
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,6 +1,5 @@
|
|
|
1
1
|
import { User } from '@quatrain/backend';
|
|
2
2
|
import { AbstractAuthAdapter, AuthParameters } from '@quatrain/auth';
|
|
3
|
-
import { ApiMiddleware } from '@quatrain/api';
|
|
4
3
|
import { UpdateRequest } from 'firebase-admin/auth';
|
|
5
4
|
/**
|
|
6
5
|
* Authentication adapter implementing the Google Firebase Auth ecosystem.
|
|
@@ -14,12 +13,6 @@ export declare class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
14
13
|
* @returns The adapter instance.
|
|
15
14
|
*/
|
|
16
15
|
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;
|
|
23
16
|
constructor(params?: AuthParameters);
|
|
24
17
|
/**
|
|
25
18
|
* Register new user in authentication
|
|
@@ -81,4 +74,11 @@ export declare class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
81
74
|
* @returns The generated token payload.
|
|
82
75
|
*/
|
|
83
76
|
refreshToken(refreshToken: string): Promise<any>;
|
|
77
|
+
/**
|
|
78
|
+
* Initiates password recovery/reset process for a user.
|
|
79
|
+
*
|
|
80
|
+
* @param email - The user email.
|
|
81
|
+
* @param redirectTo - Optional redirect destination.
|
|
82
|
+
*/
|
|
83
|
+
recoverPassword(email: string, redirectTo?: string): Promise<any>;
|
|
84
84
|
}
|
|
@@ -61,31 +61,6 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
61
61
|
static factory(config) {
|
|
62
62
|
return new FirebaseAuthAdapter({ config });
|
|
63
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
|
-
}
|
|
89
64
|
constructor(params = {}) {
|
|
90
65
|
super(params);
|
|
91
66
|
if ((0, app_1.getApps)().length === 0 && params.config) {
|
|
@@ -216,5 +191,42 @@ class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
|
216
191
|
return data;
|
|
217
192
|
});
|
|
218
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Initiates password recovery/reset process for a user.
|
|
196
|
+
*
|
|
197
|
+
* @param email - The user email.
|
|
198
|
+
* @param redirectTo - Optional redirect destination.
|
|
199
|
+
*/
|
|
200
|
+
recoverPassword(email, redirectTo) {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
202
|
+
var _a;
|
|
203
|
+
if (!this._params.config || !this._params.config.apiKey) {
|
|
204
|
+
auth_1.Auth.warn(`Can't recover password, no API key provided in Firebase config`);
|
|
205
|
+
throw new Error('Firebase Auth API key is not configured');
|
|
206
|
+
}
|
|
207
|
+
const body = {
|
|
208
|
+
requestType: 'PASSWORD_RESET',
|
|
209
|
+
email,
|
|
210
|
+
};
|
|
211
|
+
if (redirectTo) {
|
|
212
|
+
body.actionCodeSettings = {
|
|
213
|
+
url: redirectTo,
|
|
214
|
+
handleCodeInApp: true,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const response = yield nativeFetch.fetch(`https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${this._params.config.apiKey}`, {
|
|
218
|
+
method: 'POST',
|
|
219
|
+
body: JSON.stringify(body),
|
|
220
|
+
headers: { 'Content-Type': 'application/json' },
|
|
221
|
+
});
|
|
222
|
+
if (!response.ok) {
|
|
223
|
+
const errorData = yield response.json().catch(() => ({}));
|
|
224
|
+
const message = ((_a = errorData.error) === null || _a === void 0 ? void 0 : _a.message) || response.statusText;
|
|
225
|
+
throw new Error(message);
|
|
226
|
+
}
|
|
227
|
+
const data = yield response.json();
|
|
228
|
+
return data;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
219
231
|
}
|
|
220
232
|
exports.FirebaseAuthAdapter = FirebaseAuthAdapter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/auth-firebase",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
},
|
|
20
20
|
"author": "Quatrain Développement SAS <developers@quatrain.com>",
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@quatrain/core": "^1.2.
|
|
22
|
+
"@quatrain/core": "^1.2.16"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@quatrain/api": "^1.1.
|
|
26
|
-
"@quatrain/auth": "^1.2.
|
|
27
|
-
"@quatrain/backend": "^1.2.
|
|
28
|
-
"@quatrain/core": "^1.2.
|
|
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
29
|
"firebase-admin": "^13.0.1",
|
|
30
30
|
"node-fetch-native": "^1.6.4"
|
|
31
31
|
},
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
AuthenticationError,
|
|
6
6
|
AuthParameters,
|
|
7
7
|
} from '@quatrain/auth'
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
import { CreateRequest, UpdateRequest, getAuth } from 'firebase-admin/auth'
|
|
10
10
|
import { getApps, initializeApp } from 'firebase-admin/app'
|
|
11
11
|
import * as nativeFetch from 'node-fetch-native'
|
|
@@ -25,31 +25,7 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
25
25
|
return new FirebaseAuthAdapter({ config })
|
|
26
26
|
}
|
|
27
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
28
|
|
|
48
|
-
res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
|
|
49
|
-
res.status(401).send('Authentication required.')
|
|
50
|
-
return false
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
29
|
|
|
54
30
|
constructor(params: AuthParameters = {}) {
|
|
55
31
|
super(params)
|
|
@@ -183,4 +159,47 @@ export class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
|
183
159
|
|
|
184
160
|
return data
|
|
185
161
|
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Initiates password recovery/reset process for a user.
|
|
165
|
+
*
|
|
166
|
+
* @param email - The user email.
|
|
167
|
+
* @param redirectTo - Optional redirect destination.
|
|
168
|
+
*/
|
|
169
|
+
async recoverPassword(email: string, redirectTo?: string): Promise<any> {
|
|
170
|
+
if (!this._params.config || !this._params.config.apiKey) {
|
|
171
|
+
Auth.warn(`Can't recover password, no API key provided in Firebase config`)
|
|
172
|
+
throw new Error('Firebase Auth API key is not configured')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const body: any = {
|
|
176
|
+
requestType: 'PASSWORD_RESET',
|
|
177
|
+
email,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (redirectTo) {
|
|
181
|
+
body.actionCodeSettings = {
|
|
182
|
+
url: redirectTo,
|
|
183
|
+
handleCodeInApp: true,
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const response = await nativeFetch.fetch(
|
|
188
|
+
`https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=${this._params.config.apiKey}`,
|
|
189
|
+
{
|
|
190
|
+
method: 'POST',
|
|
191
|
+
body: JSON.stringify(body),
|
|
192
|
+
headers: { 'Content-Type': 'application/json' },
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
const errorData = await response.json().catch(() => ({}))
|
|
198
|
+
const message = errorData.error?.message || response.statusText
|
|
199
|
+
throw new Error(message)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const data = await response.json()
|
|
203
|
+
return data
|
|
204
|
+
}
|
|
186
205
|
}
|