@furystack/rest-service 4.1.9 → 5.0.0
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/CHANGELOG.md +177 -0
- package/dist/actions/index.js +6 -6
- package/dist/actions/index.js.map +1 -1
- package/dist/api-manager.js +2 -2
- package/dist/api-manager.js.map +1 -1
- package/dist/authenticate.spec.js +3 -3
- package/dist/authenticate.spec.js.map +1 -1
- package/dist/authorize.spec.js +3 -3
- package/dist/authorize.spec.js.map +1 -1
- package/dist/endpoint-generators/create-delete-endpoint.spec.js +1 -1
- package/dist/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js +1 -1
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js +1 -1
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-patch-endpoint.spec.js +1 -1
- package/dist/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-post-endpoint.spec.js +1 -1
- package/dist/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/index.js +5 -5
- package/dist/endpoint-generators/index.js.map +1 -1
- package/dist/http-authentication-settings.d.ts +1 -4
- package/dist/http-authentication-settings.d.ts.map +1 -1
- package/dist/http-authentication-settings.js +1 -3
- package/dist/http-authentication-settings.js.map +1 -1
- package/dist/http-user-context.d.ts +5 -8
- package/dist/http-user-context.d.ts.map +1 -1
- package/dist/http-user-context.js +19 -28
- package/dist/http-user-context.js.map +1 -1
- package/dist/http-user-context.spec.d.ts.map +1 -1
- package/dist/http-user-context.spec.js +33 -26
- package/dist/http-user-context.spec.js.map +1 -1
- package/dist/incoming-message-extensions.js +1 -1
- package/dist/incoming-message-extensions.js.map +1 -1
- package/dist/index.js +15 -15
- package/dist/index.js.map +1 -1
- package/dist/models/index.js +2 -2
- package/dist/models/index.js.map +1 -1
- package/dist/rest-service.integration.spec.js +1 -1
- package/dist/rest-service.integration.spec.js.map +1 -1
- package/dist/schema-validator/index.js +2 -2
- package/dist/schema-validator/index.js.map +1 -1
- package/dist/schema-validator/schema-validator.js +2 -2
- package/dist/schema-validator/schema-validator.js.map +1 -1
- package/dist/server-manager.js +2 -2
- package/dist/server-manager.js.map +1 -1
- package/dist/server-response-extensions.js +1 -1
- package/dist/server-response-extensions.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/validate.integration.spec.d.ts +0 -11
- package/dist/validate.integration.spec.d.ts.map +1 -1
- package/dist/validate.integration.spec.js +9 -11
- package/dist/validate.integration.spec.js.map +1 -1
- package/dist/validate.js.map +1 -1
- package/package.json +14 -16
- package/src/authenticate.spec.ts +3 -3
- package/src/authorize.spec.ts +3 -3
- package/src/http-authentication-settings.ts +2 -5
- package/src/http-user-context.spec.ts +44 -26
- package/src/http-user-context.ts +15 -25
- package/src/validate.integration.spec.ts +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createValidateApi = void 0;
|
|
4
3
|
const tslib_1 = require("tslib");
|
|
5
4
|
const inject_1 = require("@furystack/inject");
|
|
6
5
|
const rest_client_got_1 = require("@furystack/rest-client-got");
|
|
@@ -9,7 +8,7 @@ const source_1 = require("got/dist/source");
|
|
|
9
8
|
const request_action_implementation_1 = require("./request-action-implementation");
|
|
10
9
|
const validate_1 = require("./validate");
|
|
11
10
|
require("./injector-extensions");
|
|
12
|
-
const validate_integration_spec_schema_json_1 =
|
|
11
|
+
const validate_integration_spec_schema_json_1 = tslib_1.__importDefault(require("./validate.integration.spec.schema.json"));
|
|
13
12
|
// To recreate: yarn ts-json-schema-generator -f tsconfig.json --no-type-check -p packages/rest-service/src/validate.integration.schema.ts -o packages/rest-service/src/validate.integration.spec.schema.json
|
|
14
13
|
const createValidateApi = async () => {
|
|
15
14
|
const injector = new inject_1.Injector();
|
|
@@ -60,11 +59,10 @@ const createValidateApi = async () => {
|
|
|
60
59
|
client,
|
|
61
60
|
};
|
|
62
61
|
};
|
|
63
|
-
exports.createValidateApi = createValidateApi;
|
|
64
62
|
describe('Validation integration tests', () => {
|
|
65
63
|
describe('Validation errors', () => {
|
|
66
64
|
it('Should validate query', async () => {
|
|
67
|
-
await (0, utils_1.usingAsync)(await
|
|
65
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
68
66
|
var _a, _b;
|
|
69
67
|
expect.assertions(5);
|
|
70
68
|
try {
|
|
@@ -87,7 +85,7 @@ describe('Validation integration tests', () => {
|
|
|
87
85
|
});
|
|
88
86
|
});
|
|
89
87
|
it('Should validate url', async () => {
|
|
90
|
-
await (0, utils_1.usingAsync)(await
|
|
88
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
91
89
|
var _a, _b;
|
|
92
90
|
expect.assertions(4);
|
|
93
91
|
try {
|
|
@@ -109,7 +107,7 @@ describe('Validation integration tests', () => {
|
|
|
109
107
|
});
|
|
110
108
|
});
|
|
111
109
|
it('Should validate headers', async () => {
|
|
112
|
-
await (0, utils_1.usingAsync)(await
|
|
110
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
113
111
|
var _a, _b;
|
|
114
112
|
expect.assertions(3);
|
|
115
113
|
try {
|
|
@@ -130,7 +128,7 @@ describe('Validation integration tests', () => {
|
|
|
130
128
|
});
|
|
131
129
|
});
|
|
132
130
|
it('Should validate body', async () => {
|
|
133
|
-
await (0, utils_1.usingAsync)(await
|
|
131
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
134
132
|
var _a, _b;
|
|
135
133
|
expect.assertions(3);
|
|
136
134
|
try {
|
|
@@ -153,7 +151,7 @@ describe('Validation integration tests', () => {
|
|
|
153
151
|
});
|
|
154
152
|
describe('Validation Success', () => {
|
|
155
153
|
it('Should validate query', async () => {
|
|
156
|
-
await (0, utils_1.usingAsync)(await
|
|
154
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
157
155
|
const result = await client({
|
|
158
156
|
method: 'GET',
|
|
159
157
|
action: '/validate-query',
|
|
@@ -171,7 +169,7 @@ describe('Validation integration tests', () => {
|
|
|
171
169
|
});
|
|
172
170
|
});
|
|
173
171
|
it('Should validate url', async () => {
|
|
174
|
-
await (0, utils_1.usingAsync)(await
|
|
172
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
175
173
|
const result = await client({
|
|
176
174
|
method: 'GET',
|
|
177
175
|
action: '/validate-url/:id',
|
|
@@ -183,7 +181,7 @@ describe('Validation integration tests', () => {
|
|
|
183
181
|
});
|
|
184
182
|
});
|
|
185
183
|
it('Should validate headers', async () => {
|
|
186
|
-
await (0, utils_1.usingAsync)(await
|
|
184
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
187
185
|
const result = await client({
|
|
188
186
|
method: 'GET',
|
|
189
187
|
action: '/validate-headers',
|
|
@@ -201,7 +199,7 @@ describe('Validation integration tests', () => {
|
|
|
201
199
|
});
|
|
202
200
|
});
|
|
203
201
|
it('Should validate body', async () => {
|
|
204
|
-
await (0, utils_1.usingAsync)(await
|
|
202
|
+
await (0, utils_1.usingAsync)(await createValidateApi(), async ({ client }) => {
|
|
205
203
|
const result = await client({
|
|
206
204
|
method: 'POST',
|
|
207
205
|
action: '/validate-body',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.integration.spec.js","sourceRoot":"","sources":["../src/validate.integration.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate.integration.spec.js","sourceRoot":"","sources":["../src/validate.integration.spec.ts"],"names":[],"mappings":";;;AAAA,8CAA4C;AAC5C,gEAAyD;AACzD,4CAA6C;AAC7C,4CAA8C;AAC9C,mFAA4D;AAC5D,yC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rest-service",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Repository implementation for FuryStack",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"start:mock": "yarn node ./dist/mock.js"
|
|
8
|
-
},
|
|
9
6
|
"files": [
|
|
10
7
|
"dist",
|
|
11
8
|
"src"
|
|
@@ -31,26 +28,27 @@
|
|
|
31
28
|
},
|
|
32
29
|
"homepage": "https://github.com/furystack/furystack",
|
|
33
30
|
"dependencies": {
|
|
34
|
-
"@furystack/core": "^10.4.
|
|
35
|
-
"@furystack/inject": "^5.0.
|
|
36
|
-
"@furystack/logging": "^2.0.
|
|
37
|
-
"@furystack/repository": "^5.0.
|
|
38
|
-
"@furystack/rest": "^3.1.
|
|
39
|
-
"@furystack/
|
|
40
|
-
"
|
|
31
|
+
"@furystack/core": "^10.4.9",
|
|
32
|
+
"@furystack/inject": "^5.0.6",
|
|
33
|
+
"@furystack/logging": "^2.0.90",
|
|
34
|
+
"@furystack/repository": "^5.0.33",
|
|
35
|
+
"@furystack/rest": "^3.1.11",
|
|
36
|
+
"@furystack/security": "^1.0.0",
|
|
37
|
+
"@furystack/utils": "^2.0.5",
|
|
38
|
+
"ajv": "^8.11.0",
|
|
41
39
|
"ajv-formats": "^2.1.1",
|
|
42
|
-
"hash.js": "^1.1.7",
|
|
43
40
|
"path-to-regexp": "^6.2.0",
|
|
44
41
|
"semaphore-async-await": "^1.5.1",
|
|
45
42
|
"tslib": "^2.3.1",
|
|
46
43
|
"uuid": "^8.3.2"
|
|
47
44
|
},
|
|
48
45
|
"devDependencies": {
|
|
49
|
-
"@
|
|
50
|
-
"@types/
|
|
46
|
+
"@furystack/rest-client-got": "^1.0.42",
|
|
47
|
+
"@types/jest": "^27.4.1",
|
|
48
|
+
"@types/node": "^17.0.23",
|
|
51
49
|
"@types/uuid": "^8.3.4",
|
|
52
50
|
"got": "^11.8.3"
|
|
53
51
|
},
|
|
54
52
|
"typings": "./dist/index.d.ts",
|
|
55
|
-
"gitHead": "
|
|
56
|
-
}
|
|
53
|
+
"gitHead": "1045d854bfd8c475b7035471d130d401417a2321"
|
|
54
|
+
}
|
package/src/authenticate.spec.ts
CHANGED
|
@@ -26,7 +26,7 @@ describe('Authenticate', () => {
|
|
|
26
26
|
},
|
|
27
27
|
HttpUserContext,
|
|
28
28
|
)
|
|
29
|
-
const exampleAuthenticatedAction = jest.fn(async (
|
|
29
|
+
const exampleAuthenticatedAction = jest.fn(async () => EmptyResult())
|
|
30
30
|
const authorized = Authenticate()(exampleAuthenticatedAction)
|
|
31
31
|
|
|
32
32
|
const result = await authorized({ injector: i, request, response })
|
|
@@ -48,7 +48,7 @@ describe('Authenticate', () => {
|
|
|
48
48
|
},
|
|
49
49
|
HttpUserContext,
|
|
50
50
|
)
|
|
51
|
-
const exampleAuthenticatedAction = jest.fn(async (
|
|
51
|
+
const exampleAuthenticatedAction = jest.fn(async () => EmptyResult())
|
|
52
52
|
const authorized = Authenticate()(exampleAuthenticatedAction)
|
|
53
53
|
|
|
54
54
|
const result = await authorized({ injector: i, request, response })
|
|
@@ -66,7 +66,7 @@ describe('Authenticate', () => {
|
|
|
66
66
|
{ isAuthenticated: isAuthenticatedAction, getCurrentUser: async () => Promise.reject(':(') },
|
|
67
67
|
IdentityContext,
|
|
68
68
|
)
|
|
69
|
-
const exampleAuthenticatedAction = jest.fn(async (
|
|
69
|
+
const exampleAuthenticatedAction = jest.fn(async () => EmptyResult())
|
|
70
70
|
const authorized = Authenticate()(exampleAuthenticatedAction)
|
|
71
71
|
const params = { injector: i, body: undefined, query: undefined, request, response }
|
|
72
72
|
const result = await authorized(params)
|
package/src/authorize.spec.ts
CHANGED
|
@@ -17,7 +17,7 @@ describe('Authorize', () => {
|
|
|
17
17
|
{ isAuthorized: isAuthorizedAction, getCurrentUser: () => Promise.reject(':(') },
|
|
18
18
|
IdentityContext,
|
|
19
19
|
)
|
|
20
|
-
const exampleAuthorizedAction = jest.fn(async (
|
|
20
|
+
const exampleAuthorizedAction = jest.fn(async () => EmptyResult())
|
|
21
21
|
const authorized = Authorize('Role1')(exampleAuthorizedAction)
|
|
22
22
|
|
|
23
23
|
const result = await authorized({ injector: i, request, response })
|
|
@@ -37,7 +37,7 @@ describe('Authorize', () => {
|
|
|
37
37
|
},
|
|
38
38
|
IdentityContext,
|
|
39
39
|
)
|
|
40
|
-
const exampleAuthorizedAction = jest.fn(async (
|
|
40
|
+
const exampleAuthorizedAction = jest.fn(async () => EmptyResult())
|
|
41
41
|
const authorized = Authorize('Role2')(exampleAuthorizedAction)
|
|
42
42
|
|
|
43
43
|
const result = await authorized({ injector: i, request, response })
|
|
@@ -57,7 +57,7 @@ describe('Authorize', () => {
|
|
|
57
57
|
},
|
|
58
58
|
IdentityContext,
|
|
59
59
|
)
|
|
60
|
-
const exampleAuthorizedAction = jest.fn(async (
|
|
60
|
+
const exampleAuthorizedAction = jest.fn(async () => EmptyResult())
|
|
61
61
|
const authorized = Authorize('Role1')(exampleAuthorizedAction)
|
|
62
62
|
const params = { injector: i, body: undefined, query: undefined, request, response }
|
|
63
63
|
const result = await authorized(params)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { PhysicalStore, User, StoreManager } from '@furystack/core'
|
|
2
2
|
import { Constructable, Injectable } from '@furystack/inject'
|
|
3
|
-
import { sha256 } from 'hash.js'
|
|
4
3
|
import { DefaultSession } from './models/default-session'
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -10,14 +9,12 @@ import { DefaultSession } from './models/default-session'
|
|
|
10
9
|
export class HttpAuthenticationSettings<TUser extends User, TSession extends DefaultSession> {
|
|
11
10
|
public model: Constructable<TUser> = User as Constructable<TUser>
|
|
12
11
|
|
|
13
|
-
public getUserStore: (storeManager: StoreManager) => PhysicalStore<TUser
|
|
14
|
-
sm,
|
|
15
|
-
) => sm.getStoreFor<TUser & { password: string }, keyof TUser>(User as any, 'username')
|
|
12
|
+
public getUserStore: (storeManager: StoreManager) => PhysicalStore<TUser, keyof TUser> = (sm) =>
|
|
13
|
+
sm.getStoreFor<TUser, keyof TUser>(User as any, 'username')
|
|
16
14
|
|
|
17
15
|
public getSessionStore: (storeManager: StoreManager) => PhysicalStore<TSession, keyof TSession> = (sm) =>
|
|
18
16
|
sm.getStoreFor(DefaultSession, 'sessionId') as unknown as PhysicalStore<TSession, keyof TSession>
|
|
19
17
|
|
|
20
18
|
public cookieName = 'fss'
|
|
21
|
-
public hashMethod: (plain: string) => string = (plain) => sha256().update(plain).digest('hex')
|
|
22
19
|
public enableBasicAuth = true
|
|
23
20
|
}
|
|
@@ -5,18 +5,29 @@ import { User, StoreManager, InMemoryStore } from '@furystack/core'
|
|
|
5
5
|
import { DefaultSession } from './models/default-session'
|
|
6
6
|
import { HttpUserContext } from './http-user-context'
|
|
7
7
|
import './injector-extensions'
|
|
8
|
+
import { PasswordAuthenticator, PasswordCredential, UnauthenticatedError } from '@furystack/security'
|
|
8
9
|
|
|
9
10
|
export const prepareInjector = async (i: Injector) => {
|
|
10
11
|
i.setupStores((sm) =>
|
|
11
12
|
sm
|
|
12
13
|
.addStore(new InMemoryStore({ model: User, primaryKey: 'username' }))
|
|
13
|
-
.addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }))
|
|
14
|
+
.addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }))
|
|
15
|
+
.addStore(new InMemoryStore({ model: PasswordCredential, primaryKey: 'userName' })),
|
|
14
16
|
)
|
|
15
17
|
|
|
16
18
|
i.useHttpAuthentication()
|
|
17
19
|
// await i.getInstance(ServerManager).getOrCreate({ port: 19999 })
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
const setupUser = async (i: Injector, userName: string, password: string) => {
|
|
23
|
+
const sm = i.getInstance(StoreManager)
|
|
24
|
+
const pw = i.getInstance(PasswordAuthenticator)
|
|
25
|
+
const hasher = pw.getHasher()
|
|
26
|
+
const cred = await hasher.createCredential(userName, password)
|
|
27
|
+
await sm.getStoreFor(PasswordCredential, 'userName').add(cred)
|
|
28
|
+
await sm.getStoreFor(User, 'username').add({ username: userName, roles: [] })
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
describe('HttpUserContext', () => {
|
|
21
32
|
const request = { headers: {} } as IncomingMessage
|
|
22
33
|
const response = {} as any as ServerResponse
|
|
@@ -85,52 +96,58 @@ describe('HttpUserContext', () => {
|
|
|
85
96
|
await usingAsync(new Injector(), async (i) => {
|
|
86
97
|
await prepareInjector(i)
|
|
87
98
|
const ctx = i.getInstance(HttpUserContext)
|
|
88
|
-
await expect(ctx.authenticateUser('user', 'password')).rejects.
|
|
99
|
+
await expect(ctx.authenticateUser('user', 'password')).rejects.toThrowError(UnauthenticatedError)
|
|
89
100
|
})
|
|
90
101
|
})
|
|
91
102
|
|
|
92
103
|
it('Should fail when the password not equals', async () => {
|
|
93
104
|
await usingAsync(new Injector(), async (i) => {
|
|
94
105
|
await prepareInjector(i)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
await expect(ctx.authenticateUser('user', 'pass321')).rejects.toThrow('')
|
|
106
|
+
await setupUser(i, 'user', 'pass123')
|
|
107
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass321')).rejects.toThrowError(
|
|
108
|
+
UnauthenticatedError,
|
|
109
|
+
)
|
|
100
110
|
})
|
|
101
111
|
})
|
|
102
112
|
|
|
103
113
|
it('Should fail when the username not equals', async () => {
|
|
104
114
|
await usingAsync(new Injector(), async (i) => {
|
|
105
115
|
await prepareInjector(i)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
expect(ctx.authenticateUser('user', 'pass123')).rejects.toThrow('')
|
|
116
|
+
await setupUser(i, 'otherUser', 'pass123')
|
|
117
|
+
expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass123')).rejects.toThrowError(
|
|
118
|
+
UnauthenticatedError,
|
|
119
|
+
)
|
|
111
120
|
})
|
|
112
121
|
})
|
|
113
122
|
|
|
114
123
|
it('Should fail when password not provided', async () => {
|
|
115
124
|
await usingAsync(new Injector(), async (i) => {
|
|
116
125
|
await prepareInjector(i)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
await expect(ctx.authenticateUser('user', '')).rejects.toThrow('')
|
|
126
|
+
await setupUser(i, 'user', 'pass123')
|
|
127
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', '')).rejects.toThrowError(
|
|
128
|
+
UnauthenticatedError,
|
|
129
|
+
)
|
|
122
130
|
})
|
|
123
131
|
})
|
|
124
132
|
|
|
125
|
-
it('Should
|
|
133
|
+
it('Should fail when the user is not in the user store', async () => {
|
|
126
134
|
await usingAsync(new Injector(), async (i) => {
|
|
127
135
|
await prepareInjector(i)
|
|
136
|
+
await setupUser(i, 'user', 'pass123')
|
|
137
|
+
await i.getInstance(StoreManager).getStoreFor(User, 'username').remove('user')
|
|
138
|
+
await expect(i.getInstance(HttpUserContext).authenticateUser('user', 'pass123')).rejects.toThrowError(
|
|
139
|
+
UnauthenticatedError,
|
|
140
|
+
)
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('Should return the user when the username and password matches', async () => {
|
|
145
|
+
await usingAsync(new Injector(), async (i) => {
|
|
146
|
+
await prepareInjector(i)
|
|
147
|
+
await setupUser(i, 'user', 'pass123')
|
|
128
148
|
const ctx = i.getInstance(HttpUserContext)
|
|
129
|
-
const store = ctx.authentication.getUserStore(i.getInstance(StoreManager))
|
|
130
|
-
const loginUser = { username: 'user', roles: [] }
|
|
131
|
-
store.add({ ...loginUser, password: ctx.authentication.hashMethod('pass123') })
|
|
132
149
|
const value = await ctx.authenticateUser('user', 'pass123')
|
|
133
|
-
expect(value).toEqual(
|
|
150
|
+
expect(value).toEqual({ username: 'user', roles: [] })
|
|
134
151
|
})
|
|
135
152
|
})
|
|
136
153
|
})
|
|
@@ -193,7 +210,7 @@ describe('HttpUserContext', () => {
|
|
|
193
210
|
ctx.authenticateRequest({
|
|
194
211
|
headers: { authorization: `Basic dGVzdHVzZXI6cGFzc3dvcmQ=` },
|
|
195
212
|
} as IncomingMessage),
|
|
196
|
-
).rejects.
|
|
213
|
+
).rejects.toThrowError(UnauthenticatedError)
|
|
197
214
|
expect(ctx.authenticateUser).not.toBeCalled()
|
|
198
215
|
})
|
|
199
216
|
})
|
|
@@ -206,7 +223,7 @@ describe('HttpUserContext', () => {
|
|
|
206
223
|
ctx.authenticateRequest({
|
|
207
224
|
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
|
208
225
|
} as IncomingMessage),
|
|
209
|
-
).rejects.
|
|
226
|
+
).rejects.toThrowError(UnauthenticatedError)
|
|
210
227
|
})
|
|
211
228
|
})
|
|
212
229
|
|
|
@@ -221,19 +238,20 @@ describe('HttpUserContext', () => {
|
|
|
221
238
|
ctx.authenticateRequest({
|
|
222
239
|
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
|
223
240
|
} as IncomingMessage),
|
|
224
|
-
).rejects.
|
|
241
|
+
).rejects.toThrowError(UnauthenticatedError)
|
|
225
242
|
})
|
|
226
243
|
})
|
|
227
244
|
|
|
228
245
|
it('Should authenticate with cookie, if the session IDs matches', async () => {
|
|
229
246
|
await usingAsync(new Injector(), async (i) => {
|
|
230
247
|
await prepareInjector(i)
|
|
248
|
+
|
|
231
249
|
const ctx = i.getInstance(HttpUserContext)
|
|
232
250
|
ctx.authentication
|
|
233
251
|
.getSessionStore(i.getInstance(StoreManager))
|
|
234
252
|
.add({ sessionId: '666', username: testUser.username })
|
|
235
253
|
|
|
236
|
-
ctx.authentication.getUserStore(i.getInstance(StoreManager)).add({ ...testUser
|
|
254
|
+
ctx.authentication.getUserStore(i.getInstance(StoreManager)).add({ ...testUser })
|
|
237
255
|
|
|
238
256
|
const result = await ctx.authenticateRequest({
|
|
239
257
|
headers: { cookie: `${ctx.authentication.cookieName}=666;a=3` },
|
package/src/http-user-context.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { Injectable } from '@furystack/inject'
|
|
|
4
4
|
import { v1 } from 'uuid'
|
|
5
5
|
import { HttpAuthenticationSettings } from './http-authentication-settings'
|
|
6
6
|
import { DefaultSession } from 'models/default-session'
|
|
7
|
+
import { PasswordAuthenticator, UnauthenticatedError } from '@furystack/security'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Injectable UserContext for FuryStack HTTP Api
|
|
@@ -54,21 +55,16 @@ export class HttpUserContext {
|
|
|
54
55
|
* @returns the authenticated User
|
|
55
56
|
*/
|
|
56
57
|
public async authenticateUser(userName: string, password: string) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}))) ||
|
|
66
|
-
[]
|
|
67
|
-
if (match.length === 1) {
|
|
68
|
-
const { password: pw, ...user } = match[0]
|
|
69
|
-
return user
|
|
58
|
+
const result = await this.authenticator.checkPasswordForUser(userName, password)
|
|
59
|
+
|
|
60
|
+
if (!result.isValid) {
|
|
61
|
+
throw new UnauthenticatedError()
|
|
62
|
+
}
|
|
63
|
+
const user = await this.getUserStore().get(userName)
|
|
64
|
+
if (!user) {
|
|
65
|
+
throw new UnauthenticatedError()
|
|
70
66
|
}
|
|
71
|
-
|
|
67
|
+
return user
|
|
72
68
|
}
|
|
73
69
|
|
|
74
70
|
public async getCurrentUser(request: IncomingMessage) {
|
|
@@ -108,23 +104,16 @@ export class HttpUserContext {
|
|
|
108
104
|
// Cookie auth
|
|
109
105
|
const sessionId = this.getSessionIdFromRequest(request)
|
|
110
106
|
if (sessionId) {
|
|
111
|
-
const
|
|
107
|
+
const session = await this.getSessionStore().get(sessionId)
|
|
112
108
|
if (session) {
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
username: { $eq: session.username },
|
|
116
|
-
},
|
|
117
|
-
top: 2,
|
|
118
|
-
})
|
|
119
|
-
if (userResult.length === 1) {
|
|
120
|
-
const { password, ...user } = userResult[0]
|
|
109
|
+
const user = await this.getUserStore().get(session.username)
|
|
110
|
+
if (user) {
|
|
121
111
|
return user
|
|
122
112
|
}
|
|
123
|
-
throw Error('Inconsistent session result')
|
|
124
113
|
}
|
|
125
114
|
}
|
|
126
115
|
|
|
127
|
-
throw
|
|
116
|
+
throw new UnauthenticatedError()
|
|
128
117
|
}
|
|
129
118
|
|
|
130
119
|
/**
|
|
@@ -156,5 +145,6 @@ export class HttpUserContext {
|
|
|
156
145
|
constructor(
|
|
157
146
|
public readonly authentication: HttpAuthenticationSettings<User, DefaultSession>,
|
|
158
147
|
private readonly storeManager: StoreManager,
|
|
148
|
+
private readonly authenticator: PasswordAuthenticator,
|
|
159
149
|
) {}
|
|
160
150
|
}
|
|
@@ -11,7 +11,7 @@ import { ValidationApi } from 'validate.integration.schema'
|
|
|
11
11
|
|
|
12
12
|
// To recreate: yarn ts-json-schema-generator -f tsconfig.json --no-type-check -p packages/rest-service/src/validate.integration.schema.ts -o packages/rest-service/src/validate.integration.spec.schema.json
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const createValidateApi = async () => {
|
|
15
15
|
const injector = new Injector()
|
|
16
16
|
const port = Math.round(Math.random() * 1000) + 10000
|
|
17
17
|
injector.useRestService<ValidationApi>({
|