@furystack/rest-service 5.0.1 → 6.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/dist/actions/get-current-user.js +2 -1
- package/dist/actions/get-current-user.js.map +1 -1
- package/dist/actions/is-authenticated.js +3 -2
- package/dist/actions/is-authenticated.js.map +1 -1
- package/dist/actions/login.js +1 -1
- package/dist/actions/login.js.map +1 -1
- package/dist/authenticate.js +4 -4
- package/dist/authenticate.js.map +1 -1
- package/dist/authorize.js +2 -1
- package/dist/authorize.js.map +1 -1
- package/dist/endpoint-generators/create-delete-endpoint.js +2 -1
- package/dist/endpoint-generators/create-delete-endpoint.js.map +1 -1
- package/dist/endpoint-generators/create-delete-endpoint.spec.js +7 -4
- package/dist/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-get-collection-endpoint.js +2 -1
- package/dist/endpoint-generators/create-get-collection-endpoint.js.map +1 -1
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js +23 -16
- package/dist/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-get-entity-endpoint.js +2 -1
- package/dist/endpoint-generators/create-get-entity-endpoint.js.map +1 -1
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js +10 -5
- package/dist/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-patch-endpoint.js +2 -1
- package/dist/endpoint-generators/create-patch-endpoint.js.map +1 -1
- package/dist/endpoint-generators/create-patch-endpoint.spec.js +7 -4
- package/dist/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/create-post-endpoint.js +2 -1
- package/dist/endpoint-generators/create-post-endpoint.js.map +1 -1
- package/dist/endpoint-generators/create-post-endpoint.spec.js +5 -2
- package/dist/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
- package/dist/endpoint-generators/utils.js +5 -4
- package/dist/endpoint-generators/utils.js.map +1 -1
- package/dist/helpers.js +25 -0
- package/dist/helpers.js.map +1 -0
- package/dist/helpers.spec.js +19 -0
- package/dist/helpers.spec.js.map +1 -0
- package/dist/http-user-context.js +11 -3
- package/dist/http-user-context.js.map +1 -1
- package/dist/http-user-context.spec.js +4 -6
- package/dist/http-user-context.spec.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/rest-service.integration.spec.js +33 -32
- package/dist/rest-service.integration.spec.js.map +1 -1
- package/dist/rest.integration.test.js +7 -6
- package/dist/rest.integration.test.js.map +1 -1
- package/dist/validate.integration.spec.js +4 -2
- package/dist/validate.integration.spec.js.map +1 -1
- package/package.json +13 -14
- package/src/actions/get-current-user.ts +2 -2
- package/src/actions/is-authenticated.ts +3 -2
- package/src/actions/login.ts +1 -1
- package/src/authenticate.ts +4 -4
- package/src/authorize.ts +2 -1
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +7 -4
- package/src/endpoint-generators/create-delete-endpoint.ts +2 -1
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +23 -16
- package/src/endpoint-generators/create-get-collection-endpoint.ts +2 -1
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +10 -5
- package/src/endpoint-generators/create-get-entity-endpoint.ts +2 -1
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +7 -4
- package/src/endpoint-generators/create-patch-endpoint.ts +2 -1
- package/src/endpoint-generators/create-post-endpoint.spec.ts +5 -2
- package/src/endpoint-generators/create-post-endpoint.ts +2 -1
- package/src/endpoint-generators/utils.ts +23 -23
- package/src/helpers.spec.ts +24 -0
- package/src/helpers.ts +28 -0
- package/src/http-user-context.spec.ts +6 -10
- package/src/http-user-context.ts +13 -4
- package/src/index.ts +1 -1
- package/src/rest-service.integration.spec.ts +36 -36
- package/src/rest.integration.test.ts +7 -6
- package/src/validate.integration.spec.ts +5 -3
- package/{dist → types}/actions/error-action.d.ts +0 -0
- package/{dist → types}/actions/error-action.d.ts.map +0 -0
- package/{dist → types}/actions/error-action.spec.d.ts +0 -0
- package/{dist → types}/actions/error-action.spec.d.ts.map +0 -0
- package/{dist → types}/actions/get-current-user.d.ts +0 -0
- package/types/actions/get-current-user.d.ts.map +1 -0
- package/{dist → types}/actions/get-current-user.spec.d.ts +0 -0
- package/{dist → types}/actions/get-current-user.spec.d.ts.map +0 -0
- package/{dist → types}/actions/index.d.ts +0 -0
- package/{dist → types}/actions/index.d.ts.map +0 -0
- package/{dist → types}/actions/is-authenticated.d.ts +0 -0
- package/{dist → types}/actions/is-authenticated.d.ts.map +1 -1
- package/{dist → types}/actions/is-authenticated.spec.d.ts +0 -0
- package/{dist → types}/actions/is-authenticated.spec.d.ts.map +0 -0
- package/{dist → types}/actions/login-action.spec.d.ts +0 -0
- package/{dist → types}/actions/login-action.spec.d.ts.map +0 -0
- package/{dist → types}/actions/login.d.ts +1 -1
- package/types/actions/login.d.ts.map +1 -0
- package/{dist → types}/actions/logout-action.spec.d.ts +0 -0
- package/{dist → types}/actions/logout-action.spec.d.ts.map +0 -0
- package/{dist → types}/actions/logout.d.ts +0 -0
- package/{dist → types}/actions/logout.d.ts.map +0 -0
- package/{dist → types}/actions/not-found-action.d.ts +0 -0
- package/{dist → types}/actions/not-found-action.d.ts.map +0 -0
- package/{dist → types}/actions/not-found-action.spec.d.ts +0 -0
- package/{dist → types}/actions/not-found-action.spec.d.ts.map +0 -0
- package/{dist → types}/add-cors-header.spec.d.ts +0 -0
- package/{dist → types}/add-cors-header.spec.d.ts.map +0 -0
- package/{dist → types}/api-manager.d.ts +0 -0
- package/{dist → types}/api-manager.d.ts.map +0 -0
- package/{dist → types}/authenticate.d.ts +0 -0
- package/{dist → types}/authenticate.d.ts.map +1 -1
- package/{dist → types}/authenticate.spec.d.ts +0 -0
- package/{dist → types}/authenticate.spec.d.ts.map +0 -0
- package/{dist → types}/authorize.d.ts +0 -0
- package/{dist → types}/authorize.d.ts.map +1 -1
- package/{dist → types}/authorize.spec.d.ts +0 -0
- package/{dist → types}/authorize.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/create-delete-endpoint.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-delete-endpoint.d.ts.map +1 -1
- package/{dist → types}/endpoint-generators/create-delete-endpoint.spec.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-delete-endpoint.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/create-get-collection-endpoint.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-get-collection-endpoint.d.ts.map +1 -1
- package/{dist → types}/endpoint-generators/create-get-collection-endpoint.spec.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-get-collection-endpoint.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/create-get-entity-endpoint.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-get-entity-endpoint.d.ts.map +1 -1
- package/{dist → types}/endpoint-generators/create-get-entity-endpoint.spec.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-get-entity-endpoint.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/create-patch-endpoint.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-patch-endpoint.d.ts.map +1 -1
- package/{dist → types}/endpoint-generators/create-patch-endpoint.spec.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-patch-endpoint.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/create-post-endpoint.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-post-endpoint.d.ts.map +1 -1
- package/{dist → types}/endpoint-generators/create-post-endpoint.spec.d.ts +0 -0
- package/{dist → types}/endpoint-generators/create-post-endpoint.spec.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/index.d.ts +0 -0
- package/{dist → types}/endpoint-generators/index.d.ts.map +0 -0
- package/{dist → types}/endpoint-generators/utils.d.ts +1 -1
- package/{dist → types}/endpoint-generators/utils.d.ts.map +1 -1
- package/types/helpers.d.ts +21 -0
- package/types/helpers.d.ts.map +1 -0
- package/types/helpers.spec.d.ts +2 -0
- package/types/helpers.spec.d.ts.map +1 -0
- package/{dist → types}/http-authentication-settings.d.ts +0 -0
- package/{dist → types}/http-authentication-settings.d.ts.map +0 -0
- package/{dist → types}/http-user-context.d.ts +2 -1
- package/types/http-user-context.d.ts.map +1 -0
- package/{dist → types}/http-user-context.spec.d.ts +0 -1
- package/{dist → types}/http-user-context.spec.d.ts.map +1 -1
- package/{dist → types}/incoming-message-extensions.d.ts +0 -0
- package/{dist → types}/incoming-message-extensions.d.ts.map +0 -0
- package/{dist → types}/incoming-message-extensions.spec.d.ts +0 -0
- package/{dist → types}/incoming-message-extensions.spec.d.ts.map +0 -0
- package/{dist → types}/index.d.ts +1 -1
- package/types/index.d.ts.map +1 -0
- package/{dist → types}/models/cors-options.d.ts +0 -0
- package/{dist → types}/models/cors-options.d.ts.map +0 -0
- package/{dist → types}/models/default-session.d.ts +0 -0
- package/{dist → types}/models/default-session.d.ts.map +0 -0
- package/{dist → types}/models/index.d.ts +0 -0
- package/{dist → types}/models/index.d.ts.map +0 -0
- package/{dist → types}/request-action-implementation.d.ts +0 -0
- package/{dist → types}/request-action-implementation.d.ts.map +0 -0
- package/types/rest-service.integration.spec.d.ts +2 -0
- package/{dist → types}/rest-service.integration.spec.d.ts.map +1 -1
- package/{dist → types}/rest.integration.test.d.ts +1 -1
- package/types/rest.integration.test.d.ts.map +1 -0
- package/{dist → types}/schema-validator/index.d.ts +0 -0
- package/{dist → types}/schema-validator/index.d.ts.map +0 -0
- package/{dist → types}/schema-validator/schema-validation-error.d.ts +0 -0
- package/{dist → types}/schema-validator/schema-validation-error.d.ts.map +0 -0
- package/{dist → types}/schema-validator/schema-validator.d.ts +0 -0
- package/{dist → types}/schema-validator/schema-validator.d.ts.map +0 -0
- package/{dist → types}/schema-validator/schema-validator.test.d.ts +0 -0
- package/{dist → types}/schema-validator/schema-validator.test.d.ts.map +0 -0
- package/{dist → types}/schema-validator/validate-examples.d.ts +0 -0
- package/{dist → types}/schema-validator/validate-examples.d.ts.map +0 -0
- package/{dist → types}/server-manager.d.ts +0 -0
- package/{dist → types}/server-manager.d.ts.map +0 -0
- package/{dist → types}/server-response-extensions.d.ts +0 -0
- package/{dist → types}/server-response-extensions.d.ts.map +0 -0
- package/{dist → types}/server-response-extensions.spec.d.ts +0 -0
- package/{dist → types}/server-response-extensions.spec.d.ts.map +0 -0
- package/{dist → types}/utils.d.ts +0 -0
- package/{dist → types}/utils.d.ts.map +0 -0
- package/{dist → types}/validate.d.ts +0 -0
- package/{dist → types}/validate.d.ts.map +0 -0
- package/{dist → types}/validate.integration.schema.d.ts +0 -0
- package/{dist → types}/validate.integration.schema.d.ts.map +0 -0
- package/types/validate.integration.spec.d.ts +2 -0
- package/{dist → types}/validate.integration.spec.d.ts.map +1 -1
- package/dist/actions/get-current-user.d.ts.map +0 -1
- package/dist/actions/login.d.ts.map +0 -1
- package/dist/http-user-context.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/injector-extensions.d.ts +0 -21
- package/dist/injector-extensions.d.ts.map +0 -1
- package/dist/injector-extensions.js +0 -14
- package/dist/injector-extensions.js.map +0 -1
- package/dist/injector-extensions.spec.d.ts +0 -2
- package/dist/injector-extensions.spec.d.ts.map +0 -1
- package/dist/injector-extensions.spec.js +0 -19
- package/dist/injector-extensions.spec.js.map +0 -1
- package/dist/rest-service.integration.spec.d.ts +0 -2
- package/dist/rest.integration.test.d.ts.map +0 -1
- package/dist/validate.integration.spec.d.ts +0 -2
- package/src/injector-extensions.spec.ts +0 -19
- package/src/injector-extensions.ts +0 -35
package/src/authenticate.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAuthenticated } from '@furystack/core'
|
|
1
2
|
import { sleepAsync } from '@furystack/utils'
|
|
2
3
|
import { HttpUserContext } from './http-user-context'
|
|
3
4
|
import { ActionResult, JsonResult, RequestAction, RequestActionOptions } from './request-action-implementation'
|
|
@@ -6,15 +7,14 @@ export const Authenticate =
|
|
|
6
7
|
() =>
|
|
7
8
|
<T extends { result: unknown }>(action: RequestAction<T>): RequestAction<T> => {
|
|
8
9
|
return async (args: RequestActionOptions<T>): Promise<ActionResult<T>> => {
|
|
9
|
-
const
|
|
10
|
+
const { injector } = args
|
|
11
|
+
const authenticated = await isAuthenticated(injector)
|
|
10
12
|
if (!authenticated) {
|
|
11
13
|
await sleepAsync(Math.random() * 1000)
|
|
12
14
|
return JsonResult(
|
|
13
15
|
{ error: 'unauthorized' },
|
|
14
16
|
401,
|
|
15
|
-
|
|
16
|
-
? { 'WWW-Authenticate': 'Basic' }
|
|
17
|
-
: {},
|
|
17
|
+
injector.getInstance(HttpUserContext).authentication.enableBasicAuth ? { 'WWW-Authenticate': 'Basic' } : {},
|
|
18
18
|
) as unknown as ActionResult<T>
|
|
19
19
|
}
|
|
20
20
|
return (await action(args)) as any
|
package/src/authorize.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isAuthorized } from '@furystack/core'
|
|
1
2
|
import { sleepAsync } from '@furystack/utils'
|
|
2
3
|
import { ActionResult, JsonResult, RequestAction, RequestActionOptions } from './request-action-implementation'
|
|
3
4
|
|
|
@@ -6,7 +7,7 @@ export const Authorize =
|
|
|
6
7
|
<T extends { result: unknown }>(action: RequestAction<T>): RequestAction<T> => {
|
|
7
8
|
return async (options: RequestActionOptions<T>): Promise<ActionResult<T>> => {
|
|
8
9
|
try {
|
|
9
|
-
const authorized = await options.injector
|
|
10
|
+
const authorized = await isAuthorized(options.injector, ...roles)
|
|
10
11
|
if (!authorized) {
|
|
11
12
|
await sleepAsync(Math.random() * 1000)
|
|
12
13
|
return JsonResult({ error: 'forbidden' }, 403) as any
|
|
@@ -4,12 +4,15 @@ import { DeleteEndpoint } from '@furystack/rest'
|
|
|
4
4
|
import { createDeleteEndpoint } from './create-delete-endpoint'
|
|
5
5
|
import got from 'got'
|
|
6
6
|
import { MockClass, setupContext } from './utils'
|
|
7
|
+
import { useRestService } from '../helpers'
|
|
8
|
+
import { getDataSetFor } from '@furystack/repository'
|
|
7
9
|
|
|
8
10
|
describe('createDeleteEndpoint', () => {
|
|
9
11
|
it('Should delete the entity and report the success', async () => {
|
|
10
12
|
await usingAsync(new Injector(), async (i) => {
|
|
11
13
|
setupContext(i)
|
|
12
|
-
await
|
|
14
|
+
await useRestService<{ DELETE: { '/:id': DeleteEndpoint<MockClass, 'id'> } }>({
|
|
15
|
+
injector: i,
|
|
13
16
|
root: '/api',
|
|
14
17
|
port: 1111,
|
|
15
18
|
api: {
|
|
@@ -18,16 +21,16 @@ describe('createDeleteEndpoint', () => {
|
|
|
18
21
|
},
|
|
19
22
|
},
|
|
20
23
|
})
|
|
21
|
-
await
|
|
24
|
+
await getDataSetFor(i, MockClass, 'id').add(i, { id: 'mock', value: 'mock' })
|
|
22
25
|
|
|
23
|
-
const countBeforeDelete = await
|
|
26
|
+
const countBeforeDelete = await getDataSetFor(i, MockClass, 'id').count(i)
|
|
24
27
|
expect(countBeforeDelete).toBe(1)
|
|
25
28
|
|
|
26
29
|
const response = await got('http://127.0.0.1:1111/api/mock', { method: 'DELETE' })
|
|
27
30
|
expect(response.statusCode).toBe(204)
|
|
28
31
|
expect(response.body).toBe('')
|
|
29
32
|
|
|
30
|
-
const countAfterDelete = await
|
|
33
|
+
const countAfterDelete = await getDataSetFor(i, MockClass, 'id').count(i)
|
|
31
34
|
expect(countAfterDelete).toBe(0)
|
|
32
35
|
})
|
|
33
36
|
})
|
|
@@ -2,6 +2,7 @@ import { Constructable } from '@furystack/inject'
|
|
|
2
2
|
import { DeleteEndpoint } from '@furystack/rest'
|
|
3
3
|
import '@furystack/repository'
|
|
4
4
|
import { JsonResult, RequestAction } from '../request-action-implementation'
|
|
5
|
+
import { getRepository } from '@furystack/repository'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Creates a DELETE endpoint for removing entities
|
|
@@ -17,7 +18,7 @@ export const createDeleteEndpoint = <T extends object, TPrimaryKey extends keyof
|
|
|
17
18
|
}) => {
|
|
18
19
|
const endpoint: RequestAction<DeleteEndpoint<T, TPrimaryKey>> = async ({ injector, getUrlParams }) => {
|
|
19
20
|
const { id } = getUrlParams()
|
|
20
|
-
const dataSet = injector.getDataSetFor(options.model, options.primaryKey)
|
|
21
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey)
|
|
21
22
|
await dataSet.remove(injector, id)
|
|
22
23
|
return JsonResult({}, 204)
|
|
23
24
|
}
|
|
@@ -5,9 +5,11 @@ import { MockClass, setupContext } from './utils'
|
|
|
5
5
|
import { createGetCollectionEndpoint } from './create-get-collection-endpoint'
|
|
6
6
|
import { GetCollectionEndpoint, GetCollectionResult, serializeToQueryString } from '@furystack/rest'
|
|
7
7
|
import { FindOptions } from '@furystack/core'
|
|
8
|
+
import { getDataSetFor, getRepository } from '@furystack/repository'
|
|
9
|
+
import { useRestService } from '../helpers'
|
|
8
10
|
|
|
9
11
|
const addMockEntities = async (i: Injector) =>
|
|
10
|
-
await i
|
|
12
|
+
await getRepository(i)
|
|
11
13
|
.getDataSetFor(MockClass, 'id')
|
|
12
14
|
.add(
|
|
13
15
|
i,
|
|
@@ -21,7 +23,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
21
23
|
it('Should return the collection without filter / order', async () => {
|
|
22
24
|
await usingAsync(new Injector(), async (i) => {
|
|
23
25
|
setupContext(i)
|
|
24
|
-
await
|
|
26
|
+
await useRestService<{ GET: { '/entities': GetCollectionEndpoint<MockClass> } }>({
|
|
27
|
+
injector: i,
|
|
25
28
|
root: '/api',
|
|
26
29
|
port: 1112,
|
|
27
30
|
api: {
|
|
@@ -32,8 +35,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
32
35
|
})
|
|
33
36
|
await addMockEntities(i)
|
|
34
37
|
|
|
35
|
-
const count = await
|
|
36
|
-
const allEntities = await
|
|
38
|
+
const count = await getDataSetFor(i, MockClass, 'id').count(i)
|
|
39
|
+
const allEntities = await getDataSetFor(i, MockClass, 'id').find(i, {})
|
|
37
40
|
|
|
38
41
|
const response = await got('http://127.0.0.1:1112/api/entities', { method: 'GET' })
|
|
39
42
|
const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
|
|
@@ -45,7 +48,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
45
48
|
it('Should return entities in order', async () => {
|
|
46
49
|
await usingAsync(new Injector(), async (i) => {
|
|
47
50
|
setupContext(i)
|
|
48
|
-
await
|
|
51
|
+
await useRestService<{ GET: { '/entities': GetCollectionEndpoint<MockClass> } }>({
|
|
52
|
+
injector: i,
|
|
49
53
|
root: '/api',
|
|
50
54
|
port: 1113,
|
|
51
55
|
api: {
|
|
@@ -56,8 +60,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
56
60
|
})
|
|
57
61
|
await addMockEntities(i)
|
|
58
62
|
const findOptions: FindOptions<MockClass, Array<keyof MockClass>> = { order: { value: 'ASC' } }
|
|
59
|
-
const count = await
|
|
60
|
-
const orderedEntities = await
|
|
63
|
+
const count = await getDataSetFor(i, MockClass, 'id').count(i, findOptions.filter)
|
|
64
|
+
const orderedEntities = await getDataSetFor(i, MockClass, 'id').find(i, findOptions)
|
|
61
65
|
const response = await got(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
|
|
62
66
|
method: 'GET',
|
|
63
67
|
})
|
|
@@ -70,7 +74,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
70
74
|
it('Should return entities with filtering', async () => {
|
|
71
75
|
await usingAsync(new Injector(), async (i) => {
|
|
72
76
|
setupContext(i)
|
|
73
|
-
await
|
|
77
|
+
await useRestService<{ GET: { '/entities': GetCollectionEndpoint<MockClass> } }>({
|
|
78
|
+
injector: i,
|
|
74
79
|
root: '/api',
|
|
75
80
|
port: 1113,
|
|
76
81
|
api: {
|
|
@@ -84,8 +89,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
84
89
|
filter: { id: { $ne: 'mock2' } },
|
|
85
90
|
}
|
|
86
91
|
|
|
87
|
-
const count = await
|
|
88
|
-
const filteredEntities = await
|
|
92
|
+
const count = await getDataSetFor(i, MockClass, 'id').count(i, findOptions.filter)
|
|
93
|
+
const filteredEntities = await getDataSetFor(i, MockClass, 'id').find(i, findOptions)
|
|
89
94
|
|
|
90
95
|
expect(filteredEntities).not.toContainEqual({ id: 'mock2', value: '3' })
|
|
91
96
|
|
|
@@ -101,7 +106,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
101
106
|
it('Should return entities with selecting specific fields', async () => {
|
|
102
107
|
await usingAsync(new Injector(), async (i) => {
|
|
103
108
|
setupContext(i)
|
|
104
|
-
await
|
|
109
|
+
await useRestService<{ GET: { '/entities': GetCollectionEndpoint<MockClass> } }>({
|
|
110
|
+
injector: i,
|
|
105
111
|
root: '/api',
|
|
106
112
|
port: 1113,
|
|
107
113
|
api: {
|
|
@@ -115,8 +121,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
115
121
|
select: ['id'],
|
|
116
122
|
}
|
|
117
123
|
|
|
118
|
-
const count = await
|
|
119
|
-
const selectedEntities = await
|
|
124
|
+
const count = await getDataSetFor(i, MockClass, 'id').count(i, findOptions.filter)
|
|
125
|
+
const selectedEntities = await getDataSetFor(i, MockClass, 'id').find(i, findOptions)
|
|
120
126
|
|
|
121
127
|
selectedEntities.forEach((e) => expect(e.value).toBeUndefined())
|
|
122
128
|
|
|
@@ -132,7 +138,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
132
138
|
it('Should return entities with top/skip', async () => {
|
|
133
139
|
await usingAsync(new Injector(), async (i) => {
|
|
134
140
|
setupContext(i)
|
|
135
|
-
await
|
|
141
|
+
await useRestService<{ GET: { '/entities': GetCollectionEndpoint<MockClass> } }>({
|
|
142
|
+
injector: i,
|
|
136
143
|
root: '/api',
|
|
137
144
|
port: 1113,
|
|
138
145
|
api: {
|
|
@@ -147,8 +154,8 @@ describe('createGetCollectionEndpoint', () => {
|
|
|
147
154
|
top: 2,
|
|
148
155
|
}
|
|
149
156
|
|
|
150
|
-
const count = await
|
|
151
|
-
const topSkipEntities = await
|
|
157
|
+
const count = await getDataSetFor(i, MockClass, 'id').count(i, findOptions.filter)
|
|
158
|
+
const topSkipEntities = await getDataSetFor(i, MockClass, 'id').find(i, findOptions)
|
|
152
159
|
|
|
153
160
|
expect(topSkipEntities).not.toContainEqual({ id: 'mock1', value: '4' })
|
|
154
161
|
expect(topSkipEntities).not.toContainEqual({ id: 'mock4', value: '1' })
|
|
@@ -2,6 +2,7 @@ import { Constructable } from '@furystack/inject'
|
|
|
2
2
|
import { GetCollectionEndpoint } from '@furystack/rest'
|
|
3
3
|
import '@furystack/repository'
|
|
4
4
|
import { JsonResult, RequestAction } from '../request-action-implementation'
|
|
5
|
+
import { getRepository } from '@furystack/repository'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Creates a GetCollection endpoint for the given model. The model should have a Repository DataSet
|
|
@@ -17,7 +18,7 @@ export const createGetCollectionEndpoint = <T, TPrimaryKey extends keyof T>(opti
|
|
|
17
18
|
}) => {
|
|
18
19
|
const endpoint: RequestAction<GetCollectionEndpoint<T>> = async ({ injector, getQuery }) => {
|
|
19
20
|
const { findOptions } = getQuery()
|
|
20
|
-
const dataSet = injector.getDataSetFor(options.model, options.primaryKey)
|
|
21
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey)
|
|
21
22
|
const entriesPromise = dataSet.find<any>(injector, findOptions || {})
|
|
22
23
|
const countPromise = dataSet.count(injector, findOptions?.filter)
|
|
23
24
|
const [entries, count] = await Promise.all([entriesPromise, countPromise])
|
|
@@ -4,12 +4,15 @@ import { GetEntityEndpoint, serializeToQueryString } from '@furystack/rest'
|
|
|
4
4
|
import got, { HTTPError } from 'got'
|
|
5
5
|
import { MockClass, setupContext } from './utils'
|
|
6
6
|
import { createGetEntityEndpoint } from './create-get-entity-endpoint'
|
|
7
|
+
import { getDataSetFor } from '@furystack/repository'
|
|
8
|
+
import { useRestService } from '../helpers'
|
|
7
9
|
|
|
8
10
|
describe('createGetEntityEndpoint', () => {
|
|
9
11
|
it('Should return the entity', async () => {
|
|
10
12
|
await usingAsync(new Injector(), async (i) => {
|
|
11
13
|
setupContext(i)
|
|
12
|
-
await
|
|
14
|
+
await useRestService<{ GET: { '/:id': GetEntityEndpoint<MockClass, 'id'> } }>({
|
|
15
|
+
injector: i,
|
|
13
16
|
root: '/api',
|
|
14
17
|
port: 1113,
|
|
15
18
|
api: {
|
|
@@ -19,7 +22,7 @@ describe('createGetEntityEndpoint', () => {
|
|
|
19
22
|
},
|
|
20
23
|
})
|
|
21
24
|
const mockEntity: MockClass = { id: 'mock', value: 'mock' }
|
|
22
|
-
await
|
|
25
|
+
await getDataSetFor(i, MockClass, 'id').add(i, mockEntity)
|
|
23
26
|
|
|
24
27
|
const response = await got('http://127.0.0.1:1113/api/mock', { method: 'GET' })
|
|
25
28
|
expect(JSON.parse(response.body)).toStrictEqual(mockEntity)
|
|
@@ -29,7 +32,8 @@ describe('createGetEntityEndpoint', () => {
|
|
|
29
32
|
it('Should return the entity with the selected fields', async () => {
|
|
30
33
|
await usingAsync(new Injector(), async (i) => {
|
|
31
34
|
setupContext(i)
|
|
32
|
-
await
|
|
35
|
+
await useRestService<{ GET: { '/:id': GetEntityEndpoint<MockClass, 'id'> } }>({
|
|
36
|
+
injector: i,
|
|
33
37
|
root: '/api',
|
|
34
38
|
port: 1114,
|
|
35
39
|
api: {
|
|
@@ -39,7 +43,7 @@ describe('createGetEntityEndpoint', () => {
|
|
|
39
43
|
},
|
|
40
44
|
})
|
|
41
45
|
const mockEntity: MockClass = { id: 'mock', value: 'mock' }
|
|
42
|
-
await
|
|
46
|
+
await getDataSetFor(i, MockClass, 'id').add(i, mockEntity)
|
|
43
47
|
|
|
44
48
|
const response = await got(`http://127.0.0.1:1114/api/mock?${serializeToQueryString({ select: ['id'] })}`, {
|
|
45
49
|
method: 'GET',
|
|
@@ -51,7 +55,8 @@ describe('createGetEntityEndpoint', () => {
|
|
|
51
55
|
it('Should return 404 if no entity has been found', async () => {
|
|
52
56
|
await usingAsync(new Injector(), async (i) => {
|
|
53
57
|
setupContext(i)
|
|
54
|
-
await
|
|
58
|
+
await useRestService<{ GET: { '/:id': GetEntityEndpoint<MockClass, 'id'> } }>({
|
|
59
|
+
injector: i,
|
|
55
60
|
root: '/api',
|
|
56
61
|
port: 1115,
|
|
57
62
|
api: {
|
|
@@ -2,6 +2,7 @@ import { Constructable } from '@furystack/inject'
|
|
|
2
2
|
import { RequestError, GetEntityEndpoint } from '@furystack/rest'
|
|
3
3
|
import '@furystack/repository'
|
|
4
4
|
import { JsonResult, RequestAction } from '../request-action-implementation'
|
|
5
|
+
import { getRepository } from '@furystack/repository'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Creates a simple Get Entity endpoint for a specified model.
|
|
@@ -18,7 +19,7 @@ export const createGetEntityEndpoint = <T extends object, TPrimaryKey extends ke
|
|
|
18
19
|
const endpoint: RequestAction<GetEntityEndpoint<T, TPrimaryKey>> = async ({ injector, getUrlParams, getQuery }) => {
|
|
19
20
|
const { id } = getUrlParams()
|
|
20
21
|
const { select } = getQuery()
|
|
21
|
-
const dataSet = injector.getDataSetFor(options.model, options.primaryKey)
|
|
22
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey)
|
|
22
23
|
const entry = await dataSet.get(injector, id, select)
|
|
23
24
|
if (!entry) {
|
|
24
25
|
throw new RequestError('Entity not found', 404)
|
|
@@ -4,12 +4,15 @@ import { PatchEndpoint } from '@furystack/rest'
|
|
|
4
4
|
import { createPatchEndpoint } from './create-patch-endpoint'
|
|
5
5
|
import got from 'got'
|
|
6
6
|
import { MockClass, setupContext } from './utils'
|
|
7
|
+
import { getDataSetFor } from '@furystack/repository'
|
|
8
|
+
import { useRestService } from '../helpers'
|
|
7
9
|
|
|
8
10
|
describe('createPatchEndpoint', () => {
|
|
9
11
|
it('Should update the entity and report the success', async () => {
|
|
10
12
|
await usingAsync(new Injector(), async (i) => {
|
|
11
13
|
setupContext(i)
|
|
12
|
-
await
|
|
14
|
+
await useRestService<{ PATCH: { '/:id': PatchEndpoint<MockClass, 'id'> } }>({
|
|
15
|
+
injector: i,
|
|
13
16
|
root: '/api',
|
|
14
17
|
port: 1116,
|
|
15
18
|
api: {
|
|
@@ -18,9 +21,9 @@ describe('createPatchEndpoint', () => {
|
|
|
18
21
|
},
|
|
19
22
|
},
|
|
20
23
|
})
|
|
21
|
-
await
|
|
24
|
+
await getDataSetFor(i, MockClass, 'id').add(i, { id: 'mock', value: 'mock' })
|
|
22
25
|
|
|
23
|
-
const countBeforeDelete = await
|
|
26
|
+
const countBeforeDelete = await getDataSetFor(i, MockClass, 'id').count(i)
|
|
24
27
|
expect(countBeforeDelete).toBe(1)
|
|
25
28
|
|
|
26
29
|
const response = await got('http://127.0.0.1:1116/api/mock', {
|
|
@@ -29,7 +32,7 @@ describe('createPatchEndpoint', () => {
|
|
|
29
32
|
})
|
|
30
33
|
expect(response.statusCode).toBe(200)
|
|
31
34
|
expect(response.body).toBe('{}')
|
|
32
|
-
const updated = await
|
|
35
|
+
const updated = await getDataSetFor(i, MockClass, 'id').get(i, 'mock')
|
|
33
36
|
expect(updated?.value).toBe('updated')
|
|
34
37
|
})
|
|
35
38
|
})
|
|
@@ -3,6 +3,7 @@ import { PatchEndpoint } from '@furystack/rest'
|
|
|
3
3
|
import '@furystack/repository'
|
|
4
4
|
import '../incoming-message-extensions'
|
|
5
5
|
import { JsonResult, RequestAction } from '../request-action-implementation'
|
|
6
|
+
import { getRepository } from '@furystack/repository'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Creates a PATCH endpoint for updating entities
|
|
@@ -19,7 +20,7 @@ export const createPatchEndpoint = <T extends object, TPrimaryKey extends keyof
|
|
|
19
20
|
const endpoint: RequestAction<PatchEndpoint<T, TPrimaryKey>> = async ({ injector, request, getUrlParams }) => {
|
|
20
21
|
const { id } = getUrlParams()
|
|
21
22
|
const patchData = await request.readPostBody<T>()
|
|
22
|
-
const dataSet = injector.getDataSetFor(options.model, options.primaryKey)
|
|
23
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey)
|
|
23
24
|
await dataSet.update(injector, id, patchData)
|
|
24
25
|
return JsonResult({})
|
|
25
26
|
}
|
|
@@ -4,12 +4,15 @@ import { PostEndpoint } from '@furystack/rest'
|
|
|
4
4
|
import { createPostEndpoint } from './create-post-endpoint'
|
|
5
5
|
import got from 'got'
|
|
6
6
|
import { MockClass, setupContext } from './utils'
|
|
7
|
+
import { useRestService } from '../helpers'
|
|
8
|
+
import { getDataSetFor } from '@furystack/repository'
|
|
7
9
|
|
|
8
10
|
describe('createPostEndpoint', () => {
|
|
9
11
|
it('Should create the entity and report the success', async () => {
|
|
10
12
|
await usingAsync(new Injector(), async (i) => {
|
|
11
13
|
setupContext(i)
|
|
12
|
-
await
|
|
14
|
+
await useRestService<{ POST: { '/:id': PostEndpoint<MockClass, 'id'> } }>({
|
|
15
|
+
injector: i,
|
|
13
16
|
root: '/api',
|
|
14
17
|
port: 1117,
|
|
15
18
|
api: {
|
|
@@ -25,7 +28,7 @@ describe('createPostEndpoint', () => {
|
|
|
25
28
|
})
|
|
26
29
|
expect(response.statusCode).toBe(201)
|
|
27
30
|
expect(JSON.parse(response.body)).toStrictEqual(entityToPost)
|
|
28
|
-
const posted = await
|
|
31
|
+
const posted = await getDataSetFor(i, MockClass, 'id').get(i, entityToPost.id)
|
|
29
32
|
expect(posted?.value).toBe('posted')
|
|
30
33
|
})
|
|
31
34
|
})
|
|
@@ -4,6 +4,7 @@ import '@furystack/repository'
|
|
|
4
4
|
import '../incoming-message-extensions'
|
|
5
5
|
import { JsonResult, RequestAction } from '../request-action-implementation'
|
|
6
6
|
import { WithOptionalId } from '@furystack/core'
|
|
7
|
+
import { getRepository } from '@furystack/repository'
|
|
7
8
|
/**
|
|
8
9
|
* Creates a POST endpoint for updating entities
|
|
9
10
|
*
|
|
@@ -17,7 +18,7 @@ export const createPostEndpoint = <T extends object, TPrimaryKey extends keyof T
|
|
|
17
18
|
primaryKey: TPrimaryKey
|
|
18
19
|
}) => {
|
|
19
20
|
const endpoint: RequestAction<PostEndpoint<T, TPrimaryKey>> = async ({ injector, request }) => {
|
|
20
|
-
const dataSet = injector.getDataSetFor(options.model, options.primaryKey)
|
|
21
|
+
const dataSet = getRepository(injector).getDataSetFor(options.model, options.primaryKey)
|
|
21
22
|
|
|
22
23
|
const entityToCreate = await request.readPostBody<WithOptionalId<T, typeof dataSet['primaryKey']>>()
|
|
23
24
|
const { created } = await dataSet.add(injector, entityToCreate)
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Injector } from '@furystack/inject'
|
|
2
|
-
import { InMemoryStore, User } from '@furystack/core'
|
|
2
|
+
import { addStore, InMemoryStore, User } from '@furystack/core'
|
|
3
3
|
import { DefaultSession } from '../models/default-session'
|
|
4
4
|
import '@furystack/repository'
|
|
5
|
-
import '../
|
|
5
|
+
import '../helpers'
|
|
6
|
+
import { getRepository } from '@furystack/repository'
|
|
6
7
|
|
|
7
8
|
export class MockClass {
|
|
8
9
|
id!: string
|
|
@@ -10,25 +11,24 @@ export class MockClass {
|
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export const setupContext = (i: Injector) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
).setupRepository((r) => r.createDataSet(MockClass, 'id'))
|
|
14
|
+
addStore(
|
|
15
|
+
i,
|
|
16
|
+
new InMemoryStore({
|
|
17
|
+
model: MockClass,
|
|
18
|
+
primaryKey: 'id',
|
|
19
|
+
}),
|
|
20
|
+
)
|
|
21
|
+
.addStore(
|
|
22
|
+
new InMemoryStore({
|
|
23
|
+
model: User,
|
|
24
|
+
primaryKey: 'username',
|
|
25
|
+
}),
|
|
26
|
+
)
|
|
27
|
+
.addStore(
|
|
28
|
+
new InMemoryStore({
|
|
29
|
+
model: DefaultSession,
|
|
30
|
+
primaryKey: 'sessionId',
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
getRepository(i).createDataSet(MockClass, 'id')
|
|
34
34
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Injector } from '@furystack/inject'
|
|
2
|
+
import { usingAsync } from '@furystack/utils'
|
|
3
|
+
import { useHttpAuthentication } from './helpers'
|
|
4
|
+
|
|
5
|
+
describe('Injector extensions', () => {
|
|
6
|
+
describe('useHttpAuthentication', () => {
|
|
7
|
+
it('Should set up HTTP Authentication', async () => {
|
|
8
|
+
await usingAsync(new Injector(), async (i) => {
|
|
9
|
+
useHttpAuthentication(i)
|
|
10
|
+
// TODO: Check if HTTP Auth has been set up
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe('useRestService()', () => {
|
|
16
|
+
it.todo(
|
|
17
|
+
'Should set up a REST service',
|
|
18
|
+
// await usingAsync(new Injector(), async (i) => {
|
|
19
|
+
// useRestService({ injector: i, api: {}, })
|
|
20
|
+
// TODO: Assert if REST service has been set up
|
|
21
|
+
// })
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
})
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { User } from '@furystack/core'
|
|
2
|
+
import { Injector } from '@furystack/inject'
|
|
3
|
+
import { HttpAuthenticationSettings } from './http-authentication-settings'
|
|
4
|
+
import { RestApi } from '@furystack/rest'
|
|
5
|
+
import { ApiManager, ImplementApiOptions } from './api-manager'
|
|
6
|
+
import { DefaultSession } from './models/default-session'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Sets up the @furystack/rest-service with the provided settings
|
|
10
|
+
*
|
|
11
|
+
* @param api The API implementation details
|
|
12
|
+
*/
|
|
13
|
+
export const useRestService = async <T extends RestApi>(api: ImplementApiOptions<T>) => {
|
|
14
|
+
await api.injector.getInstance(ApiManager).addApi({ ...api })
|
|
15
|
+
return this
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Sets up the HTTP Authentication
|
|
20
|
+
*
|
|
21
|
+
* @param injector The Injector instance
|
|
22
|
+
* @param settings Settings for HTTP Authentication
|
|
23
|
+
* @returns void
|
|
24
|
+
*/
|
|
25
|
+
export const useHttpAuthentication = <TUser extends User, TSession extends DefaultSession>(
|
|
26
|
+
injector: Injector,
|
|
27
|
+
settings?: Partial<HttpAuthenticationSettings<TUser, TSession>>,
|
|
28
|
+
) => injector.setExplicitInstance({ ...new HttpAuthenticationSettings(), ...settings }, HttpAuthenticationSettings)
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from 'http'
|
|
2
2
|
import { usingAsync } from '@furystack/utils'
|
|
3
3
|
import { Injector } from '@furystack/inject'
|
|
4
|
-
import { User, StoreManager, InMemoryStore } from '@furystack/core'
|
|
4
|
+
import { User, StoreManager, InMemoryStore, addStore } from '@furystack/core'
|
|
5
5
|
import { DefaultSession } from './models/default-session'
|
|
6
6
|
import { HttpUserContext } from './http-user-context'
|
|
7
|
-
import './injector-extensions'
|
|
8
7
|
import { PasswordAuthenticator, PasswordCredential, UnauthenticatedError } from '@furystack/security'
|
|
8
|
+
import { useHttpAuthentication } from './helpers'
|
|
9
9
|
|
|
10
10
|
export const prepareInjector = async (i: Injector) => {
|
|
11
|
-
i
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
.addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }))
|
|
15
|
-
.addStore(new InMemoryStore({ model: PasswordCredential, primaryKey: 'userName' })),
|
|
16
|
-
)
|
|
11
|
+
addStore(i, new InMemoryStore({ model: User, primaryKey: 'username' }))
|
|
12
|
+
.addStore(new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }))
|
|
13
|
+
.addStore(new InMemoryStore({ model: PasswordCredential, primaryKey: 'userName' }))
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
// await i.getInstance(ServerManager).getOrCreate({ port: 19999 })
|
|
15
|
+
useHttpAuthentication(i)
|
|
20
16
|
}
|
|
21
17
|
|
|
22
18
|
const setupUser = async (i: Injector, userName: string, password: string) => {
|
package/src/http-user-context.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from 'http'
|
|
2
2
|
import { User, StoreManager } from '@furystack/core'
|
|
3
3
|
import { Injectable } from '@furystack/inject'
|
|
4
|
-
import { v1 } from 'uuid'
|
|
5
4
|
import { HttpAuthenticationSettings } from './http-authentication-settings'
|
|
6
|
-
import { DefaultSession } from 'models/default-session'
|
|
5
|
+
import { DefaultSession } from './models/default-session'
|
|
7
6
|
import { PasswordAuthenticator, UnauthenticatedError } from '@furystack/security'
|
|
7
|
+
import { randomBytes } from 'crypto'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Injectable UserContext for FuryStack HTTP Api
|
|
@@ -24,6 +24,15 @@ export class HttpUserContext {
|
|
|
24
24
|
return users[0]
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
private getSessionById = async (sessionId: string) => {
|
|
28
|
+
const sessionStore = this.getSessionStore()
|
|
29
|
+
const sessions = await sessionStore.find({ filter: { sessionId: { $eq: sessionId } }, top: 2 })
|
|
30
|
+
if (sessions.length !== 1) {
|
|
31
|
+
throw new UnauthenticatedError()
|
|
32
|
+
}
|
|
33
|
+
return sessions[0]
|
|
34
|
+
}
|
|
35
|
+
|
|
27
36
|
private user?: User
|
|
28
37
|
|
|
29
38
|
/**
|
|
@@ -113,7 +122,7 @@ export class HttpUserContext {
|
|
|
113
122
|
// Cookie auth
|
|
114
123
|
const sessionId = this.getSessionIdFromRequest(request)
|
|
115
124
|
if (sessionId) {
|
|
116
|
-
const session = await this.
|
|
125
|
+
const session = await this.getSessionById(sessionId)
|
|
117
126
|
if (session) {
|
|
118
127
|
const user = await this.getUserByName(session.username)
|
|
119
128
|
if (user) {
|
|
@@ -133,7 +142,7 @@ export class HttpUserContext {
|
|
|
133
142
|
* @returns the current User
|
|
134
143
|
*/
|
|
135
144
|
public async cookieLogin(user: User, serverResponse: ServerResponse): Promise<User> {
|
|
136
|
-
const sessionId =
|
|
145
|
+
const sessionId = randomBytes(32).toString('hex')
|
|
137
146
|
await this.getSessionStore().add({ sessionId, username: user.username })
|
|
138
147
|
serverResponse.setHeader('Set-Cookie', `${this.authentication.cookieName}=${sessionId}; Path=/; HttpOnly`)
|
|
139
148
|
this.user = user
|
package/src/index.ts
CHANGED