@aeriajs/server 0.0.277 → 0.0.279
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/getToken.d.ts +3 -0
- package/dist/getToken.js +50 -0
- package/dist/getToken.mjs +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/init.d.ts +1 -17
- package/dist/init.js +33 -59
- package/dist/init.mjs +32 -57
- package/package.json +9 -9
package/dist/getToken.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getToken = exports.authenticationGuard = void 0;
|
|
4
|
+
const types_1 = require("@aeriajs/types");
|
|
5
|
+
const common_1 = require("@aeriajs/common");
|
|
6
|
+
const core_1 = require("@aeriajs/core");
|
|
7
|
+
const authenticationGuard = (decodedToken) => {
|
|
8
|
+
decodedToken.authenticated = true;
|
|
9
|
+
return true;
|
|
10
|
+
};
|
|
11
|
+
exports.authenticationGuard = authenticationGuard;
|
|
12
|
+
const getToken = async (request, context) => {
|
|
13
|
+
if (!request.headers.authorization) {
|
|
14
|
+
return types_1.Result.result({
|
|
15
|
+
authenticated: false,
|
|
16
|
+
sub: null,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const decodedToken = await (0, core_1.decodeToken)(typeof request.headers.authorization === 'string'
|
|
20
|
+
? request.headers.authorization.split('Bearer ').at(-1)
|
|
21
|
+
: '');
|
|
22
|
+
if ((0, exports.authenticationGuard)(decodedToken)) {
|
|
23
|
+
if (typeof decodedToken.sub === 'string') {
|
|
24
|
+
decodedToken.sub = new core_1.ObjectId(decodedToken.sub);
|
|
25
|
+
Object.assign(decodedToken.userinfo, (0, common_1.throwIfError)(await (0, core_1.traverseDocument)(decodedToken.userinfo, context.collections.user.description, {
|
|
26
|
+
autoCast: true,
|
|
27
|
+
})));
|
|
28
|
+
if (context.config.security.revalidateToken) {
|
|
29
|
+
const userCollection = (0, core_1.getDatabaseCollection)('user');
|
|
30
|
+
const user = await userCollection.findOne({
|
|
31
|
+
_id: decodedToken.sub,
|
|
32
|
+
active: true,
|
|
33
|
+
}, {
|
|
34
|
+
projection: {
|
|
35
|
+
roles: 1,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
if (!user) {
|
|
39
|
+
return types_1.Result.error(types_1.ACError.InvalidToken);
|
|
40
|
+
}
|
|
41
|
+
const rolesMatch = decodedToken.roles.every((role) => user.roles.includes(role));
|
|
42
|
+
if (!rolesMatch) {
|
|
43
|
+
return types_1.Result.error(types_1.ACError.InvalidToken);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return types_1.Result.result(decodedToken);
|
|
49
|
+
};
|
|
50
|
+
exports.getToken = getToken;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { Result, ACError } from "@aeriajs/types";
|
|
3
|
+
import { throwIfError } from "@aeriajs/common";
|
|
4
|
+
import { getDatabaseCollection, decodeToken, traverseDocument, ObjectId } from "@aeriajs/core";
|
|
5
|
+
export const authenticationGuard = (decodedToken) => {
|
|
6
|
+
decodedToken.authenticated = true;
|
|
7
|
+
return true;
|
|
8
|
+
};
|
|
9
|
+
export const getToken = async (request, context) => {
|
|
10
|
+
if (!request.headers.authorization) {
|
|
11
|
+
return Result.result({
|
|
12
|
+
authenticated: false,
|
|
13
|
+
sub: null
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
const decodedToken = await decodeToken(typeof request.headers.authorization === "string" ? request.headers.authorization.split("Bearer ").at(-1) : "");
|
|
17
|
+
if (authenticationGuard(decodedToken)) {
|
|
18
|
+
if (typeof decodedToken.sub === "string") {
|
|
19
|
+
decodedToken.sub = new ObjectId(decodedToken.sub);
|
|
20
|
+
Object.assign(decodedToken.userinfo, throwIfError(await traverseDocument(decodedToken.userinfo, context.collections.user.description, {
|
|
21
|
+
autoCast: true
|
|
22
|
+
})));
|
|
23
|
+
if (context.config.security.revalidateToken) {
|
|
24
|
+
const userCollection = getDatabaseCollection("user");
|
|
25
|
+
const user = await userCollection.findOne({
|
|
26
|
+
_id: decodedToken.sub,
|
|
27
|
+
active: true
|
|
28
|
+
}, {
|
|
29
|
+
projection: {
|
|
30
|
+
roles: 1
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
if (!user) {
|
|
34
|
+
return Result.error(ACError.InvalidToken);
|
|
35
|
+
}
|
|
36
|
+
const rolesMatch = decodedToken.roles.every((role) => user.roles.includes(role));
|
|
37
|
+
if (!rolesMatch) {
|
|
38
|
+
return Result.error(ACError.InvalidToken);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return Result.result(decodedToken);
|
|
44
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.mjs
CHANGED
package/dist/init.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RouteContext, Collection,
|
|
1
|
+
import type { RouteContext, Collection, ApiConfig, NonCircularJsonSchema } from '@aeriajs/types';
|
|
2
2
|
import { type createRouter } from '@aeriajs/http';
|
|
3
3
|
import { DEFAULT_API_CONFIG } from './constants.js';
|
|
4
4
|
type DeepPartial<T> = T extends Record<string, unknown> ? {
|
|
@@ -14,22 +14,6 @@ export type InitOptions = {
|
|
|
14
14
|
description: NonCircularJsonSchema;
|
|
15
15
|
}>;
|
|
16
16
|
};
|
|
17
|
-
export declare const getToken: (request: GenericRequest, context: RouteContext) => Promise<{
|
|
18
|
-
readonly _tag: "Error";
|
|
19
|
-
readonly error: "INVALID_TOKEN";
|
|
20
|
-
readonly result: undefined;
|
|
21
|
-
} | {
|
|
22
|
-
readonly _tag: "Result";
|
|
23
|
-
readonly error: undefined;
|
|
24
|
-
readonly result: import("@aeriajs/types").UnauthenticatedToken | AuthenticatedToken<true, import("@aeriajs/types").UserRole>;
|
|
25
|
-
} | {
|
|
26
|
-
readonly _tag: "Error";
|
|
27
|
-
readonly error: {
|
|
28
|
-
readonly httpStatus: 401;
|
|
29
|
-
readonly code: "AUTHENTICATION_ERROR";
|
|
30
|
-
};
|
|
31
|
-
readonly result: undefined;
|
|
32
|
-
}>;
|
|
33
17
|
export declare const init: (_options?: InitOptions) => {
|
|
34
18
|
options: {
|
|
35
19
|
config: {};
|
package/dist/init.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.init =
|
|
3
|
+
exports.init = void 0;
|
|
4
4
|
const types_1 = require("@aeriajs/types");
|
|
5
5
|
const common_1 = require("@aeriajs/common");
|
|
6
6
|
const http_1 = require("@aeriajs/http");
|
|
@@ -9,60 +9,7 @@ const core_1 = require("@aeriajs/core");
|
|
|
9
9
|
const constants_js_1 = require("./constants.js");
|
|
10
10
|
const warmup_js_1 = require("./warmup.js");
|
|
11
11
|
const routes_js_1 = require("./routes.js");
|
|
12
|
-
const
|
|
13
|
-
decodedToken.authenticated = true;
|
|
14
|
-
return true;
|
|
15
|
-
};
|
|
16
|
-
const getToken = async (request, context) => {
|
|
17
|
-
if (!request.headers.authorization) {
|
|
18
|
-
return types_1.Result.result({
|
|
19
|
-
authenticated: false,
|
|
20
|
-
sub: null,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
const decodedToken = await (0, core_1.decodeToken)(typeof request.headers.authorization === 'string'
|
|
25
|
-
? request.headers.authorization.split('Bearer ').at(-1)
|
|
26
|
-
: '');
|
|
27
|
-
if (authenticationGuard(decodedToken)) {
|
|
28
|
-
if (typeof decodedToken.sub === 'string') {
|
|
29
|
-
decodedToken.sub = new core_1.ObjectId(decodedToken.sub);
|
|
30
|
-
Object.assign(decodedToken.userinfo, (0, common_1.throwIfError)(await (0, core_1.traverseDocument)(decodedToken.userinfo, context.collections.user.description, {
|
|
31
|
-
autoCast: true,
|
|
32
|
-
})));
|
|
33
|
-
if (context.config.security.revalidateToken) {
|
|
34
|
-
const userCollection = (0, core_1.getDatabaseCollection)('user');
|
|
35
|
-
const user = await userCollection.findOne({
|
|
36
|
-
_id: decodedToken.sub,
|
|
37
|
-
active: true,
|
|
38
|
-
}, {
|
|
39
|
-
projection: {
|
|
40
|
-
roles: 1,
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
if (!user) {
|
|
44
|
-
return types_1.Result.error(types_1.ACError.InvalidToken);
|
|
45
|
-
}
|
|
46
|
-
const rolesMatch = decodedToken.roles.every((role) => user.roles.includes(role));
|
|
47
|
-
if (!rolesMatch) {
|
|
48
|
-
return types_1.Result.error(types_1.ACError.InvalidToken);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return types_1.Result.result(decodedToken);
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
if (process.env.NODE_ENV === 'development') {
|
|
57
|
-
console.trace(err);
|
|
58
|
-
}
|
|
59
|
-
return (0, common_1.endpointError)({
|
|
60
|
-
httpStatus: types_1.HTTPStatus.Unauthorized,
|
|
61
|
-
code: types_1.ACError.AuthenticationError,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
exports.getToken = getToken;
|
|
12
|
+
const getToken_js_1 = require("./getToken.js");
|
|
66
13
|
const init = (_options = {}) => {
|
|
67
14
|
const options = Object.assign({
|
|
68
15
|
config: {},
|
|
@@ -86,14 +33,41 @@ const init = (_options = {}) => {
|
|
|
86
33
|
const apiRouter = (0, routes_js_1.registerRoutes)();
|
|
87
34
|
const server = (0, node_http_1.registerServer)(config.server, async (request, response) => {
|
|
88
35
|
if (config.server && config.server.cors) {
|
|
89
|
-
|
|
36
|
+
let result;
|
|
37
|
+
switch (typeof config.server.cors) {
|
|
38
|
+
case 'function': {
|
|
39
|
+
result = await config.server.cors(request, response, constants_js_1.DEFAULT_API_CONFIG.server.cors);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case 'object': {
|
|
43
|
+
result = await (0, http_1.cors)(request, response, config.server.cors);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (result === null) {
|
|
90
48
|
return;
|
|
91
49
|
}
|
|
92
50
|
}
|
|
93
51
|
await (0, http_1.wrapRouteExecution)(response, async () => {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
52
|
+
const getTokenFn = config.server?.getToken
|
|
53
|
+
? config.server.getToken
|
|
54
|
+
: getToken_js_1.getToken;
|
|
55
|
+
let token;
|
|
56
|
+
try {
|
|
57
|
+
const { error, result } = await getTokenFn(request, parentContext);
|
|
58
|
+
if (error) {
|
|
59
|
+
return types_1.Result.error(error);
|
|
60
|
+
}
|
|
61
|
+
token = result;
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
if (process.env.NODE_ENV === 'development') {
|
|
65
|
+
console.trace(err);
|
|
66
|
+
}
|
|
67
|
+
return (0, common_1.endpointError)({
|
|
68
|
+
httpStatus: types_1.HTTPStatus.Unauthorized,
|
|
69
|
+
code: types_1.ACError.AuthenticationError,
|
|
70
|
+
});
|
|
97
71
|
}
|
|
98
72
|
const context = await (0, core_1.createContext)({
|
|
99
73
|
parentContext,
|
package/dist/init.mjs
CHANGED
|
@@ -1,62 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { ACError, HTTPStatus, Result } from "@aeriajs/types";
|
|
3
|
+
import { deepMerge, endpointError } from "@aeriajs/common";
|
|
4
4
|
import { cors, wrapRouteExecution } from "@aeriajs/http";
|
|
5
5
|
import { registerServer } from "@aeriajs/node-http";
|
|
6
|
-
import { createContext, getDatabase
|
|
6
|
+
import { createContext, getDatabase } from "@aeriajs/core";
|
|
7
7
|
import { DEFAULT_API_CONFIG } from "./constants.mjs";
|
|
8
8
|
import { warmup } from "./warmup.mjs";
|
|
9
9
|
import { registerRoutes } from "./routes.mjs";
|
|
10
|
-
|
|
11
|
-
decodedToken.authenticated = true;
|
|
12
|
-
return true;
|
|
13
|
-
};
|
|
14
|
-
export const getToken = async (request, context) => {
|
|
15
|
-
if (!request.headers.authorization) {
|
|
16
|
-
return Result.result({
|
|
17
|
-
authenticated: false,
|
|
18
|
-
sub: null
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
const decodedToken = await decodeToken(typeof request.headers.authorization === "string" ? request.headers.authorization.split("Bearer ").at(-1) : "");
|
|
23
|
-
if (authenticationGuard(decodedToken)) {
|
|
24
|
-
if (typeof decodedToken.sub === "string") {
|
|
25
|
-
decodedToken.sub = new ObjectId(decodedToken.sub);
|
|
26
|
-
Object.assign(decodedToken.userinfo, throwIfError(await traverseDocument(decodedToken.userinfo, context.collections.user.description, {
|
|
27
|
-
autoCast: true
|
|
28
|
-
})));
|
|
29
|
-
if (context.config.security.revalidateToken) {
|
|
30
|
-
const userCollection = getDatabaseCollection("user");
|
|
31
|
-
const user = await userCollection.findOne({
|
|
32
|
-
_id: decodedToken.sub,
|
|
33
|
-
active: true
|
|
34
|
-
}, {
|
|
35
|
-
projection: {
|
|
36
|
-
roles: 1
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
if (!user) {
|
|
40
|
-
return Result.error(ACError.InvalidToken);
|
|
41
|
-
}
|
|
42
|
-
const rolesMatch = decodedToken.roles.every((role) => user.roles.includes(role));
|
|
43
|
-
if (!rolesMatch) {
|
|
44
|
-
return Result.error(ACError.InvalidToken);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return Result.result(decodedToken);
|
|
50
|
-
} catch (err) {
|
|
51
|
-
if (true) {
|
|
52
|
-
console.trace(err);
|
|
53
|
-
}
|
|
54
|
-
return endpointError({
|
|
55
|
-
httpStatus: HTTPStatus.Unauthorized,
|
|
56
|
-
code: ACError.AuthenticationError
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
};
|
|
10
|
+
import { getToken } from "./getToken.mjs";
|
|
60
11
|
export const init = (_options = {}) => {
|
|
61
12
|
const options = Object.assign({
|
|
62
13
|
config: {}
|
|
@@ -80,14 +31,38 @@ export const init = (_options = {}) => {
|
|
|
80
31
|
const apiRouter = registerRoutes();
|
|
81
32
|
const server = registerServer(config.server, async (request, response) => {
|
|
82
33
|
if (config.server && config.server.cors) {
|
|
83
|
-
|
|
34
|
+
let result;
|
|
35
|
+
switch (typeof config.server.cors) {
|
|
36
|
+
case "function": {
|
|
37
|
+
result = await config.server.cors(request, response, DEFAULT_API_CONFIG.server.cors);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case "object": {
|
|
41
|
+
result = await cors(request, response, config.server.cors);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (result === null) {
|
|
84
46
|
return;
|
|
85
47
|
}
|
|
86
48
|
}
|
|
87
49
|
await wrapRouteExecution(response, async () => {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
50
|
+
const getTokenFn = config.server?.getToken ? config.server.getToken : getToken;
|
|
51
|
+
let token;
|
|
52
|
+
try {
|
|
53
|
+
const { error, result } = await getTokenFn(request, parentContext);
|
|
54
|
+
if (error) {
|
|
55
|
+
return Result.error(error);
|
|
56
|
+
}
|
|
57
|
+
token = result;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
if (true) {
|
|
60
|
+
console.trace(err);
|
|
61
|
+
}
|
|
62
|
+
return endpointError({
|
|
63
|
+
httpStatus: HTTPStatus.Unauthorized,
|
|
64
|
+
code: ACError.AuthenticationError
|
|
65
|
+
});
|
|
91
66
|
}
|
|
92
67
|
const context = await createContext({
|
|
93
68
|
parentContext,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.279",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
"mongodb": "^6.17.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@aeriajs/core": "^0.0.
|
|
37
|
-
"@aeriajs/builtins": "^0.0.
|
|
38
|
-
"@aeriajs/common": "^0.0.
|
|
39
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
40
|
-
"@aeriajs/http": "^0.0.
|
|
41
|
-
"@aeriajs/node-http": "^0.0.
|
|
42
|
-
"@aeriajs/server": "^0.0.
|
|
43
|
-
"@aeriajs/types": "^0.0.
|
|
36
|
+
"@aeriajs/core": "^0.0.277",
|
|
37
|
+
"@aeriajs/builtins": "^0.0.277",
|
|
38
|
+
"@aeriajs/common": "^0.0.157",
|
|
39
|
+
"@aeriajs/entrypoint": "^0.0.163",
|
|
40
|
+
"@aeriajs/http": "^0.0.193",
|
|
41
|
+
"@aeriajs/node-http": "^0.0.193",
|
|
42
|
+
"@aeriajs/server": "^0.0.279",
|
|
43
|
+
"@aeriajs/types": "^0.0.135",
|
|
44
44
|
"mongodb": "^6.17.0"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|