@quatrain/auth-firebase 1.2.1 → 1.2.3
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,10 @@
|
|
|
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';
|
|
4
5
|
export declare class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
6
|
+
static factory(config: any): FirebaseAuthAdapter;
|
|
7
|
+
middleware(): ApiMiddleware;
|
|
5
8
|
constructor(params?: AuthParameters);
|
|
6
9
|
/**
|
|
7
10
|
* Register new user in authentication
|
|
@@ -48,9 +48,32 @@ 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
50
|
class FirebaseAuthAdapter extends auth_1.AbstractAuthAdapter {
|
|
51
|
+
static factory(config) {
|
|
52
|
+
return new FirebaseAuthAdapter({ config });
|
|
53
|
+
}
|
|
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
|
+
}
|
|
51
74
|
constructor(params = {}) {
|
|
52
75
|
super(params);
|
|
53
|
-
if ((0, app_1.getApps)().length === 0) {
|
|
76
|
+
if ((0, app_1.getApps)().length === 0 && params.config) {
|
|
54
77
|
(0, app_1.initializeApp)(params.config);
|
|
55
78
|
}
|
|
56
79
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/auth-firebase",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
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/api": "^1.1.4",
|
|
25
26
|
"@quatrain/auth": "^1.2.1",
|
|
26
|
-
"@quatrain/backend": "^1.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"
|
|
@@ -252,7 +252,7 @@ describe('FirebaseAuthAdapter', () => {
|
|
|
252
252
|
json: jest.fn().mockResolvedValue(mockResponse),
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
;(nativeFetch.fetch as jest.Mock).mockResolvedValue(mockFetchResponse)
|
|
255
|
+
;(nativeFetch.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
|
|
256
256
|
|
|
257
257
|
const result = await adapter.refreshToken('old-refresh-token')
|
|
258
258
|
|
|
@@ -291,11 +291,11 @@ describe('FirebaseAuthAdapter', () => {
|
|
|
291
291
|
json: jest.fn().mockResolvedValue({}),
|
|
292
292
|
}
|
|
293
293
|
|
|
294
|
-
;(nativeFetch.fetch as jest.Mock).mockResolvedValue(mockFetchResponse)
|
|
294
|
+
;(nativeFetch.fetch as unknown as jest.Mock).mockResolvedValue(mockFetchResponse)
|
|
295
295
|
|
|
296
296
|
await adapter.refreshToken('refresh-token')
|
|
297
297
|
|
|
298
|
-
const callArgs = (nativeFetch.fetch as jest.Mock).mock.calls[0]
|
|
298
|
+
const callArgs = (nativeFetch.fetch as unknown as jest.Mock).mock.calls[0]
|
|
299
299
|
expect(callArgs[0]).toBe(
|
|
300
300
|
'https://securetoken.googleapis.com/v1/token?key=test-api-key'
|
|
301
301
|
)
|
|
@@ -5,14 +5,40 @@ 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
|
|
|
12
13
|
export class FirebaseAuthAdapter extends AbstractAuthAdapter {
|
|
14
|
+
static factory(config: any): FirebaseAuthAdapter {
|
|
15
|
+
return new FirebaseAuthAdapter({ config })
|
|
16
|
+
}
|
|
17
|
+
|
|
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
|
+
|
|
33
|
+
res.setHeader('WWW-Authenticate', 'Bearer realm="Core API"')
|
|
34
|
+
res.status(401).send('Authentication required.')
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
13
39
|
constructor(params: AuthParameters = {}) {
|
|
14
40
|
super(params)
|
|
15
|
-
if (getApps().length === 0) {
|
|
41
|
+
if (getApps().length === 0 && params.config) {
|
|
16
42
|
initializeApp(params.config)
|
|
17
43
|
}
|
|
18
44
|
}
|