@thzero/library_server_koa 0.15.32
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/README.md +15 -0
- package/boot/index.js +182 -0
- package/boot/plugins/admin/news.js +11 -0
- package/boot/plugins/admin/users.js +11 -0
- package/boot/plugins/api.js +16 -0
- package/boot/plugins/apiFront.js +11 -0
- package/boot/plugins/news.js +11 -0
- package/boot/plugins/users.js +11 -0
- package/boot/plugins/usersExtended.js +11 -0
- package/license.md +9 -0
- package/middleware/authentication.js +78 -0
- package/middleware/authorization.js +191 -0
- package/package.json +43 -0
- package/routes/admin/index.js +123 -0
- package/routes/admin/news.js +22 -0
- package/routes/admin/users.js +26 -0
- package/routes/baseNews.js +40 -0
- package/routes/baseUsers.js +123 -0
- package/routes/home.js +28 -0
- package/routes/index.js +21 -0
- package/routes/news.js +6 -0
- package/routes/plans.js +41 -0
- package/routes/users.js +6 -0
- package/routes/utility.js +51 -0
- package/routes/version.js +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
# library_server_koa
|
|
6
|
+
|
|
7
|
+
An opinionated library of common functionality to bootstrap a Koa based API application.
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
[](https://npmjs.org/package/@thzero/library_server_koa)
|
|
12
|
+
|
|
13
|
+
### Requirements
|
|
14
|
+
|
|
15
|
+
#### library_server
|
package/boot/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
|
|
3
|
+
import Koa from 'koa';
|
|
4
|
+
import koaCors from '@koa/cors';
|
|
5
|
+
import koaHelmet from 'koa-helmet';
|
|
6
|
+
import koaStatic from 'koa-static';
|
|
7
|
+
|
|
8
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
9
|
+
import Utility from '@thzero/library_common/utility';
|
|
10
|
+
|
|
11
|
+
import TokenExpiredError from '@thzero/library_server/errors/tokenExpired';
|
|
12
|
+
|
|
13
|
+
import injector from '@thzero/library_common/utility/injector';
|
|
14
|
+
|
|
15
|
+
import BootMain from '@thzero/library_server/boot';
|
|
16
|
+
|
|
17
|
+
const ResponseTime = 'X-Response-Time';
|
|
18
|
+
|
|
19
|
+
class KoaBootMain extends BootMain {
|
|
20
|
+
async _initApp(args, plugins) {
|
|
21
|
+
const app = new Koa();
|
|
22
|
+
|
|
23
|
+
// https://github.com/koajs/cors
|
|
24
|
+
app.use(koaCors({
|
|
25
|
+
allowMethods: 'GET,POST,DELETE',
|
|
26
|
+
maxAge : 7200,
|
|
27
|
+
allowHeaders: `${LibraryConstants.Headers.AuthKeys.API}, ${LibraryConstants.Headers.AuthKeys.AUTH}, ${LibraryConstants.Headers.CorrelationId}, Content-Type`,
|
|
28
|
+
credentials: true,
|
|
29
|
+
origin: '*'
|
|
30
|
+
}));
|
|
31
|
+
// https://www.npmjs.com/package/koa-helmet
|
|
32
|
+
app.use(koaHelmet());
|
|
33
|
+
|
|
34
|
+
// error
|
|
35
|
+
app.use(async (ctx, next) => {
|
|
36
|
+
try {
|
|
37
|
+
await next();
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
ctx.status = err.status || 500;
|
|
41
|
+
if (err instanceof TokenExpiredError) {
|
|
42
|
+
ctx.status = 401;
|
|
43
|
+
ctx.response.header['WWW-Authenticate'] = 'Bearer error="invalid_token", error_description="The access token expired"'
|
|
44
|
+
}
|
|
45
|
+
ctx.app.emit('error', err, ctx);
|
|
46
|
+
await this.usageMetricsServiceI.register(ctx, err).catch(() => {
|
|
47
|
+
this.loggerServiceI.exception('KoaBootMain', 'start', err);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
app.on('error', (err, ctx) => {
|
|
53
|
+
this.loggerServiceI.error('KoaBootMain', 'start', 'Uncaught Exception', err);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// config
|
|
57
|
+
app.use(async (ctx, next) => {
|
|
58
|
+
ctx.config = this._appConfig;
|
|
59
|
+
await next();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// correlationId
|
|
63
|
+
app.use(async (ctx, next) => {
|
|
64
|
+
ctx.correlationId = ctx.request.header[LibraryConstants.Headers.CorrelationId]
|
|
65
|
+
await next();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// logger
|
|
69
|
+
app.use(async (ctx, next) => {
|
|
70
|
+
await next();
|
|
71
|
+
const rt = ctx.response.get(ResponseTime);
|
|
72
|
+
this.loggerServiceI.info2(`${ctx.method} ${ctx.url} - ${rt}`);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// x-response-time
|
|
76
|
+
app.use(async (ctx, next) => {
|
|
77
|
+
const start = Utility.timerStart();
|
|
78
|
+
await next();
|
|
79
|
+
const delta = Utility.timerStop(start, true);
|
|
80
|
+
ctx.set(ResponseTime, delta);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
app.use(koaStatic('./public'));
|
|
84
|
+
|
|
85
|
+
this._initPreAuth(app);
|
|
86
|
+
|
|
87
|
+
// auth-api-token
|
|
88
|
+
app.use(async (ctx, next) => {
|
|
89
|
+
if (ctx.originalUrl === '/favicon.ico') {
|
|
90
|
+
await next();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const key = ctx.get(LibraryConstants.Headers.AuthKeys.API);
|
|
95
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
96
|
+
if (!String.isNullOrEmpty(key)) {
|
|
97
|
+
const auth = ctx.config.get('auth');
|
|
98
|
+
if (auth) {
|
|
99
|
+
const apiKey = auth.apiKey;
|
|
100
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
101
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
102
|
+
if (key === apiKey) {
|
|
103
|
+
ctx.state.apiKey = key;
|
|
104
|
+
await next();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
(async () => {
|
|
111
|
+
const usageMetrics = {};
|
|
112
|
+
usageMetrics.url = ctx.request.path;
|
|
113
|
+
usageMetrics.correlationId = ctx.correlationId;
|
|
114
|
+
usageMetrics.href = ctx.request.href;
|
|
115
|
+
usageMetrics.headers = ctx.request.headers;
|
|
116
|
+
usageMetrics.host = ctx.request.host;
|
|
117
|
+
usageMetrics.hostname = ctx.request.hostname;
|
|
118
|
+
usageMetrics.querystring = ctx.request.querystring;
|
|
119
|
+
usageMetrics.type = ctx.request.type;
|
|
120
|
+
usageMetrics.token = ctx.request.token;
|
|
121
|
+
await this.usageMetricsServiceI.register(usageMetrics).catch((err) => {
|
|
122
|
+
this.loggerServiceI.error('KoaBootMain', 'start', 'usageMetrics', err);
|
|
123
|
+
});
|
|
124
|
+
})();
|
|
125
|
+
|
|
126
|
+
console.log('Unauthorized... auth-api-token failure');
|
|
127
|
+
ctx.throw(401);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
this._initPostAuth(app);
|
|
131
|
+
|
|
132
|
+
// usage metrics
|
|
133
|
+
app.use(async (ctx, next) => {
|
|
134
|
+
await next();
|
|
135
|
+
await this.usageMetricsServiceI.register(ctx).catch((err) => {
|
|
136
|
+
this.loggerServiceI.error('KoaBootMain', 'start', 'usageMetrics', err);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
this._routes = [];
|
|
141
|
+
|
|
142
|
+
this._initPreRoutes(app);
|
|
143
|
+
|
|
144
|
+
for (const pluginRoute of plugins)
|
|
145
|
+
await pluginRoute.initRoutes(this._routes);
|
|
146
|
+
|
|
147
|
+
await this._initRoutes();
|
|
148
|
+
|
|
149
|
+
console.log();
|
|
150
|
+
|
|
151
|
+
for (const route of this._routes) {
|
|
152
|
+
await route.init(injector, app, this._appConfig);
|
|
153
|
+
app
|
|
154
|
+
.use(route.router.routes())
|
|
155
|
+
.use(route.router.allowedMethods());
|
|
156
|
+
|
|
157
|
+
console.log([ route.id ]);
|
|
158
|
+
|
|
159
|
+
for (let i = 0; i < route.router.stack.length; i++)
|
|
160
|
+
console.log([ route.router.stack[i].path, route.router.stack[i].methods ]);
|
|
161
|
+
|
|
162
|
+
console.log();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const serverHttp = http.createServer(app.callback());
|
|
166
|
+
return { app: app, server: serverHttp, listen: serverHttp.listen };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_initAppListen(app, server, port, err) {
|
|
170
|
+
server.listen(port, err);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async _initAppPost(app, args) {
|
|
174
|
+
this._initPostRoutes(app);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
_initRoute(route) {
|
|
178
|
+
this._routes.push(route);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export default KoaBootMain;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseNewsAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/news';
|
|
2
|
+
|
|
3
|
+
import adminNewsRoute from '../../../routes/admin/news';
|
|
4
|
+
|
|
5
|
+
class NewsAdminBootPlugin extends BaseNewsAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminNews() {
|
|
7
|
+
return new adminNewsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsAdminBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseUsersAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/users';
|
|
2
|
+
|
|
3
|
+
import adminUsersRoute from '../../../routes/admin/users'
|
|
4
|
+
|
|
5
|
+
class UsersAdminBootPlugin extends BaseUsersAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminUsers() {
|
|
7
|
+
return new adminUsersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersAdminBootPlugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import BaseApiBootPlugin from '@thzero/library_server/boot/plugins/api';
|
|
2
|
+
|
|
3
|
+
import homeRoute from '../../routes/home';
|
|
4
|
+
import versionRoute from '../../routes/version';
|
|
5
|
+
|
|
6
|
+
class ApiBootPlugin extends BaseApiBootPlugin {
|
|
7
|
+
_initRoutesHome() {
|
|
8
|
+
return new homeRoute();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
_initRoutesVersion() {
|
|
12
|
+
return new versionRoute();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default ApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseApiFrontBootPlugin from '@thzero/library_server/boot/plugins/apiFront';
|
|
2
|
+
|
|
3
|
+
import utilityRoute from '../../routes/utility';
|
|
4
|
+
|
|
5
|
+
class FrontApiBootPlugin extends BaseApiFrontBootPlugin {
|
|
6
|
+
_initRoutesUtility() {
|
|
7
|
+
return new utilityRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default FrontApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseNewsApiBootPlugin from '@thzero/library_server/boot/plugins/news';
|
|
2
|
+
|
|
3
|
+
import newsRoute from '../../routes/news';
|
|
4
|
+
|
|
5
|
+
class NewsApiBootPlugin extends BaseNewsApiBootPlugin {
|
|
6
|
+
_initRoutesNews() {
|
|
7
|
+
return new newsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseUsersApiBootPlugin from '@thzero/library_server/boot/plugins/users';
|
|
2
|
+
|
|
3
|
+
import usersRoute from '../../routes/users';
|
|
4
|
+
|
|
5
|
+
class UsersApiBootPlugin extends BaseUsersApiBootPlugin {
|
|
6
|
+
_initRoutesUsers() {
|
|
7
|
+
return new usersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseExtendedUsersApiBootPlugin from '@thzero/library_server/boot/plugins/usersExtended';
|
|
2
|
+
|
|
3
|
+
import plansService from '../../service/plans';
|
|
4
|
+
|
|
5
|
+
class ExtendedUsersApiBootPlugin extends BaseExtendedUsersApiBootPlugin {
|
|
6
|
+
_initServicesPlans() {
|
|
7
|
+
return new plansService();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default ExtendedUsersApiBootPlugin;
|
package/license.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2022 thZero.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
|
|
3
|
+
|
|
4
|
+
import injector from '@thzero/library_common/utility/injector';
|
|
5
|
+
|
|
6
|
+
const separator = ': ';
|
|
7
|
+
|
|
8
|
+
function getAuthToken(context) {
|
|
9
|
+
if (!context)
|
|
10
|
+
return null;
|
|
11
|
+
|
|
12
|
+
const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
13
|
+
const token = context.get(LibraryConstants.Headers.AuthKeys.AUTH);
|
|
14
|
+
logger.debug('middleware', 'getAuthToken', 'token', token, context.correlationId);
|
|
15
|
+
const split = token.split(LibraryConstants.Headers.AuthKeys.AUTH_BEARER + separator);
|
|
16
|
+
logger.debug('middleware', 'getAuthToken', 'split', split, context.correlationId);
|
|
17
|
+
logger.debug('middleware', 'getAuthToken', 'split.length', split.length, context.correlationId);
|
|
18
|
+
if (split.length > 1)
|
|
19
|
+
return split[1];
|
|
20
|
+
|
|
21
|
+
logger.debug('middleware', 'getAuthToken', 'fail', null, context.correlationId);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const authentication = (required) => {
|
|
26
|
+
return async (ctx, next) => {
|
|
27
|
+
const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
28
|
+
|
|
29
|
+
const token = getAuthToken(ctx);
|
|
30
|
+
logger.debug('middleware', 'authentication', 'token', token, ctx.correlationId);
|
|
31
|
+
logger.debug('middleware', 'authentication', 'required', required, ctx.correlationId);
|
|
32
|
+
const valid = ((required && !String.isNullOrEmpty(token)) || !required);
|
|
33
|
+
logger.debug('middleware', 'authentication', 'valid', valid, ctx.correlationId);
|
|
34
|
+
if (valid) {
|
|
35
|
+
if (!String.isNullOrEmpty(token)) {
|
|
36
|
+
const service = injector.getService(LibraryConstants.InjectorKeys.SERVICE_AUTH);
|
|
37
|
+
const results = await service.verifyToken(ctx.correlationId, token);
|
|
38
|
+
logger.debug('middleware', 'authentication', 'results', results, ctx.correlationId);
|
|
39
|
+
if (!results || !results.success) {
|
|
40
|
+
logger.warn('middleware', 'authentication', 'Unauthenticated... invalid token', null, ctx.correlationId);
|
|
41
|
+
ctx.throw(401);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
ctx.state.token = token;
|
|
46
|
+
ctx.state.user = results.user;
|
|
47
|
+
ctx.state.claims = results.claims;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await next();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
(async () => {
|
|
55
|
+
const usageMetrics = {
|
|
56
|
+
url: ctx.request.path,
|
|
57
|
+
correlationId: ctx.correlationId,
|
|
58
|
+
href: ctx.request.href,
|
|
59
|
+
headers: ctx.request.headers,
|
|
60
|
+
host: ctx.request.host,
|
|
61
|
+
hostname: ctx.request.hostname,
|
|
62
|
+
querystring: ctx.request.querystring,
|
|
63
|
+
type: ctx.request.type,
|
|
64
|
+
token: ctx.request.token
|
|
65
|
+
};
|
|
66
|
+
const serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
67
|
+
await serviceUsageMetrics.register(usageMetrics).catch((err) => {
|
|
68
|
+
const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
69
|
+
logger.error('middleware', 'authentication', err, null, ctx.correlationId);
|
|
70
|
+
});
|
|
71
|
+
})();
|
|
72
|
+
|
|
73
|
+
logger.warn('middleware', 'authentication', 'Unauthorized... authentication unknown', null, ctx.correlationId);
|
|
74
|
+
ctx.throw(401);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export default authentication;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
|
|
3
|
+
|
|
4
|
+
import injector from '@thzero/library_common/utility/injector';
|
|
5
|
+
|
|
6
|
+
// require('../utility/string.cjs');
|
|
7
|
+
String.isNullOrEmpty = function(value) {
|
|
8
|
+
//return !(typeof value === 'string' && value.length > 0)
|
|
9
|
+
return !value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
String.isString = function(value) {
|
|
13
|
+
return (typeof value === "string" || value instanceof String);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
String.trim = function(value) {
|
|
17
|
+
if (!value || !String.isString(value))
|
|
18
|
+
return value;
|
|
19
|
+
return value.trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const logicalAnd = 'and';
|
|
23
|
+
const logicalOr = 'or';
|
|
24
|
+
|
|
25
|
+
const authorizationCheckClaims = async (ctx, success, logical, security, logger) => {
|
|
26
|
+
if (!ctx)
|
|
27
|
+
return false;
|
|
28
|
+
if (!(ctx.state.claims && Array.isArray(ctx.state.claims)))
|
|
29
|
+
return false;
|
|
30
|
+
|
|
31
|
+
let result;
|
|
32
|
+
let roleAct;
|
|
33
|
+
let roleObj;
|
|
34
|
+
let roleParts;
|
|
35
|
+
for (const claim of ctx.state.claims) {
|
|
36
|
+
logger.debug('middleware', 'authorization', 'authorization.claim', claim, ctx.correlationId);
|
|
37
|
+
|
|
38
|
+
for (const role of ctx.state.roles) {
|
|
39
|
+
logger.debug('middleware', 'authorization', 'role', role, ctx.correlationId);
|
|
40
|
+
|
|
41
|
+
roleParts = role.split('.');
|
|
42
|
+
if (roleParts && roleParts.length < 1)
|
|
43
|
+
success = false;
|
|
44
|
+
|
|
45
|
+
roleObj = roleParts[0];
|
|
46
|
+
roleAct = roleParts.length >= 2 ? roleParts[1] : null
|
|
47
|
+
|
|
48
|
+
result = await security.validate(claim, null, roleObj, roleAct);
|
|
49
|
+
logger.debug('middleware', 'authorization', 'result', result, ctx.correlationId);
|
|
50
|
+
if (logical === logicalOr)
|
|
51
|
+
success = success || result;
|
|
52
|
+
else
|
|
53
|
+
success = success && result;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return success;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const authorizationCheckRoles = async (ctx, success, logical, security, logger) => {
|
|
61
|
+
if (!ctx)
|
|
62
|
+
return false;
|
|
63
|
+
|
|
64
|
+
logger.debug('middleware', 'authorizationCheckRoles', 'user', ctx.state.user, ctx.correlationId);
|
|
65
|
+
if (!(ctx.state.user && ctx.state.user.roles && Array.isArray(ctx.state.user.roles)))
|
|
66
|
+
return false;
|
|
67
|
+
|
|
68
|
+
logger.debug('middleware', 'authorizationCheckRoles', 'logical', logical, ctx.correlationId);
|
|
69
|
+
|
|
70
|
+
let result;
|
|
71
|
+
let roleAct;
|
|
72
|
+
let roleObj;
|
|
73
|
+
let roleParts;
|
|
74
|
+
for (const userRole of ctx.state.user.roles) {
|
|
75
|
+
logger.debug('middleware', 'authorizationCheckRoles', 'userRole', userRole, ctx.correlationId);
|
|
76
|
+
|
|
77
|
+
for (const role of ctx.state.roles) {
|
|
78
|
+
logger.debug('middleware', 'authorizationCheckRoles', 'role', role, ctx.correlationId);
|
|
79
|
+
|
|
80
|
+
roleParts = role.split('.');
|
|
81
|
+
if (roleParts && roleParts.length < 1)
|
|
82
|
+
success = false;
|
|
83
|
+
|
|
84
|
+
roleObj = roleParts[0];
|
|
85
|
+
roleAct = roleParts.length >= 2 ? roleParts[1] : null
|
|
86
|
+
|
|
87
|
+
result = await security.validate(userRole, null, roleObj, roleAct);
|
|
88
|
+
logger.debug('middleware', 'authorizationCheckRoles', 'result', result, ctx.correlationId);
|
|
89
|
+
if (logical === logicalOr) {
|
|
90
|
+
if (result)
|
|
91
|
+
return result;
|
|
92
|
+
|
|
93
|
+
success = false;
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
success = success && result;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return success;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const initalizeRoles = (ctx, roles, logger) => {
|
|
104
|
+
if (Array.isArray(roles)) {
|
|
105
|
+
// logger.debug('middleware', 'initalizeRoles', 'roles1a', roles);
|
|
106
|
+
ctx.state.roles = roles;
|
|
107
|
+
}
|
|
108
|
+
else if ((typeof(roles) === 'string') || (roles instanceof String)) {
|
|
109
|
+
// logger.debug('middleware', 'initalizeRoles', 'roles1b', roles);
|
|
110
|
+
ctx.state.roles = roles.split(',');
|
|
111
|
+
ctx.state.roles.map(item => item ? item.trim() : item);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const authorization = (roles, logical) => {
|
|
116
|
+
if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
|
|
117
|
+
logical = logicalOr;
|
|
118
|
+
|
|
119
|
+
return async (ctx, next) => {
|
|
120
|
+
const config = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
|
|
121
|
+
const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
122
|
+
const security = injector.getService(LibraryConstants.InjectorKeys.SERVICE_SECURITY);
|
|
123
|
+
|
|
124
|
+
// logger.debug('token', ctx.state.token);
|
|
125
|
+
logger.debug('middleware', 'authorization', 'user', ctx.state.user, ctx.correlationId);
|
|
126
|
+
logger.debug('middleware', 'authorization', 'claims', ctx.state.claims, ctx.correlationId);
|
|
127
|
+
logger.debug('middleware', 'authorization', 'roles1', roles, ctx.correlationId);
|
|
128
|
+
ctx.state.roles = [];
|
|
129
|
+
if (roles) {
|
|
130
|
+
// logger.debug('authorization.roles1', roles);
|
|
131
|
+
// logger.debug('authorization.roles1', (typeof roles));
|
|
132
|
+
// logger.debug('authorization.roles1', Array.isArray(roles));
|
|
133
|
+
// logger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
|
|
134
|
+
// if (Array.isArray(roles)) {
|
|
135
|
+
// // logger.debug('authorization.roles1a', roles);
|
|
136
|
+
// ctx.state.roles = roles;
|
|
137
|
+
// }
|
|
138
|
+
// else if ((typeof(roles) === 'string') || (roles instanceof String)) {
|
|
139
|
+
// // logger.debug('authorization.roles1b', roles);
|
|
140
|
+
// ctx.state.roles = roles.split(',');
|
|
141
|
+
// ctx.state.roles.map(item => item ? item.trim() : item);
|
|
142
|
+
// }
|
|
143
|
+
initalizeRoles(ctx, roles, logger);
|
|
144
|
+
}
|
|
145
|
+
logger.debug('middleware', 'authorization', 'roles2', ctx.state.roles, ctx.correlationId);
|
|
146
|
+
|
|
147
|
+
let success = false; //(logical === logicalOr ? false : true);
|
|
148
|
+
if (ctx.state.roles && Array.isArray(ctx.state.roles) && (ctx.state.roles.length > 0)) {
|
|
149
|
+
const auth = config.get('auth');
|
|
150
|
+
if (auth) {
|
|
151
|
+
logger.debug('middleware', 'authorization', 'auth.claims', auth.claims, ctx.correlationId);
|
|
152
|
+
logger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, ctx.correlationId);
|
|
153
|
+
}
|
|
154
|
+
if (auth && auth.claims && auth.claims.check)
|
|
155
|
+
success = await authorizationCheckClaims(ctx, (logical === logicalOr ? false : true), logical, security, logger);
|
|
156
|
+
|
|
157
|
+
if (!success)
|
|
158
|
+
success = await authorizationCheckRoles(ctx, (logical === logicalOr ? false : true), logical, security, logger);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
logger.debug('middleware', 'authorization', 'success', null, ctx.state.success, ctx.correlationId);
|
|
162
|
+
if (success) {
|
|
163
|
+
await next();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
(async () => {
|
|
168
|
+
const serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
169
|
+
const usageMetrics = {
|
|
170
|
+
url: ctx.request.path,
|
|
171
|
+
correlationId: ctx.correlationId,
|
|
172
|
+
href: ctx.request.href,
|
|
173
|
+
headers: ctx.request.headers,
|
|
174
|
+
host: ctx.request.host,
|
|
175
|
+
hostname: ctx.request.hostname,
|
|
176
|
+
querystring: ctx.request.querystring,
|
|
177
|
+
type: ctx.request.type,
|
|
178
|
+
token: ctx.request.token
|
|
179
|
+
};
|
|
180
|
+
await serviceUsageMetrics.register(usageMetrics).catch((err) => {
|
|
181
|
+
const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
182
|
+
logger.error(null, err);
|
|
183
|
+
});
|
|
184
|
+
})();
|
|
185
|
+
|
|
186
|
+
logger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, ctx.correlationId);
|
|
187
|
+
ctx.throw(401);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export default authorization;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thzero/library_server_koa",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.15.32",
|
|
5
|
+
"version_major": 0,
|
|
6
|
+
"version_minor": 15,
|
|
7
|
+
"version_patch": 32,
|
|
8
|
+
"version_date": "04/18/2022",
|
|
9
|
+
"description": "An opinionated library of common functionality to bootstrap a Koa based API application.",
|
|
10
|
+
"author": "thZero",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/thzero/library_server_koa.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/thzero/library_server_koa/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/thzero/library_server_koa#readme",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.8.3"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"cli-update": "./node_modules/.bin/library-cli --updateversion --pi",
|
|
25
|
+
"cli-update-w": ".\\node_modules\\.bin\\library-cli --updateversion --pi",
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@koa/cors": "^3.3.0",
|
|
30
|
+
"@koa/router": "^10.1.1",
|
|
31
|
+
"koa": "^2.13.4",
|
|
32
|
+
"koa-body": "^4.2.0",
|
|
33
|
+
"koa-helmet": "^6.1.0",
|
|
34
|
+
"koa-static": "^5.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@thzero/library_cli": "^0.15"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@thzero/library_common": "^0.15",
|
|
41
|
+
"@thzero/library_common_service": "^0.15"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from '@thzero/library_server/routes/index';
|
|
6
|
+
|
|
7
|
+
import authentication from '../../middleware/authentication';
|
|
8
|
+
import authorization from '../../middleware/authorization';
|
|
9
|
+
|
|
10
|
+
class AdminBaseRoute extends BaseRoute {
|
|
11
|
+
constructor(urlFragment, role, serviceKey) {
|
|
12
|
+
if (!urlFragment)
|
|
13
|
+
throw Error('Invalid url fragment');
|
|
14
|
+
|
|
15
|
+
super(`/admin/${urlFragment}`);
|
|
16
|
+
|
|
17
|
+
this._options = {
|
|
18
|
+
role: role,
|
|
19
|
+
serviceKey: serviceKey
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// this._service = null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async init(injector, config) {
|
|
26
|
+
const router = await super.init(injector, config);
|
|
27
|
+
router.service = injector.getService(this._options.serviceKey);
|
|
28
|
+
// this._service = injector.getService(this._options.serviceKey);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_allowsCreate() {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_allowsDelete() {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_allowsUpdate() {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_initializeRoutesCreate(router) {
|
|
44
|
+
const self = this;
|
|
45
|
+
router.post('/',
|
|
46
|
+
authentication(true),
|
|
47
|
+
authorization([ `${self._options.role}.create` ]),
|
|
48
|
+
koaBody({
|
|
49
|
+
text: false,
|
|
50
|
+
}),
|
|
51
|
+
// eslint-disable-next-line
|
|
52
|
+
async (ctx, next) => {
|
|
53
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
54
|
+
// const response = (await router.service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
55
|
+
const response = (await ctx.router.service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
56
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_initializeRoutesDelete(router) {
|
|
62
|
+
const self = this;
|
|
63
|
+
router.delete('/:id',
|
|
64
|
+
authentication(true),
|
|
65
|
+
authorization([ `${self._options.role}.delete` ]),
|
|
66
|
+
// eslint-disable-next-line
|
|
67
|
+
async (ctx, next) => {
|
|
68
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
69
|
+
// const response = (await service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
|
|
70
|
+
const response = (await ctx.router.service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
|
|
71
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_initializeRoutesUpdate(router) {
|
|
77
|
+
const self = this;
|
|
78
|
+
router.post('/:id',
|
|
79
|
+
authentication(true),
|
|
80
|
+
authorization([ `${self._options.role}.update` ]),
|
|
81
|
+
koaBody({
|
|
82
|
+
text: false,
|
|
83
|
+
}),
|
|
84
|
+
// eslint-disable-next-line
|
|
85
|
+
async (ctx, next) => {
|
|
86
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
87
|
+
// const response = (await service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
|
|
88
|
+
const response = (await ctx.router.service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
|
|
89
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_initializeRoutes(router) {
|
|
95
|
+
if (this._allowsDelete)
|
|
96
|
+
this._initializeRoutesDelete(router);
|
|
97
|
+
|
|
98
|
+
router.post('/search',
|
|
99
|
+
authentication(true),
|
|
100
|
+
authorization([ `${this._options.role}.search` ]),
|
|
101
|
+
koaBody({
|
|
102
|
+
text: false,
|
|
103
|
+
}),
|
|
104
|
+
// eslint-disable-next-line
|
|
105
|
+
async (ctx, next) => {
|
|
106
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
107
|
+
// const response = (await service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
108
|
+
const response = (await ctx.router.service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
109
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
if (this._allowsUpdate())
|
|
114
|
+
this._initializeRoutesUpdate(router);
|
|
115
|
+
|
|
116
|
+
if (this._allowsCreate())
|
|
117
|
+
this._initializeRoutesCreate(router);
|
|
118
|
+
|
|
119
|
+
return router;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default AdminBaseRoute;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import AdminRoute from './index'
|
|
4
|
+
|
|
5
|
+
class NewsAdminRoute extends AdminRoute {
|
|
6
|
+
constructor(urlFragment, role, serviceKey) {
|
|
7
|
+
urlFragment = urlFragment ? urlFragment : 'news';
|
|
8
|
+
role = role ? role : 'news';
|
|
9
|
+
serviceKey = serviceKey ? serviceKey : LibraryConstants.InjectorKeys.SERVICE_ADMIN_NEWS;
|
|
10
|
+
super(urlFragment, role, serviceKey);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get id() {
|
|
14
|
+
return 'admin-news';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get _version() {
|
|
18
|
+
return 'v1';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default NewsAdminRoute;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import AdminRoute from './index'
|
|
4
|
+
|
|
5
|
+
class UsersAdminRoute extends AdminRoute {
|
|
6
|
+
constructor(urlFragment, role, serviceKey) {
|
|
7
|
+
urlFragment = urlFragment ? urlFragment : 'users';
|
|
8
|
+
role = role ? role : 'users';
|
|
9
|
+
serviceKey = serviceKey ? serviceKey : LibraryConstants.InjectorKeys.SERVICE_ADMIN_USERS;
|
|
10
|
+
super(urlFragment, role, serviceKey);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get id() {
|
|
14
|
+
return 'admin-users';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_allowsCreate() {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get _version() {
|
|
22
|
+
return 'v1';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default UsersAdminRoute;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
import authentication from '../middleware/authentication';
|
|
8
|
+
|
|
9
|
+
class BaseNewsRoute extends BaseRoute {
|
|
10
|
+
constructor(prefix, version) {
|
|
11
|
+
super(prefix ? prefix : '/news');
|
|
12
|
+
|
|
13
|
+
// this._serviceNews = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async init(injector, config) {
|
|
17
|
+
const router = await super.init(injector, app, config);
|
|
18
|
+
router.serviceNews = injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
19
|
+
// this._serviceNews = injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get id() {
|
|
23
|
+
return 'news';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
_initializeRoutes(router) {
|
|
27
|
+
router.get('/latest/:date',
|
|
28
|
+
authentication(false),
|
|
29
|
+
// eslint-disable-next-line
|
|
30
|
+
async (ctx, next) => {
|
|
31
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
32
|
+
// const response = (await service.latest(ctx.correlationId, ctx.state.user, parseInt(ctx.params.date))).check(ctx);
|
|
33
|
+
const response = (await ctx.router.serviceNews.latest(ctx.correlationId, ctx.state.user, parseInt(ctx.params.date))).check(ctx);
|
|
34
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default BaseNewsRoute;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
4
|
+
|
|
5
|
+
import Utility from '@thzero/library_common/utility';
|
|
6
|
+
|
|
7
|
+
import BaseRoute from './index';
|
|
8
|
+
|
|
9
|
+
import authentication from '../middleware/authentication';
|
|
10
|
+
import authorization from '../middleware/authorization';
|
|
11
|
+
|
|
12
|
+
class BaseUsersRoute extends BaseRoute {
|
|
13
|
+
constructor(prefix, version) {
|
|
14
|
+
super(prefix ? prefix : '/users');
|
|
15
|
+
|
|
16
|
+
// this._serviceUsers = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async init(injector, app, config) {
|
|
20
|
+
const router = await super.init(injector, app, config);
|
|
21
|
+
router.serviceUsers = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
22
|
+
// this._serviceUsers = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get id() {
|
|
26
|
+
return 'users';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_initializeRoutes(router) {
|
|
30
|
+
this._initializeRoutesGamerById(router);
|
|
31
|
+
this._initializeRoutesGamerByTag(router);
|
|
32
|
+
this._initializeRoutesRefreshSettings(router);
|
|
33
|
+
this._initializeRoutesUpdate(router);
|
|
34
|
+
this._initializeRoutesUpdateSettings(router);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_initializeRoutesGamerById(router) {
|
|
38
|
+
return router.get('/gamerId/:gamerId',
|
|
39
|
+
authentication(false),
|
|
40
|
+
// authorization('user'),
|
|
41
|
+
koaBody({
|
|
42
|
+
text: false,
|
|
43
|
+
}),
|
|
44
|
+
// eslint-disable-next-line
|
|
45
|
+
async (ctx, next) => {
|
|
46
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
47
|
+
// const response = (await service.fetchByGamerId(ctx.correlationId, ctx.params.gamerId)).check(ctx);
|
|
48
|
+
const response = (await ctx.router.serviceUsers.fetchByGamerId(ctx.correlationId, ctx.params.gamerId)).check(ctx);
|
|
49
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_initializeRoutesGamerByTag(router) {
|
|
55
|
+
return router.get('/gamerTag/:gamerTag',
|
|
56
|
+
authentication(false),
|
|
57
|
+
// authorization('user'),
|
|
58
|
+
koaBody({
|
|
59
|
+
text: false,
|
|
60
|
+
}),
|
|
61
|
+
// eslint-disable-next-line
|
|
62
|
+
async (ctx, next) => {
|
|
63
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
64
|
+
// const response = (await service.fetchByGamerTag(ctx.correlationId, ctx.params.gamerTag)).check(ctx);
|
|
65
|
+
const response = (await ctx.router.serviceUsers.fetchByGamerTag(ctx.correlationId, ctx.params.gamerTag)).check(ctx);
|
|
66
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_initializeRoutesRefreshSettings(router) {
|
|
72
|
+
return router.post('/refresh/settings',
|
|
73
|
+
authentication(true),
|
|
74
|
+
authorization('user'),
|
|
75
|
+
koaBody({
|
|
76
|
+
text: false,
|
|
77
|
+
}),
|
|
78
|
+
// eslint-disable-next-line
|
|
79
|
+
async (ctx, next) => {
|
|
80
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
81
|
+
// const response = (await service.refreshSettings(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
82
|
+
const response = (await ctx.router.serviceUsers.refreshSettings(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
83
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_initializeRoutesUpdate(router) {
|
|
89
|
+
return router.post('/update',
|
|
90
|
+
authentication(true),
|
|
91
|
+
authorization('user'),
|
|
92
|
+
koaBody({
|
|
93
|
+
text: false,
|
|
94
|
+
}),
|
|
95
|
+
// eslint-disable-next-line
|
|
96
|
+
async (ctx, next) => {
|
|
97
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
98
|
+
// const response = (await service.update(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
99
|
+
const response = (await ctx.router.serviceUsers.update(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
100
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_initializeRoutesUpdateSettings(router) {
|
|
106
|
+
return router.post('/update/settings',
|
|
107
|
+
authentication(true),
|
|
108
|
+
authorization('user'),
|
|
109
|
+
koaBody({
|
|
110
|
+
text: false,
|
|
111
|
+
}),
|
|
112
|
+
// eslint-disable-next-line
|
|
113
|
+
async (ctx, next) => {
|
|
114
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
115
|
+
// const response = (await service.updateSettings(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
116
|
+
const response = (await ctx.router.serviceUsers.updateSettings(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
117
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default BaseUsersRoute;
|
package/routes/home.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import BaseRoute from './index';
|
|
2
|
+
|
|
3
|
+
class HomeRoute extends BaseRoute {
|
|
4
|
+
constructor(prefix) {
|
|
5
|
+
super(prefix ? prefix : '');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get id() {
|
|
9
|
+
return 'home';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_initializeRoutes(router) {
|
|
13
|
+
// eslint-disable-next-line
|
|
14
|
+
router.get('/', (ctx, next) => {
|
|
15
|
+
ctx.status = 404;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get _ignoreApi() {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get _version() {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default HomeRoute;
|
package/routes/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import koaRouter from '@koa/router';
|
|
2
|
+
|
|
3
|
+
import BaseRoute from'@thzero/library_server/routes/index';
|
|
4
|
+
|
|
5
|
+
class KoaBaseRoute extends BaseRoute {
|
|
6
|
+
_initializeRouter(app, config) {
|
|
7
|
+
return new koaRouter({
|
|
8
|
+
prefix: this._prefix
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_jsonResponse(ctx, json) {
|
|
13
|
+
if (!ctx)
|
|
14
|
+
throw Error('Invalid context for response.');
|
|
15
|
+
|
|
16
|
+
ctx.type = 'application/json; charset=utf-8';
|
|
17
|
+
ctx.body = json;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default KoaBaseRoute;
|
package/routes/news.js
ADDED
package/routes/plans.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import LibraryConstants from '.@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
class PlansRoute extends BaseRoute {
|
|
8
|
+
constructor(prefix) {
|
|
9
|
+
super(prefix ? prefix : '/plans');
|
|
10
|
+
|
|
11
|
+
// this._servicePlans = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async init(injector, app, config) {
|
|
15
|
+
const router = await super.init(injector, app, config);
|
|
16
|
+
router.servicePlans = injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
17
|
+
// this._servicePlans = injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get id() {
|
|
21
|
+
return 'plans';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_initializeRoutes(router) {
|
|
25
|
+
router.get('/',
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
async (ctx, next) => {
|
|
28
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
29
|
+
// const response = (await service.servicePlans.listing(ctx.correlationId)).check(ctx);
|
|
30
|
+
const response = (await ctx.router.servicePlans.listing(ctx.correlationId)).check(ctx);
|
|
31
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get _version() {
|
|
37
|
+
return 'v1';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default PlansRoute;
|
package/routes/users.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
4
|
+
|
|
5
|
+
import Utility from '@thzero/library_common/utility';
|
|
6
|
+
|
|
7
|
+
import BaseRoute from './index';
|
|
8
|
+
|
|
9
|
+
import authentication from '../middleware/authentication';
|
|
10
|
+
// import authorization from '../middleware/authorization';
|
|
11
|
+
|
|
12
|
+
class UtilityRoute extends BaseRoute {
|
|
13
|
+
constructor(prefix) {
|
|
14
|
+
super(prefix ? prefix : '/utility');
|
|
15
|
+
|
|
16
|
+
// this._serviceUtility = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async init(injector, app, config) {
|
|
20
|
+
const router = await super.init(injector, app, config);
|
|
21
|
+
router.serviceUtility = injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
22
|
+
// this._serviceUtility = injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get id() {
|
|
26
|
+
return 'utility';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_initializeRoutes(router) {
|
|
30
|
+
router.post('/logger',
|
|
31
|
+
authentication(false),
|
|
32
|
+
// authorization('utility'),
|
|
33
|
+
// eslint-disable-next-line,
|
|
34
|
+
koaBody({
|
|
35
|
+
text: false,
|
|
36
|
+
}),
|
|
37
|
+
async (ctx, next) => {
|
|
38
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
39
|
+
// const response = (await service.logger(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
40
|
+
const response = (await ctx.router.serviceUtility.logger(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
41
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get _version() {
|
|
47
|
+
return 'v1';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default UtilityRoute;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
class VersionRoute extends BaseRoute {
|
|
8
|
+
constructor(prefix) {
|
|
9
|
+
super(prefix ? prefix : '');
|
|
10
|
+
|
|
11
|
+
// this._serviceVersion = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async init(injector, app, config) {
|
|
15
|
+
const router = await super.init(injector, app, config);
|
|
16
|
+
router.serviceVersion = injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
17
|
+
// this._serviceVersion = injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get id() {
|
|
21
|
+
return 'version';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_initializeRoutes(router) {
|
|
25
|
+
router.get('/version',
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
async (ctx, next) => {
|
|
28
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
29
|
+
// const response = (await service.version(ctx.correlationId)).check(ctx);
|
|
30
|
+
const response = (await ctx.router.serviceVersion.version(ctx.correlationId)).check(ctx);
|
|
31
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get _version() {
|
|
37
|
+
return 'v1';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default VersionRoute;
|