miolo 0.0.35 → 0.0.38
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/lib/config/defaults.js +19 -2
- package/lib/server/index.js +11 -3
- package/lib/server/middleware/auth/guest.js +83 -0
- package/lib/server/middleware/request.js +25 -4
- package/package.json +1 -1
- package/src/config/defaults.js +13 -1
- package/src/server/index.js +5 -0
- package/src/server/middleware/auth/guest.js +59 -0
- package/src/server/middleware/request.js +17 -3
package/lib/config/defaults.js
CHANGED
|
@@ -70,7 +70,19 @@ module.exports = {
|
|
|
70
70
|
body_field: undefined,
|
|
71
71
|
getUserId: ctx => {
|
|
72
72
|
try {
|
|
73
|
-
|
|
73
|
+
var from_pport = ctx.state.user.id;
|
|
74
|
+
|
|
75
|
+
if (from_pport != undefined) {
|
|
76
|
+
return from_pport;
|
|
77
|
+
}
|
|
78
|
+
} catch (e) {}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
var from_auth = ctx.user.id;
|
|
82
|
+
|
|
83
|
+
if (from_auth != undefined) {
|
|
84
|
+
return from_auth;
|
|
85
|
+
}
|
|
74
86
|
} catch (e) {}
|
|
75
87
|
|
|
76
88
|
var uid = ctx.headers['user-id'];
|
|
@@ -220,7 +232,8 @@ module.exports = {
|
|
|
220
232
|
to: 'devel@afialapis.com'
|
|
221
233
|
}
|
|
222
234
|
},
|
|
223
|
-
auth: {
|
|
235
|
+
auth: {
|
|
236
|
+
//basic: {
|
|
224
237
|
// auth_user: async (username, password) => { return {id: 1} },
|
|
225
238
|
// realm: '',
|
|
226
239
|
// paths: [],
|
|
@@ -235,6 +248,10 @@ module.exports = {
|
|
|
235
248
|
// url_login_redirect: undefined
|
|
236
249
|
// url_logout_redirect: '/'
|
|
237
250
|
//}
|
|
251
|
+
guest: {
|
|
252
|
+
make_guest_token: undefined // (session) => ''
|
|
253
|
+
|
|
254
|
+
}
|
|
238
255
|
},
|
|
239
256
|
middlewares: [// async (ctx) => {}
|
|
240
257
|
]
|
package/lib/server/index.js
CHANGED
|
@@ -55,7 +55,7 @@ function miolo(_x, _x2, _x3) {
|
|
|
55
55
|
|
|
56
56
|
function _miolo() {
|
|
57
57
|
_miolo = _asyncToGenerator(function* (sconfig, render, callback) {
|
|
58
|
-
var _config$http, _config$auth, _config$auth2;
|
|
58
|
+
var _config$http, _config$auth, _config$auth2, _config$auth3;
|
|
59
59
|
|
|
60
60
|
// Init some pieces
|
|
61
61
|
var config = (0, _config.init_config)(sconfig);
|
|
@@ -87,7 +87,15 @@ function _miolo() {
|
|
|
87
87
|
|
|
88
88
|
(0, _catch_js_error.init_route_catch_js_error)(app, '/sys/jserror'); // auth middleware
|
|
89
89
|
|
|
90
|
-
if (config !== null && config !== void 0 && (_config$auth = config.auth) !== null && _config$auth !== void 0 && _config$auth.
|
|
90
|
+
if (config !== null && config !== void 0 && (_config$auth = config.auth) !== null && _config$auth !== void 0 && _config$auth.guest) {
|
|
91
|
+
var {
|
|
92
|
+
init_guest_auth_middleware
|
|
93
|
+
} = require("./middleware/auth/guest");
|
|
94
|
+
|
|
95
|
+
init_guest_auth_middleware(app, config.auth.guest, config === null || config === void 0 ? void 0 : config.session, logger);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (config !== null && config !== void 0 && (_config$auth2 = config.auth) !== null && _config$auth2 !== void 0 && _config$auth2.basic) {
|
|
91
99
|
var {
|
|
92
100
|
init_basic_auth_middleware
|
|
93
101
|
} = require("./middleware/auth/basic");
|
|
@@ -95,7 +103,7 @@ function _miolo() {
|
|
|
95
103
|
init_basic_auth_middleware(app, config.auth.basic);
|
|
96
104
|
}
|
|
97
105
|
|
|
98
|
-
if (config !== null && config !== void 0 && (_config$
|
|
106
|
+
if (config !== null && config !== void 0 && (_config$auth3 = config.auth) !== null && _config$auth3 !== void 0 && _config$auth3.passport) {
|
|
99
107
|
var {
|
|
100
108
|
init_passport_auth_middleware
|
|
101
109
|
} = require("./middleware/auth/passport");
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.init_guest_auth_middleware = void 0;
|
|
7
|
+
|
|
8
|
+
var _jwtSimple = _interopRequireDefault(require("jwt-simple"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
13
|
+
|
|
14
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
15
|
+
|
|
16
|
+
function _guest_token_make_with_jwt(session, logger) {
|
|
17
|
+
var _session$options;
|
|
18
|
+
|
|
19
|
+
var buid = Math.random().toString();
|
|
20
|
+
var secret = session === null || session === void 0 ? void 0 : session.secret;
|
|
21
|
+
|
|
22
|
+
if (!secret) {
|
|
23
|
+
secret = 'miolo_unsafe_secret';
|
|
24
|
+
logger.error('Guest token made with an unsafe secret string. Please, configure your own through session.secret.');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var maxAge = session === null || session === void 0 ? void 0 : (_session$options = session.options) === null || _session$options === void 0 ? void 0 : _session$options.maxAge;
|
|
28
|
+
|
|
29
|
+
if (isNaN(maxAge)) {
|
|
30
|
+
maxAge = 86400;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var payload = {
|
|
34
|
+
admin: false,
|
|
35
|
+
buid: buid,
|
|
36
|
+
expires: Date.now() + maxAge
|
|
37
|
+
};
|
|
38
|
+
return _jwtSimple.default.encode(payload, secret);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
var init_guest_auth_middleware = (app, options, session, logger) => {
|
|
42
|
+
var _make_guest_token = () => {
|
|
43
|
+
try {
|
|
44
|
+
var {
|
|
45
|
+
make_guest_token
|
|
46
|
+
} = options;
|
|
47
|
+
|
|
48
|
+
if (make_guest_token != undefined) {
|
|
49
|
+
return make_guest_token(session || {});
|
|
50
|
+
}
|
|
51
|
+
} catch (_) {}
|
|
52
|
+
|
|
53
|
+
return _guest_token_make_with_jwt(session || {}, logger);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function guest_auth_middleware(_x, _x2) {
|
|
57
|
+
return _guest_auth_middleware.apply(this, arguments);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function _guest_auth_middleware() {
|
|
61
|
+
_guest_auth_middleware = _asyncToGenerator(function* (ctx, next) {
|
|
62
|
+
// Try to get our token from headers (server) or cookies (client)
|
|
63
|
+
var token = ctx.cookies.get('token') || ctx.headers['token'];
|
|
64
|
+
|
|
65
|
+
if (token) {
|
|
66
|
+
token = yield _make_guest_token();
|
|
67
|
+
logger.debug("Guest token conceeded");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
ctx.cookies.set('token', token);
|
|
71
|
+
ctx.user = {
|
|
72
|
+
name: 'guest',
|
|
73
|
+
token
|
|
74
|
+
};
|
|
75
|
+
yield next();
|
|
76
|
+
});
|
|
77
|
+
return _guest_auth_middleware.apply(this, arguments);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
app.use(guest_auth_middleware);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
exports.init_guest_auth_middleware = init_guest_auth_middleware;
|
|
@@ -31,8 +31,6 @@ function init_request_middleware(app) {
|
|
|
31
31
|
|
|
32
32
|
function _request_middleware() {
|
|
33
33
|
_request_middleware = _asyncToGenerator(function* (ctx, next) {
|
|
34
|
-
var _ctx$state;
|
|
35
|
-
|
|
36
34
|
var logger = ctx.miolo.logger;
|
|
37
35
|
REQUEST_COUNTER += 1;
|
|
38
36
|
ctx.requestId = REQUEST_COUNTER; // Patch for koa-better-body
|
|
@@ -45,10 +43,33 @@ function init_request_middleware(app) {
|
|
|
45
43
|
ctx.request.ip = ip;
|
|
46
44
|
logger.info("req begin ".concat(ctx.requestId, " - ip ").concat(ip, " - ").concat((0, _farrapaColors.cyan)(ctx.request.method), " ").concat((0, _farrapaColors.cyan)(ctx.request.url), " ").concat(ctx.request.body != undefined ? JSON.stringify(ctx.request.body) : ''));
|
|
47
45
|
yield next();
|
|
48
|
-
var user =
|
|
46
|
+
var user = undefined;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
user = ctx.state.user;
|
|
50
|
+
} catch (_) {
|
|
51
|
+
user = ctx === null || ctx === void 0 ? void 0 : ctx.user;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var uid_desc = '';
|
|
55
|
+
|
|
56
|
+
if (user != undefined) {
|
|
57
|
+
var _user, _user3;
|
|
58
|
+
|
|
59
|
+
if ((_user = user) !== null && _user !== void 0 && _user.id) {
|
|
60
|
+
var _user2;
|
|
61
|
+
|
|
62
|
+
uid_desc = " - uid ".concat((_user2 = user) === null || _user2 === void 0 ? void 0 : _user2.id);
|
|
63
|
+
} else if ((_user3 = user) !== null && _user3 !== void 0 && _user3.token) {
|
|
64
|
+
var _user4;
|
|
65
|
+
|
|
66
|
+
uid_desc = " - token ".concat((_user4 = user) === null || _user4 === void 0 ? void 0 : _user4.token);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
49
70
|
var elapsed = parseFloat((_perf_hooks.performance.now() - started) / 1000.0).toFixed(2);
|
|
50
71
|
var tcolor = elapsed < 1.0 ? _farrapaColors.green : elapsed < 2.0 ? _farrapaColors.yellow : _farrapaColors.red;
|
|
51
|
-
logger.info("req end ".concat(ctx.requestId
|
|
72
|
+
logger.info("req end ".concat(ctx.requestId).concat(uid_desc, " => ").concat(tcolor("DONE in ".concat(elapsed, " seconds"))));
|
|
52
73
|
});
|
|
53
74
|
return _request_middleware.apply(this, arguments);
|
|
54
75
|
}
|
package/package.json
CHANGED
package/src/config/defaults.js
CHANGED
|
@@ -65,8 +65,17 @@ module.exports= {
|
|
|
65
65
|
|
|
66
66
|
getUserId: (ctx) => {
|
|
67
67
|
try {
|
|
68
|
-
|
|
68
|
+
const from_pport= ctx.state.user.id
|
|
69
|
+
if (from_pport!=undefined) {
|
|
70
|
+
return from_pport
|
|
71
|
+
}
|
|
69
72
|
} catch(e) {}
|
|
73
|
+
try {
|
|
74
|
+
const from_auth= ctx.user.id
|
|
75
|
+
if (from_auth!=undefined) {
|
|
76
|
+
return from_auth
|
|
77
|
+
}
|
|
78
|
+
} catch(e) {}
|
|
70
79
|
let uid= ctx.headers['user-id']
|
|
71
80
|
if (uid!=undefined) {
|
|
72
81
|
return uid
|
|
@@ -231,6 +240,9 @@ module.exports= {
|
|
|
231
240
|
// url_login_redirect: undefined
|
|
232
241
|
// url_logout_redirect: '/'
|
|
233
242
|
//}
|
|
243
|
+
guest: {
|
|
244
|
+
make_guest_token: undefined // (session) => ''
|
|
245
|
+
}
|
|
234
246
|
},
|
|
235
247
|
middlewares: [
|
|
236
248
|
// async (ctx) => {}
|
package/src/server/index.js
CHANGED
|
@@ -62,6 +62,11 @@ async function miolo(sconfig, render, callback) {
|
|
|
62
62
|
init_route_catch_js_error(app, '/sys/jserror')
|
|
63
63
|
|
|
64
64
|
// auth middleware
|
|
65
|
+
if (config?.auth?.guest) {
|
|
66
|
+
const {init_guest_auth_middleware} = require('./middleware/auth/guest')
|
|
67
|
+
init_guest_auth_middleware(app, config.auth.guest, config?.session, logger)
|
|
68
|
+
}
|
|
69
|
+
|
|
65
70
|
if (config?.auth?.basic) {
|
|
66
71
|
const {init_basic_auth_middleware} = require('./middleware/auth/basic')
|
|
67
72
|
init_basic_auth_middleware(app, config.auth.basic)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import jwt from 'jwt-simple'
|
|
2
|
+
|
|
3
|
+
function _guest_token_make_with_jwt(session, logger) {
|
|
4
|
+
const buid= Math.random().toString()
|
|
5
|
+
let secret = session?.secret
|
|
6
|
+
if (!secret) {
|
|
7
|
+
secret= 'miolo_unsafe_secret'
|
|
8
|
+
logger.error('Guest token made with an unsafe secret string. Please, configure your own through session.secret.')
|
|
9
|
+
}
|
|
10
|
+
let maxAge = session?.options?.maxAge
|
|
11
|
+
if (isNaN(maxAge)) {
|
|
12
|
+
maxAge= 86400
|
|
13
|
+
}
|
|
14
|
+
const payload = {
|
|
15
|
+
admin : false,
|
|
16
|
+
buid : buid,
|
|
17
|
+
expires: Date.now() + maxAge
|
|
18
|
+
}
|
|
19
|
+
return jwt.encode(payload, secret)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const init_guest_auth_middleware = ( app, options, session, logger ) => {
|
|
24
|
+
|
|
25
|
+
const _make_guest_token = () => {
|
|
26
|
+
try {
|
|
27
|
+
let {make_guest_token} = options
|
|
28
|
+
if (make_guest_token!=undefined) {
|
|
29
|
+
return make_guest_token(session || {})
|
|
30
|
+
}
|
|
31
|
+
} catch(_) {}
|
|
32
|
+
|
|
33
|
+
return _guest_token_make_with_jwt(session || {}, logger)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function guest_auth_middleware(ctx, next) {
|
|
37
|
+
|
|
38
|
+
// Try to get our token from headers (server) or cookies (client)
|
|
39
|
+
let token= ctx.cookies.get('token') || ctx.headers['token']
|
|
40
|
+
|
|
41
|
+
if (token) {
|
|
42
|
+
token = await _make_guest_token()
|
|
43
|
+
logger.debug(`Guest token conceeded`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
ctx.cookies.set('token', token)
|
|
47
|
+
ctx.user = {
|
|
48
|
+
name: 'guest',
|
|
49
|
+
token
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
await next()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
app.use(guest_auth_middleware)
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {init_guest_auth_middleware}
|
|
@@ -28,8 +28,22 @@ function init_request_middleware(app) {
|
|
|
28
28
|
|
|
29
29
|
await next()
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
|
|
32
|
+
let user = undefined
|
|
33
|
+
try {
|
|
34
|
+
user= ctx.state.user
|
|
35
|
+
} catch(_) {
|
|
36
|
+
user= ctx?.user
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let uid_desc= ''
|
|
40
|
+
if (user != undefined) {
|
|
41
|
+
if (user?.id) {
|
|
42
|
+
uid_desc= ` - uid ${user?.id}`
|
|
43
|
+
} else if (user?.token) {
|
|
44
|
+
uid_desc= ` - token ${user?.token}`
|
|
45
|
+
}
|
|
46
|
+
}
|
|
33
47
|
|
|
34
48
|
const elapsed = parseFloat( (performance.now() - started) / 1000.0 ).toFixed(2)
|
|
35
49
|
|
|
@@ -39,7 +53,7 @@ function init_request_middleware(app) {
|
|
|
39
53
|
? yellow
|
|
40
54
|
: red
|
|
41
55
|
|
|
42
|
-
logger.info(`req end ${ctx.requestId}
|
|
56
|
+
logger.info(`req end ${ctx.requestId}${uid_desc} => ${tcolor(`DONE in ${elapsed} seconds`)}`)
|
|
43
57
|
}
|
|
44
58
|
|
|
45
59
|
app.use(request_middleware)
|