@volcanicminds/backend 0.2.38 → 0.2.40
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/index.js +52 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/api/auth/controller/auth.js +33 -17
- package/dist/lib/api/auth/controller/auth.js.map +1 -1
- package/dist/lib/api/auth/routes.js +29 -0
- package/dist/lib/api/auth/routes.js.map +1 -1
- package/dist/lib/api/token/controller/token.js +114 -0
- package/dist/lib/api/token/controller/token.js.map +1 -0
- package/dist/lib/api/token/routes.js +170 -0
- package/dist/lib/api/token/routes.js.map +1 -0
- package/dist/lib/api/users/controller/user.js +1 -1
- package/dist/lib/api/users/controller/user.js.map +1 -1
- package/dist/lib/hooks/onRequest.js +28 -10
- package/dist/lib/hooks/onRequest.js.map +1 -1
- package/dist/lib/middleware/isAdmin.js +1 -1
- package/dist/lib/middleware/isAdmin.js.map +1 -1
- package/dist/lib/middleware/isAuthenticated.js +1 -1
- package/dist/lib/middleware/isAuthenticated.js.map +1 -1
- package/dist/lib/schemas/auth.js +2 -0
- package/dist/lib/schemas/auth.js.map +1 -0
- package/dist/lib/schemas/common.js +33 -0
- package/dist/lib/schemas/common.js.map +1 -0
- package/dist/lib/schemas/token.js +39 -0
- package/dist/lib/schemas/token.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.ts +57 -5
- package/lib/api/auth/controller/auth.ts +34 -15
- package/lib/api/auth/routes.ts +29 -0
- package/lib/api/token/controller/token.ts +99 -0
- package/lib/api/token/routes.ts +168 -0
- package/lib/api/users/controller/user.ts +1 -1
- package/lib/hooks/onRequest.ts +32 -12
- package/lib/middleware/isAdmin.ts +1 -1
- package/lib/middleware/isAuthenticated.ts +1 -1
- package/lib/schemas/auth.ts +0 -0
- package/lib/schemas/common.ts +31 -0
- package/lib/schemas/token.ts +37 -0
- package/package.json +1 -1
- package/types/global.d.ts +32 -5
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = {
|
|
3
|
+
config: {
|
|
4
|
+
title: 'Integration token functions',
|
|
5
|
+
description: 'Integration token functions',
|
|
6
|
+
controller: 'controller',
|
|
7
|
+
tags: ['token'],
|
|
8
|
+
deprecated: false,
|
|
9
|
+
version: false,
|
|
10
|
+
enable: true
|
|
11
|
+
},
|
|
12
|
+
routes: [
|
|
13
|
+
{
|
|
14
|
+
method: 'GET',
|
|
15
|
+
path: '/',
|
|
16
|
+
roles: [roles.admin, roles.backoffice],
|
|
17
|
+
handler: 'token.find',
|
|
18
|
+
middlewares: [],
|
|
19
|
+
config: {
|
|
20
|
+
title: 'Find tokens',
|
|
21
|
+
description: 'Get tokens list',
|
|
22
|
+
query: { $ref: 'getQueryParamsSchema' },
|
|
23
|
+
response: {
|
|
24
|
+
200: {
|
|
25
|
+
description: 'Default response',
|
|
26
|
+
type: 'array',
|
|
27
|
+
items: { $ref: 'tokenSchema#' }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
method: 'GET',
|
|
34
|
+
path: '/count',
|
|
35
|
+
roles: [roles.admin, roles.backoffice],
|
|
36
|
+
handler: 'token.count',
|
|
37
|
+
middlewares: [],
|
|
38
|
+
config: {
|
|
39
|
+
title: 'Count tokens',
|
|
40
|
+
description: 'Count tokens',
|
|
41
|
+
response: {
|
|
42
|
+
200: {
|
|
43
|
+
description: 'Default response',
|
|
44
|
+
type: 'number'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
method: 'GET',
|
|
51
|
+
path: '/:id',
|
|
52
|
+
roles: [roles.admin, roles.backoffice],
|
|
53
|
+
handler: 'token.findOne',
|
|
54
|
+
middlewares: [],
|
|
55
|
+
config: {
|
|
56
|
+
title: 'Find token',
|
|
57
|
+
description: 'Get token by id',
|
|
58
|
+
params: { $ref: 'tokenParamsSchema#' },
|
|
59
|
+
response: {
|
|
60
|
+
200: {
|
|
61
|
+
description: 'Default response',
|
|
62
|
+
$ref: 'tokenSchema#'
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
method: 'POST',
|
|
69
|
+
path: '/',
|
|
70
|
+
roles: [roles.admin, roles.backoffice],
|
|
71
|
+
handler: 'token.create',
|
|
72
|
+
middlewares: ['global.isAuthenticated'],
|
|
73
|
+
config: {
|
|
74
|
+
title: 'Create new token',
|
|
75
|
+
description: 'Create a new token',
|
|
76
|
+
body: { $ref: 'tokenBodySchema' },
|
|
77
|
+
response: {
|
|
78
|
+
200: {
|
|
79
|
+
description: 'Default response',
|
|
80
|
+
$ref: 'tokenSchema#'
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
method: 'PUT',
|
|
87
|
+
path: '/:id',
|
|
88
|
+
roles: [roles.admin, roles.backoffice],
|
|
89
|
+
handler: 'token.update',
|
|
90
|
+
middlewares: ['global.isAuthenticated'],
|
|
91
|
+
config: {
|
|
92
|
+
title: 'Update existing token',
|
|
93
|
+
description: 'Update an existing token',
|
|
94
|
+
params: { $ref: 'tokenParamsSchema#' },
|
|
95
|
+
body: { $ref: 'tokenBodySchema' },
|
|
96
|
+
response: {
|
|
97
|
+
200: {
|
|
98
|
+
description: 'Default response',
|
|
99
|
+
$ref: 'tokenSchema#'
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
method: 'DELETE',
|
|
106
|
+
path: '/:id',
|
|
107
|
+
roles: [roles.admin, roles.backoffice],
|
|
108
|
+
handler: 'token.remove',
|
|
109
|
+
middlewares: ['global.isAuthenticated'],
|
|
110
|
+
config: {
|
|
111
|
+
title: 'Unregister existing token (actually disables it)',
|
|
112
|
+
description: 'Unregister an existing token (actually disables it)',
|
|
113
|
+
params: { $ref: 'tokenParamsSchema#' },
|
|
114
|
+
response: {
|
|
115
|
+
200: {
|
|
116
|
+
description: 'Default response',
|
|
117
|
+
type: 'object',
|
|
118
|
+
properties: {
|
|
119
|
+
ok: { type: 'boolean' }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
method: 'POST',
|
|
127
|
+
path: '/block/:id',
|
|
128
|
+
roles: [roles.admin, roles.backoffice],
|
|
129
|
+
handler: 'token.block',
|
|
130
|
+
middlewares: ['global.isAuthenticated'],
|
|
131
|
+
config: {
|
|
132
|
+
title: 'Block a token by id',
|
|
133
|
+
description: 'Block a token by id',
|
|
134
|
+
params: { $ref: 'tokenParamsSchema#' },
|
|
135
|
+
body: { $ref: 'blockBodySchema' },
|
|
136
|
+
response: {
|
|
137
|
+
200: {
|
|
138
|
+
description: 'Default response',
|
|
139
|
+
type: 'object',
|
|
140
|
+
properties: {
|
|
141
|
+
ok: { type: 'boolean' }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
method: 'POST',
|
|
149
|
+
path: '/unblock/:id',
|
|
150
|
+
roles: [roles.admin, roles.backoffice],
|
|
151
|
+
handler: 'token.unblock',
|
|
152
|
+
middlewares: ['global.isAuthenticated'],
|
|
153
|
+
config: {
|
|
154
|
+
title: 'Unblock a token by id',
|
|
155
|
+
description: 'Unblock a token by id',
|
|
156
|
+
params: { $ref: 'tokenParamsSchema#' },
|
|
157
|
+
response: {
|
|
158
|
+
200: {
|
|
159
|
+
description: 'Default response',
|
|
160
|
+
type: 'object',
|
|
161
|
+
properties: {
|
|
162
|
+
ok: { type: 'boolean' }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
};
|
|
170
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../../../lib/api/token/routes.ts"],"names":[],"mappings":";AAAA,MAAM,CAAC,OAAO,GAAG;IACf,MAAM,EAAE;QACN,KAAK,EAAE,6BAA6B;QACpC,WAAW,EAAE,6BAA6B;QAC1C,UAAU,EAAE,YAAY;QACxB,IAAI,EAAE,CAAC,OAAO,CAAC;QACf,UAAU,EAAE,KAAK;QACjB,OAAO,EAAE,KAAK;QACd,MAAM,EAAE,IAAI;KACb;IACD,MAAM,EAAE;QACN;YACE,MAAM,EAAE,KAAK;YACb,IAAI,EAAE,GAAG;YACT,KAAK,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC;YACtC,OAAO,EAAE,YAAY;YACrB,WAAW,EAAE,EAAE;YACf,MAAM,EAAE;gBACN,KAAK,EAAE,aAAa;gBACpB,WAAW,EAAE,iBAAiB;gBAC9B,KAAK,EAAE,EAAE,IAAI,EAAE,sBAAsB,EAAE;gBACvC,QAAQ,EAAE;oBACR,GAAG,EAAE;wBACH,WAAW,EAAE,kBAAkB;wBAC/B,IAAI,EAAE,OAAO;wBACb,KAAK,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;qBAChC;iBACF;aACF;SACF;QACD;YACE,MAAM,EAAE,KAAK;YACb,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC;YACtC,OAAO,EAAE,aAAa;YACtB,WAAW,EAAE,EAAE;YACf,MAAM,EAAE;gBACN,KAAK,EAAE,cAAc;gBACrB,WAAW,EAAE,cAAc;gBAC3B,QAAQ,EAAE;oBACR,GAAG,EAAE;wBACH,WAAW,EAAE,kBAAkB;wBAC/B,IAAI,EAAE,QAAQ;qBACf;iBACF;aACF;SACF;QACD;YACE,MAAM,EAAE,KAAK;YACb,IAAI,EAAE,MAAM;YAC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E,CAAC,wBAAwB,CAAC;YACvC,MAAM,EAAE;gBACN,KAAK,EAAE,uBAAuB;gBAC9B,WAAW,EAAE,uBAAuB;gBACpC,MAAM,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE;gBACtC,QAAQ,EAAE;oBACR,GAAG,EAAE;wBACH,WAAW,EAAE,kBAAkB;wBAC/B,IAAI,EAAE,QAAQ;wBACd,UAAU,EAAE;4BACV,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;yBACxB;qBACF;iBACF;aACF;SACF;KACF;CACF,CAAA"}
|
|
@@ -20,7 +20,7 @@ exports.user = user;
|
|
|
20
20
|
function isAdmin(req, reply) {
|
|
21
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
22
22
|
const user = req.user;
|
|
23
|
-
reply.send({ isAdmin: (user === null || user === void 0 ? void 0 : user.
|
|
23
|
+
reply.send({ isAdmin: (user === null || user === void 0 ? void 0 : user.getId()) && req.hasRole(roles.admin) });
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
exports.isAdmin = isAdmin;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../../../lib/api/users/controller/user.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,SAAsB,IAAI,CAAC,GAAmB,EAAE,KAAmB;;QACjE,MAAM,IAAI,GAAkC,GAAG,CAAC,IAAI,CAAA;QACpD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,iCAAM,IAAI,KAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,IAAG,CAAC,CAAC,EAAE,CAAC,CAAA;IACzD,CAAC;CAAA;AAHD,oBAGC;AAED,SAAsB,OAAO,CAAC,GAAmB,EAAE,KAAmB;;QACpE,MAAM,IAAI,GAAkC,GAAG,CAAC,IAAI,CAAA;QACpD,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,EAAE,KAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../../../lib/api/users/controller/user.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,SAAsB,IAAI,CAAC,GAAmB,EAAE,KAAmB;;QACjE,MAAM,IAAI,GAAkC,GAAG,CAAC,IAAI,CAAA;QACpD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,iCAAM,IAAI,KAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,IAAG,CAAC,CAAC,EAAE,CAAC,CAAA;IACzD,CAAC;CAAA;AAHD,oBAGC;AAED,SAAsB,OAAO,CAAC,GAAmB,EAAE,KAAmB;;QACpE,MAAM,IAAI,GAAkC,GAAG,CAAC,IAAI,CAAA;QACpD,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,KAAK,EAAE,KAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACpE,CAAC;CAAA;AAHD,0BAGC"}
|
|
@@ -11,28 +11,44 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const common_1 = require("../util/common");
|
|
13
13
|
module.exports = (req, reply) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
-
var _a, _b, _c, _d, _e;
|
|
14
|
+
var _a, _b, _c, _d, _e, _f;
|
|
15
15
|
log.i && (req.startedAt = new Date());
|
|
16
16
|
req.data = () => (0, common_1.getData)(req);
|
|
17
17
|
req.parameters = () => (0, common_1.getParams)(req);
|
|
18
18
|
req.roles = () => (req.user ? req.user.roles : [roles.public]);
|
|
19
19
|
req.hasRole = (r) => (req.user ? req.user.roles : [roles.public]).some((role) => role === (r === null || r === void 0 ? void 0 : r.code));
|
|
20
20
|
const auth = ((_a = req.headers) === null || _a === void 0 ? void 0 : _a.authorization) || '';
|
|
21
|
-
const [prefix,
|
|
21
|
+
const [prefix, bearerToken] = auth.split(' ');
|
|
22
22
|
const isRoutePublic = (req.routeConfig.requiredRoles || []).some((role) => role.code === roles.public.code);
|
|
23
|
-
if (prefix === 'Bearer' &&
|
|
23
|
+
if (prefix === 'Bearer' && bearerToken != null) {
|
|
24
24
|
let user = {};
|
|
25
|
+
let token = {};
|
|
26
|
+
console.log('bearer ' + bearerToken);
|
|
25
27
|
try {
|
|
26
|
-
const tokenData = reply.server.jwt.verify(
|
|
28
|
+
const tokenData = reply.server.jwt.verify(bearerToken);
|
|
27
29
|
user = yield req.server['userManager'].retrieveUserByExternalId(tokenData === null || tokenData === void 0 ? void 0 : tokenData.sub);
|
|
30
|
+
console.log(user);
|
|
28
31
|
if (!user) {
|
|
32
|
+
token = yield req.server['tokenManager'].retrieveTokenByExternalId(tokenData === null || tokenData === void 0 ? void 0 : tokenData.sub);
|
|
33
|
+
console.log(token);
|
|
34
|
+
}
|
|
35
|
+
if (!user && !token) {
|
|
29
36
|
return reply.status(404).send({ statusCode: 404, code: 'USER_NOT_FOUND', message: 'User not found' });
|
|
30
37
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
if (user) {
|
|
39
|
+
const isValid = yield req.server['userManager'].isValidUser(user);
|
|
40
|
+
if (!isValid) {
|
|
41
|
+
return reply.status(404).send({ statusCode: 404, code: 'USER_NOT_VALID', message: 'User not valid' });
|
|
42
|
+
}
|
|
43
|
+
req.user = user;
|
|
44
|
+
}
|
|
45
|
+
if (token) {
|
|
46
|
+
const isValid = yield req.server['tokenManager'].isValidToken(token);
|
|
47
|
+
if (!isValid) {
|
|
48
|
+
return reply.status(404).send({ statusCode: 404, code: 'TOKEN_NOT_VALID', message: 'Token not valid' });
|
|
49
|
+
}
|
|
50
|
+
req.token = token;
|
|
34
51
|
}
|
|
35
|
-
req.user = user;
|
|
36
52
|
}
|
|
37
53
|
catch (error) {
|
|
38
54
|
if (!isRoutePublic) {
|
|
@@ -42,8 +58,10 @@ module.exports = (req, reply) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
42
58
|
}
|
|
43
59
|
if (((_b = req.routeConfig.requiredRoles) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
44
60
|
const { method = '', url = '', requiredRoles } = req.routeConfig;
|
|
45
|
-
const
|
|
46
|
-
|
|
61
|
+
const authRoles = ((_e = (((_c = req.user) === null || _c === void 0 ? void 0 : _c.roles) || ((_d = req.token) === null || _d === void 0 ? void 0 : _d.roles))) === null || _e === void 0 ? void 0 : _e.map((code) => code)) || [
|
|
62
|
+
((_f = roles.public) === null || _f === void 0 ? void 0 : _f.code) || 'public'
|
|
63
|
+
];
|
|
64
|
+
const resolvedRoles = authRoles.length > 0 ? requiredRoles.filter((r) => authRoles.includes(r.code)) : [];
|
|
47
65
|
if (!resolvedRoles.length) {
|
|
48
66
|
log.w && log.warn(`Not allowed to call ${method.toUpperCase()} ${url}`);
|
|
49
67
|
return reply
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"onRequest.js","sourceRoot":"","sources":["../../../lib/hooks/onRequest.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,2CAAmD;AAGnD,MAAM,CAAC,OAAO,GAAG,CAAO,GAAG,EAAE,KAAK,EAAE,EAAE;;IAEpC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,CAAA;IACrC,GAAG,CAAC,IAAI,GAAG,GAAG,EAAE,CAAC,IAAA,gBAAO,EAAC,GAAG,CAAC,CAAA;IAC7B,GAAG,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,IAAA,kBAAS,EAAC,GAAG,CAAC,CAAA;IACrC,GAAG,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,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,EAAE;QAC7C,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,EAAE,EAAE,aAAa,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;QAChE,MAAM,SAAS,GAAa,CAAC,MAAA,CAAC,CAAA,MAAA,GAAG,CAAC,IAAI,0CAAE,KAAK,MAAI,MAAA,GAAG,CAAC,KAAK,0CAAE,KAAK,CAAA,CAAC,0CAAE,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAc,KAAI;YACtG,CAAA,MAAA,KAAK,CAAC,MAAM,0CAAE,IAAI,KAAI,QAAQ;SAC/B,CAAA;QACD,MAAM,aAAa,GAAG,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;QAEzG,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE;YACzB,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,uBAAuB,MAAM,CAAC,WAAW,EAAE,IAAI,GAAG,EAAE,CAAC,CAAA;YACvE,OAAO,KAAK;iBACT,MAAM,CAAC,GAAG,CAAC;iBACX,IAAI,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,IAAI,EAAE,kBAAkB,EAAE,OAAO,EAAE,gCAAgC,EAAE,CAAC,CAAA;SAClG;KACF;AACH,CAAC,CAAA,CAAA"}
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.preHandler = void 0;
|
|
4
4
|
function preHandler(req, res, done) {
|
|
5
5
|
try {
|
|
6
|
-
if (req.user && req.user.
|
|
6
|
+
if (req.user && req.user.getId() && req.hasRole(roles.admin)) {
|
|
7
7
|
return done();
|
|
8
8
|
}
|
|
9
9
|
throw new Error('User without this privilege');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isAdmin.js","sourceRoot":"","sources":["../../../lib/middleware/isAdmin.ts"],"names":[],"mappings":";;;AAEA,SAAgB,UAAU,CAAC,GAAmB,EAAE,GAAiB,EAAE,IAAS;IAC1E,IAAI;QACF,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;
|
|
1
|
+
{"version":3,"file":"isAdmin.js","sourceRoot":"","sources":["../../../lib/middleware/isAdmin.ts"],"names":[],"mappings":";;;AAEA,SAAgB,UAAU,CAAC,GAAmB,EAAE,GAAiB,EAAE,IAAS;IAC1E,IAAI;QACF,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAC5D,OAAO,IAAI,EAAE,CAAA;SACd;QAED,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;KAC/C;IAAC,OAAO,GAAG,EAAE;QACZ,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,iCAAiC,GAAG,EAAE,CAAC,CAAA;QAC1D,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACxB;AACH,CAAC;AAXD,gCAWC"}
|
|
@@ -4,7 +4,7 @@ exports.preHandler = void 0;
|
|
|
4
4
|
function preHandler(req, res, done) {
|
|
5
5
|
var _a;
|
|
6
6
|
try {
|
|
7
|
-
if (!!((_a = req.user) === null || _a === void 0 ? void 0 : _a.
|
|
7
|
+
if (!!((_a = req.user) === null || _a === void 0 ? void 0 : _a.getId())) {
|
|
8
8
|
return done();
|
|
9
9
|
}
|
|
10
10
|
throw new Error('Unauthorized');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isAuthenticated.js","sourceRoot":"","sources":["../../../lib/middleware/isAuthenticated.ts"],"names":[],"mappings":";;;AAEA,SAAgB,UAAU,CAAC,GAAmB,EAAE,GAAiB,EAAE,IAAS;;IAC1E,IAAI;QACF,IAAI,CAAC,CAAC,CAAA,MAAA,GAAG,CAAC,IAAI,0CAAE,EAAE,CAAA,EAAE;
|
|
1
|
+
{"version":3,"file":"isAuthenticated.js","sourceRoot":"","sources":["../../../lib/middleware/isAuthenticated.ts"],"names":[],"mappings":";;;AAEA,SAAgB,UAAU,CAAC,GAAmB,EAAE,GAAiB,EAAE,IAAS;;IAC1E,IAAI;QACF,IAAI,CAAC,CAAC,CAAA,MAAA,GAAG,CAAC,IAAI,0CAAE,KAAK,EAAE,CAAA,EAAE;YACvB,OAAO,IAAI,EAAE,CAAA;SACd;QAED,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;KAChC;IAAC,OAAO,GAAG,EAAE;QACZ,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,iCAAiC,GAAG,EAAE,CAAC,CAAA;QAC1D,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KAC/B;AACH,CAAC;AAXD,gCAWC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../lib/schemas/auth.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blockBodySchema = exports.getQueryParamsSchema = void 0;
|
|
4
|
+
exports.getQueryParamsSchema = {
|
|
5
|
+
$id: 'getQueryParamsSchema',
|
|
6
|
+
type: 'object',
|
|
7
|
+
nullable: true,
|
|
8
|
+
properties: {
|
|
9
|
+
page: {
|
|
10
|
+
type: 'number',
|
|
11
|
+
description: 'Page **number** (default 1)'
|
|
12
|
+
},
|
|
13
|
+
pageSize: {
|
|
14
|
+
type: 'number',
|
|
15
|
+
description: 'Page **size** (default 25)'
|
|
16
|
+
},
|
|
17
|
+
sort: {
|
|
18
|
+
type: 'array',
|
|
19
|
+
description: 'Sorting **order** (default ascending).<br/>\
|
|
20
|
+
Otherwise, use the postfix `:desc` or `:asc` (like `&sort=myfield:desc`)',
|
|
21
|
+
items: { type: 'string' }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.blockBodySchema = {
|
|
26
|
+
$id: 'blockBodySchema',
|
|
27
|
+
type: 'object',
|
|
28
|
+
nullable: true,
|
|
29
|
+
properties: {
|
|
30
|
+
reason: { type: 'string' }
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../lib/schemas/common.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG;IAClC,GAAG,EAAE,sBAAsB;IAC3B,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE;QACV,IAAI,EAAE;YACJ,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,6BAA6B;SAC3C;QACD,QAAQ,EAAE;YACR,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,4BAA4B;SAC1C;QACD,IAAI,EAAE;YACJ,IAAI,EAAE,OAAO;YACb,WAAW,EACT;iFACyE;YAC3E,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC1B;KACF;CACF,CAAA;AAEY,QAAA,eAAe,GAAG;IAC7B,GAAG,EAAE,iBAAiB;IACtB,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE;QACV,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC3B;CACF,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokenSchema = exports.tokenBodySchema = exports.tokenParamsSchema = void 0;
|
|
4
|
+
exports.tokenParamsSchema = {
|
|
5
|
+
$id: 'tokenParamsSchema',
|
|
6
|
+
type: 'object',
|
|
7
|
+
nullable: true,
|
|
8
|
+
properties: {
|
|
9
|
+
id: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'Token id'
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.tokenBodySchema = {
|
|
16
|
+
$id: 'tokenBodySchema',
|
|
17
|
+
type: 'object',
|
|
18
|
+
nullable: true,
|
|
19
|
+
properties: {
|
|
20
|
+
name: { type: 'string' },
|
|
21
|
+
description: { type: 'string' },
|
|
22
|
+
requiredRoles: { type: 'array', items: { type: 'string' } }
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.tokenSchema = {
|
|
26
|
+
$id: 'tokenSchema',
|
|
27
|
+
type: 'object',
|
|
28
|
+
nullable: true,
|
|
29
|
+
properties: {
|
|
30
|
+
id: { type: 'string' },
|
|
31
|
+
_id: { type: 'string' },
|
|
32
|
+
externalId: { type: 'string' },
|
|
33
|
+
name: { type: 'string' },
|
|
34
|
+
description: { type: 'string' },
|
|
35
|
+
token: { type: 'string' },
|
|
36
|
+
roles: { type: 'array', items: { type: 'string' } }
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../../lib/schemas/token.ts"],"names":[],"mappings":";;;AAAa,QAAA,iBAAiB,GAAG;IAC/B,GAAG,EAAE,mBAAmB;IACxB,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE;QACV,EAAE,EAAE;YACF,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,UAAU;SACxB;KACF;CACF,CAAA;AAEY,QAAA,eAAe,GAAG;IAC7B,GAAG,EAAE,iBAAiB;IACtB,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC/B,aAAa,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;KAC5D;CACF,CAAA;AAEY,QAAA,WAAW,GAAG;IACzB,GAAG,EAAE,aAAa;IAClB,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE;QACV,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACvB,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC9B,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC/B,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACzB,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;KACpD;CACF,CAAA"}
|
package/index.d.ts
CHANGED
package/index.ts
CHANGED
|
@@ -27,7 +27,7 @@ import fastifyApollo, { fastifyApolloDrainPlugin } from '@as-integrations/fastif
|
|
|
27
27
|
import { myContextFunction, MyContext } from './lib/apollo/context'
|
|
28
28
|
import resolvers from './lib/apollo/resolvers'
|
|
29
29
|
import typeDefs from './lib/apollo/type-defs'
|
|
30
|
-
import { UserManagement } from './types/global'
|
|
30
|
+
import { UserManagement, TokenManagement } from './types/global'
|
|
31
31
|
|
|
32
32
|
global.log = logger
|
|
33
33
|
|
|
@@ -168,6 +168,9 @@ const start = async (decorators) => {
|
|
|
168
168
|
// defaults
|
|
169
169
|
decorators = {
|
|
170
170
|
userManager: {
|
|
171
|
+
isValidUser(data: any) {
|
|
172
|
+
throw Error('Not implemented')
|
|
173
|
+
},
|
|
171
174
|
createUser(data: any) {
|
|
172
175
|
throw Error('Not implemented')
|
|
173
176
|
},
|
|
@@ -183,6 +186,15 @@ const start = async (decorators) => {
|
|
|
183
186
|
retrieveUserByEmail(email: string) {
|
|
184
187
|
throw Error('Not implemented')
|
|
185
188
|
},
|
|
189
|
+
retrieveUserByConfirmationToken(code: string) {
|
|
190
|
+
throw Error('Not implemented')
|
|
191
|
+
},
|
|
192
|
+
retrieveUserByResetPasswordToken(code: string) {
|
|
193
|
+
throw Error('Not implemented')
|
|
194
|
+
},
|
|
195
|
+
retrieveUserByUsername(username: string) {
|
|
196
|
+
throw Error('Not implemented')
|
|
197
|
+
},
|
|
186
198
|
retrieveUserByExternalId(externalId: string) {
|
|
187
199
|
throw Error('Not implemented')
|
|
188
200
|
},
|
|
@@ -201,16 +213,54 @@ const start = async (decorators) => {
|
|
|
201
213
|
resetPassword(user: any, password: string) {
|
|
202
214
|
throw Error('Not implemented')
|
|
203
215
|
},
|
|
204
|
-
|
|
216
|
+
blockUserById(id: string, reason: string) {
|
|
205
217
|
throw Error('Not implemented')
|
|
206
218
|
},
|
|
207
|
-
|
|
219
|
+
unblockUserById(data: any) {
|
|
208
220
|
throw Error('Not implemented')
|
|
209
221
|
},
|
|
210
|
-
|
|
222
|
+
countQuery(data: any) {
|
|
223
|
+
throw Error('Not implemented')
|
|
224
|
+
},
|
|
225
|
+
findQuery(data: any) {
|
|
211
226
|
throw Error('Not implemented')
|
|
212
227
|
}
|
|
213
228
|
} as UserManagement,
|
|
229
|
+
tokenManager: {
|
|
230
|
+
isValidToken(data: any) {
|
|
231
|
+
throw Error('Not implemented')
|
|
232
|
+
},
|
|
233
|
+
createToken(data: any) {
|
|
234
|
+
throw Error('Not implemented')
|
|
235
|
+
},
|
|
236
|
+
resetExternalId(id: string) {
|
|
237
|
+
throw Error('Not implemented')
|
|
238
|
+
},
|
|
239
|
+
updateTokenById(id: string, token: any) {
|
|
240
|
+
throw Error('Not implemented')
|
|
241
|
+
},
|
|
242
|
+
retrieveTokenById(id: string) {
|
|
243
|
+
throw Error('Not implemented')
|
|
244
|
+
},
|
|
245
|
+
retrieveTokenByExternalId(id: string) {
|
|
246
|
+
throw Error('Not implemented')
|
|
247
|
+
},
|
|
248
|
+
blockTokenById(id: string, reason: string) {
|
|
249
|
+
throw Error('Not implemented')
|
|
250
|
+
},
|
|
251
|
+
unblockTokenById(id: string) {
|
|
252
|
+
throw Error('Not implemented')
|
|
253
|
+
},
|
|
254
|
+
countQuery(data: any) {
|
|
255
|
+
throw Error('Not implemented')
|
|
256
|
+
},
|
|
257
|
+
findQuery(data: any) {
|
|
258
|
+
throw Error('Not implemented')
|
|
259
|
+
},
|
|
260
|
+
removeTokenById(id: string) {
|
|
261
|
+
throw Error('Not implemented')
|
|
262
|
+
}
|
|
263
|
+
} as TokenManagement,
|
|
214
264
|
...decorators
|
|
215
265
|
}
|
|
216
266
|
|
|
@@ -243,13 +293,15 @@ export {
|
|
|
243
293
|
FastifyReply,
|
|
244
294
|
FastifyRequest,
|
|
245
295
|
AuthenticatedUser,
|
|
296
|
+
AuthenticatedToken,
|
|
246
297
|
Role,
|
|
247
298
|
Data,
|
|
248
299
|
Roles,
|
|
249
300
|
Route,
|
|
250
301
|
RouteConfig,
|
|
251
302
|
ConfiguredRoute,
|
|
252
|
-
UserManagement
|
|
303
|
+
UserManagement,
|
|
304
|
+
TokenManagement
|
|
253
305
|
} from './types/global'
|
|
254
306
|
|
|
255
307
|
/**
|
|
@@ -46,7 +46,7 @@ export async function unregister(req: FastifyRequest, reply: FastifyReply) {
|
|
|
46
46
|
return reply.status(403).send(Error('User blocked'))
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
user = await req.server['userManager'].disableUserById(user
|
|
49
|
+
user = await req.server['userManager'].disableUserById(user.getId())
|
|
50
50
|
isValid = await req.server['userManager'].isValidUser(user)
|
|
51
51
|
|
|
52
52
|
if (!isValid) {
|
|
@@ -56,6 +56,21 @@ export async function unregister(req: FastifyRequest, reply: FastifyReply) {
|
|
|
56
56
|
return { ok: true }
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
export async function validatePassword(req: FastifyRequest, reply: FastifyReply) {
|
|
60
|
+
const { password } = req.data()
|
|
61
|
+
|
|
62
|
+
if (!password) {
|
|
63
|
+
return reply.status(400).send(Error('Password cannot be null'))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const match = regExp.password.test(password)
|
|
67
|
+
if (!match) {
|
|
68
|
+
return reply.status(400).send(Error('Password is not valid'))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { ok: match }
|
|
72
|
+
}
|
|
73
|
+
|
|
59
74
|
export async function changePassword(req: FastifyRequest, reply: FastifyReply) {
|
|
60
75
|
const { email, oldPassword, newPassword1, newPassword2 } = req.data()
|
|
61
76
|
|
|
@@ -90,16 +105,20 @@ export async function forgotPassword(req: FastifyRequest, reply: FastifyReply) {
|
|
|
90
105
|
return reply.status(400).send(Error('Missing a valid user identifier'))
|
|
91
106
|
}
|
|
92
107
|
|
|
93
|
-
let user =
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
let user = null as any
|
|
109
|
+
if (email) {
|
|
110
|
+
user = await req.server['userManager'].retrieveUserByEmail(email)
|
|
111
|
+
} else if (username) {
|
|
112
|
+
user = await req.server['userManager'].retrieveUserByUsername(username)
|
|
113
|
+
}
|
|
114
|
+
|
|
96
115
|
let isValid = await req.server['userManager'].isValidUser(user)
|
|
97
116
|
|
|
98
117
|
if (!isValid) {
|
|
99
118
|
return reply.status(403).send(Error('Wrong credentials'))
|
|
100
119
|
}
|
|
101
120
|
|
|
102
|
-
if (user
|
|
121
|
+
if (user?.blocked) {
|
|
103
122
|
return reply.status(403).send(Error('User blocked'))
|
|
104
123
|
}
|
|
105
124
|
|
|
@@ -116,9 +135,7 @@ export async function confirmEmail(req: FastifyRequest, reply: FastifyReply) {
|
|
|
116
135
|
return reply.status(400).send(Error('Missing the confirm email token'))
|
|
117
136
|
}
|
|
118
137
|
|
|
119
|
-
let user = await
|
|
120
|
-
where: { confirmationToken: code }
|
|
121
|
-
})
|
|
138
|
+
let user = await req.server['userManager'].retrieveUserByConfirmationToken(code)
|
|
122
139
|
let isValid = await req.server['userManager'].isValidUser(user)
|
|
123
140
|
|
|
124
141
|
if (!isValid) {
|
|
@@ -146,9 +163,7 @@ export async function resetPassword(req: FastifyRequest, reply: FastifyReply) {
|
|
|
146
163
|
return reply.status(400).send(Error('Repeated new password not match'))
|
|
147
164
|
}
|
|
148
165
|
|
|
149
|
-
let user = await
|
|
150
|
-
where: { resetPasswordToken: code }
|
|
151
|
-
})
|
|
166
|
+
let user = await req.server['userManager'].retrieveUserByResetPasswordToken(code)
|
|
152
167
|
let isValid = await req.server['userManager'].isValidUser(user)
|
|
153
168
|
|
|
154
169
|
if (!isValid) {
|
|
@@ -181,6 +196,10 @@ export async function login(req: FastifyRequest, reply: FastifyReply) {
|
|
|
181
196
|
return reply.status(403).send(Error('Wrong credentials'))
|
|
182
197
|
}
|
|
183
198
|
|
|
199
|
+
if (!(user.confirmed === true)) {
|
|
200
|
+
return reply.status(403).send(Error('User email unconfirmed'))
|
|
201
|
+
}
|
|
202
|
+
|
|
184
203
|
if (user.blocked) {
|
|
185
204
|
return reply.status(403).send(Error('User blocked'))
|
|
186
205
|
}
|
|
@@ -197,11 +216,11 @@ export async function login(req: FastifyRequest, reply: FastifyReply) {
|
|
|
197
216
|
|
|
198
217
|
export async function invalidateTokens(req: FastifyRequest, reply: FastifyReply) {
|
|
199
218
|
let isValid = await req.server['userManager'].isValidUser(req.user)
|
|
200
|
-
if (!isValid) {
|
|
219
|
+
if (!req.user || !isValid) {
|
|
201
220
|
return reply.status(403).send(Error('User not linked'))
|
|
202
221
|
}
|
|
203
222
|
|
|
204
|
-
const user = await req.server['userManager'].resetExternalId(req.user
|
|
223
|
+
const user = await req.server['userManager'].resetExternalId(req.user.getId())
|
|
205
224
|
isValid = await req.server['userManager'].isValidUser(user)
|
|
206
225
|
return { ok: isValid }
|
|
207
226
|
}
|
|
@@ -215,7 +234,7 @@ export async function block(req: FastifyRequest, reply: FastifyReply) {
|
|
|
215
234
|
const { reason } = req.data()
|
|
216
235
|
|
|
217
236
|
const user = await req.server['userManager'].blockUserById(userId, reason)
|
|
218
|
-
return { ok: !!user
|
|
237
|
+
return { ok: !!user.getId() }
|
|
219
238
|
}
|
|
220
239
|
|
|
221
240
|
export async function unblock(req: FastifyRequest, reply: FastifyReply) {
|
|
@@ -227,5 +246,5 @@ export async function unblock(req: FastifyRequest, reply: FastifyReply) {
|
|
|
227
246
|
|
|
228
247
|
const { id: userId } = req.parameters()
|
|
229
248
|
const user = await req.server['userManager'].unblockUserById(userId)
|
|
230
|
-
return { ok: !!user
|
|
249
|
+
return { ok: !!user.getId() }
|
|
231
250
|
}
|