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