miolo 3.0.0-beta.21 → 3.0.0-beta.210
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/bin/build/build.mjs +53 -0
- package/bin/build/build_bin.mjs +17 -0
- package/bin/build/cli/client.mjs +52 -0
- package/bin/build/cli/css.mjs +22 -0
- package/bin/build/cli/index.mjs +40 -0
- package/bin/build/cli/ssr.mjs +21 -0
- package/bin/build/server/aliases.mjs +112 -0
- package/bin/build/server/babel.config.js +24 -0
- package/bin/build/server/banner.mjs +20 -0
- package/bin/build/server/bundle.mjs +111 -0
- package/bin/build/server/fix.mjs +15 -0
- package/bin/build/server/index.mjs +69 -0
- package/bin/build/server/options.mjs +83 -0
- package/bin/create/auth.mjs +23 -0
- package/bin/create/copy.mjs +175 -0
- package/bin/create/docker.mjs +25 -0
- package/bin/create/index.mjs +137 -0
- package/bin/create/pkgjson.mjs +72 -0
- package/bin/create/prepare-template.mjs +158 -0
- package/bin/create/validation.mjs +27 -0
- package/bin/dev/dev.mjs +32 -23
- package/bin/dev/dev_start.mjs +6 -5
- package/bin/index.mjs +94 -52
- package/bin/prod-bin/create-bin.mjs +42 -27
- package/bin/prod-bin/run.mjs +13 -9
- package/bin/{prod-run → run}/pid.mjs +4 -4
- package/bin/run/restart.mjs +13 -0
- package/bin/run/start.mjs +18 -0
- package/bin/run/stop.mjs +20 -0
- package/bin/util.mjs +35 -11
- package/package.json +59 -39
- package/src/config/.env +34 -12
- package/src/config/defaults.mjs +253 -185
- package/src/config/env.mjs +40 -22
- package/src/config/index.mjs +19 -24
- package/src/config/util.mjs +25 -10
- package/src/db-conn.mjs +34 -0
- package/src/engines/cron/emails.mjs +10 -5
- package/src/engines/cron/index.mjs +45 -51
- package/src/engines/cron/init.mjs +16 -17
- package/src/engines/cron/ipsum.mjs +65 -60
- package/src/engines/cron/syscheck.mjs +30 -30
- package/src/engines/emailer/index.mjs +1 -2
- package/src/engines/emailer/queue.mjs +14 -20
- package/src/engines/emailer/transporter.mjs +86 -74
- package/src/engines/geoip/index.mjs +23 -28
- package/src/engines/http/index.mjs +26 -15
- package/src/engines/logger/buildErrorEmailBody.mjs +72 -0
- package/src/engines/logger/index.mjs +114 -122
- package/src/engines/logger/injectStackTrace.mjs +59 -0
- package/src/engines/logger/logger_mail.mjs +47 -61
- package/src/engines/logger/reopenTransportOnHupSignal.mjs +12 -13
- package/src/engines/parser/Parser.mjs +77 -60
- package/src/engines/parser/index.mjs +1 -1
- package/src/engines/schema/diffObjs.mjs +41 -0
- package/src/engines/schema/index.mjs +4 -0
- package/src/engines/schema/input.mjs +54 -0
- package/src/engines/schema/output.mjs +66 -0
- package/src/engines/socket/index.mjs +44 -46
- package/src/index.mjs +15 -10
- package/src/middleware/auth/basic.mjs +41 -40
- package/src/middleware/auth/custom.mjs +10 -13
- package/src/middleware/auth/guest.mjs +27 -27
- package/src/middleware/auth/passport/index.mjs +374 -0
- package/src/middleware/auth/passport/session/index.mjs +43 -0
- package/src/middleware/auth/{credentials → passport}/session/store.mjs +35 -15
- package/src/middleware/auth/passport/session/store_koa_redis.mjs +3 -0
- package/src/middleware/context/cache/index.mjs +78 -33
- package/src/middleware/context/cache/options.mjs +19 -21
- package/src/middleware/context/db.mjs +45 -20
- package/src/middleware/context/index.mjs +12 -12
- package/src/middleware/extra.mjs +4 -5
- package/src/middleware/http/body.mjs +25 -25
- package/src/middleware/http/catcher.mjs +81 -8
- package/src/middleware/http/custom_blacklist.mjs +19 -16
- package/src/middleware/http/headers.mjs +37 -34
- package/src/middleware/http/ratelimit.mjs +16 -23
- package/src/middleware/http/request.mjs +60 -65
- package/src/middleware/routes/catch_js_error.mjs +30 -23
- package/src/middleware/routes/robots.mjs +4 -7
- package/src/middleware/routes/router/crud/attachCrudRoutes.mjs +108 -90
- package/src/middleware/routes/router/crud/getCrudConfig.mjs +31 -55
- package/src/middleware/routes/router/defaults.mjs +6 -19
- package/src/middleware/routes/router/index.mjs +17 -21
- package/src/middleware/routes/router/queries/attachQueriesRoutes.mjs +227 -50
- package/src/middleware/routes/router/queries/getQueriesConfig.mjs +45 -55
- package/src/middleware/routes/router/utils.mjs +41 -26
- package/src/middleware/ssr/context.mjs +5 -7
- package/src/middleware/ssr/html.mjs +66 -43
- package/src/middleware/ssr/loader.mjs +11 -14
- package/src/middleware/ssr/ssr_render.mjs +39 -22
- package/src/middleware/static/index.mjs +33 -14
- package/src/middleware/vite/devserver.mjs +38 -22
- package/src/middleware/vite/watcher.mjs +12 -14
- package/src/server-cron.mjs +13 -8
- package/src/server-dev.mjs +13 -16
- package/src/server.mjs +49 -51
- package/template/.agent/skills/miolo-app-arch/SKILL.md +218 -0
- package/template/.agent/skills/miolo-auth/SKILL.md +450 -0
- package/template/.agent/skills/miolo-cli-router/SKILL.md +394 -0
- package/template/.agent/skills/miolo-database/SKILL.md +358 -0
- package/template/.agent/skills/miolo-react-patterns/SKILL.md +426 -0
- package/template/.agent/skills/miolo-routing/SKILL.md +326 -0
- package/template/.agent/skills/miolo-schemas/SKILL.md +329 -0
- package/template/.agent/skills/miolo-session-context/SKILL.md +397 -0
- package/template/.agent/skills/miolo-ssr/SKILL.md +433 -0
- package/template/.editorconfig +18 -0
- package/template/.env +120 -0
- package/template/biome.json +63 -0
- package/template/components.json +21 -0
- package/template/db/init.sh +89 -0
- package/template/db/sql/00_drop.sql +2 -0
- package/template/db/sql/01_users.sql +31 -0
- package/template/db/sql/02_todos.sql +20 -0
- package/template/docker/Dockerfile +13 -0
- package/template/docker/docker-compose.yaml +79 -0
- package/template/gitignore +42 -0
- package/template/jsconfig.json +18 -0
- package/template/package.json +88 -0
- package/template/postcss.config.js +9 -0
- package/template/src/cli/App.jsx +25 -0
- package/template/src/cli/components/JsonTreeViewer.jsx +128 -0
- package/template/src/cli/components/shadcn-io/spinner/index.jsx +232 -0
- package/template/src/cli/components/stepper.jsx +408 -0
- package/template/src/cli/components/ui/avatar.jsx +36 -0
- package/template/src/cli/components/ui/badge.jsx +31 -0
- package/template/src/cli/components/ui/breadcrumb.jsx +97 -0
- package/template/src/cli/components/ui/card.jsx +73 -0
- package/template/src/cli/components/ui/collapsible.jsx +16 -0
- package/template/src/cli/components/ui/dropdown-menu.jsx +179 -0
- package/template/src/cli/components/ui/field.jsx +217 -0
- package/template/src/cli/components/ui/input.jsx +19 -0
- package/template/src/cli/components/ui/label.jsx +17 -0
- package/template/src/cli/components/ui/pagination.jsx +99 -0
- package/template/src/cli/components/ui/patched/alert.jsx +56 -0
- package/template/src/cli/components/ui/patched/button.jsx +45 -0
- package/template/src/cli/components/ui/patched/dialog.jsx +114 -0
- package/template/src/cli/components/ui/patched/sidebar.jsx +660 -0
- package/template/src/cli/components/ui/select.jsx +141 -0
- package/template/src/cli/components/ui/separator.jsx +21 -0
- package/template/src/cli/components/ui/sheet.jsx +115 -0
- package/template/src/cli/components/ui/skeleton.jsx +13 -0
- package/template/src/cli/components/ui/sonner.jsx +22 -0
- package/template/src/cli/components/ui/switch.jsx +25 -0
- package/template/src/cli/components/ui/table.jsx +88 -0
- package/template/src/cli/components/ui/textarea.jsx +16 -0
- package/template/src/cli/components/ui/tooltip.jsx +45 -0
- package/template/src/cli/config/store_keys.mjs +2 -0
- package/template/src/cli/context/data/DataContext.jsx +5 -0
- package/template/src/cli/context/data/DataProvider.jsx +44 -0
- package/template/src/cli/context/data/useBreads.mjs +15 -0
- package/template/src/cli/context/data/useDataContext.mjs +4 -0
- package/template/src/cli/context/session/SessionContext.mjs +4 -0
- package/template/src/cli/context/session/SessionProvider.jsx +31 -0
- package/template/src/cli/context/session/makePermissioner.mjs +34 -0
- package/template/src/cli/context/session/useSessionContext.mjs +6 -0
- package/template/src/cli/context/theme/ThemeContext.mjs +4 -0
- package/template/src/cli/context/theme/ThemeProvider.jsx +49 -0
- package/template/src/cli/context/theme/useThemeContext.mjs +6 -0
- package/template/src/cli/context/ui/UIContext.jsx +5 -0
- package/template/src/cli/context/ui/UIProvider.jsx +16 -0
- package/template/src/cli/context/ui/useUIContext.mjs +4 -0
- package/template/src/cli/context/util.mjs +17 -0
- package/template/src/cli/entry-cli.jsx +33 -0
- package/template/src/cli/hooks/useIsMobile.mjs +19 -0
- package/template/src/cli/hooks/useStoragedState.mjs +63 -0
- package/template/src/cli/index.html +29 -0
- package/template/src/cli/layout/app-sidebar.jsx +25 -0
- package/template/src/cli/layout/main-layout.jsx +63 -0
- package/template/src/cli/layout/nav-last-todos.jsx +72 -0
- package/template/src/cli/layout/nav-main.jsx +39 -0
- package/template/src/cli/layout/nav-user.jsx +105 -0
- package/template/src/cli/layout/prop-switcher.jsx +93 -0
- package/template/src/cli/lib/utils.mjs +10 -0
- package/template/src/cli/pages/Index.jsx +13 -0
- package/template/src/cli/pages/IndexOffline.jsx +13 -0
- package/template/src/cli/pages/IndexOnline.jsx +18 -0
- package/template/src/cli/pages/dash/Dashboard.jsx +29 -0
- package/template/src/cli/pages/offline/Login.jsx +43 -0
- package/template/src/cli/pages/offline/LoginForm.jsx +115 -0
- package/template/src/cli/pages/security/Security.jsx +39 -0
- package/template/src/cli/pages/security/SecurityForm.jsx +106 -0
- package/template/src/cli/pages/todos/TodoActions.jsx +99 -0
- package/template/src/cli/pages/todos/TodoAdd.jsx +43 -0
- package/template/src/cli/pages/todos/TodoList.jsx +60 -0
- package/template/src/cli/pages/todos/Todos.jsx +23 -0
- package/template/src/cli/pages/todos/context/TodosContext.jsx +5 -0
- package/template/src/cli/pages/todos/context/TodosProvider.jsx +191 -0
- package/template/src/cli/pages/todos/context/useTodosContext.mjs +4 -0
- package/template/src/ns/models/Todo.mjs +29 -0
- package/template/src/ns/models/TodoList.mjs +8 -0
- package/template/src/ns/models/User.mjs +40 -0
- package/template/src/server/bot/check_today.mjs +10 -0
- package/template/src/server/io/cache/base.mjs +21 -0
- package/template/src/server/io/db/filter.mjs +92 -0
- package/template/src/server/io/db/todos/delete.mjs +29 -0
- package/template/src/server/io/db/todos/find.mjs +13 -0
- package/template/src/server/io/db/todos/read.mjs +83 -0
- package/template/src/server/io/db/todos/toggle.mjs +37 -0
- package/template/src/server/io/db/todos/upsave.mjs +32 -0
- package/template/src/server/io/db/triggers/user.mjs +13 -0
- package/template/src/server/io/db/users/auth.mjs +132 -0
- package/template/src/server/io/db/users/pwd.mjs +38 -0
- package/template/src/server/io/db/users/save.mjs +17 -0
- package/template/src/server/miolo/auth/basic.mjs +15 -0
- package/template/src/server/miolo/auth/guest.mjs +3 -0
- package/template/src/server/miolo/auth/passport.mjs +73 -0
- package/template/src/server/miolo/cache.mjs +11 -0
- package/template/src/server/miolo/cron/foo.mjs +7 -0
- package/template/src/server/miolo/cron/index.mjs +28 -0
- package/template/src/server/miolo/cron/invalidate.mjs +21 -0
- package/template/src/server/miolo/db.mjs +36 -0
- package/template/src/server/miolo/http.mjs +14 -0
- package/template/src/server/miolo/index.mjs +43 -0
- package/template/src/server/miolo/routes/crud.mjs +16 -0
- package/template/src/server/miolo/routes/index.mjs +8 -0
- package/template/src/server/miolo/ssr/entry-server.jsx +13 -0
- package/template/src/server/miolo/ssr/loader.mjs +18 -0
- package/template/src/server/routes/index.mjs +66 -0
- package/template/src/server/routes/todos/mod.mjs +52 -0
- package/template/src/server/routes/todos/read.mjs +45 -0
- package/template/src/server/routes/todos/special.mjs +47 -0
- package/template/src/server/routes/users/user.mjs +54 -0
- package/template/src/server/server.mjs +10 -0
- package/template/src/server/utils/crypt.mjs +38 -0
- package/template/src/server/utils/io.mjs +15 -0
- package/template/src/server/utils/pwdfor.mjs +25 -0
- package/template/src/server/utils/schema.mjs +22 -0
- package/template/src/static/img/default/profile.png +0 -0
- package/template/src/static/img/favicon.ico +0 -0
- package/template/src/static/img/miolo_logo.png +0 -0
- package/template/src/static/img/miolo_name.png +0 -0
- package/template/src/static/public/manifest.json +21 -0
- package/template/src/static/public/sw.js +79 -0
- package/template/src/static/style/globals.css +156 -0
- package/template/src/static/style/json-tree.css +54 -0
- package/template/src/static/style/skeleton.css +49 -0
- package/bin/prod-build/build-client.mjs +0 -67
- package/bin/prod-build/build-server.mjs +0 -58
- package/bin/prod-run/restart.mjs +0 -9
- package/bin/prod-run/start.mjs +0 -15
- package/bin/prod-run/stop.mjs +0 -20
- package/src/engines/logger/verify.mjs +0 -22
- package/src/middleware/auth/credentials/index.mjs +0 -151
- package/src/middleware/auth/credentials/session/index.mjs +0 -24
- package/src/middleware/auth/credentials/session/store_koa_redis.mjs +0 -3
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { Server } from 'socket.io'
|
|
1
|
+
import { Server } from "socket.io"
|
|
3
2
|
|
|
4
3
|
function init_socket(app, config) {
|
|
5
4
|
if (config?.enabled !== true) {
|
|
@@ -8,60 +7,59 @@ function init_socket(app, config) {
|
|
|
8
7
|
|
|
9
8
|
const logger = app.context.miolo.logger
|
|
10
9
|
|
|
11
|
-
logger.info(
|
|
10
|
+
logger.info("[socket] Attaching Socket server")
|
|
12
11
|
|
|
13
12
|
const io = new Server(app.http.server)
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (config?.userRooms === true) {
|
|
15
|
+
io.use(async (socket, next) => {
|
|
16
|
+
try {
|
|
17
|
+
const { store, options } = app.context.miolo.session || {}
|
|
18
|
+
if (store && options) {
|
|
19
|
+
// Create a fake Koa context to easily parse the signed cookies
|
|
20
|
+
const ctx = app.createContext(
|
|
21
|
+
socket.request,
|
|
22
|
+
socket.request.res || { headersSent: false }
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
// Read the session cookie (Koa handles signature validation if options.signed is true)
|
|
26
|
+
const sessionCookieValue = ctx.cookies.get(options.key, options)
|
|
27
|
+
|
|
28
|
+
if (sessionCookieValue) {
|
|
29
|
+
// If the cookie is present, koa-session stores the external key directly
|
|
30
|
+
// or sometimes it's base64 encoded. koa-session's decode function:
|
|
31
|
+
const sessionId = sessionCookieValue
|
|
32
|
+
|
|
33
|
+
const session = await store.get(sessionId, undefined, {})
|
|
34
|
+
|
|
35
|
+
if (session?.user) {
|
|
36
|
+
const userId = session.user?.id
|
|
37
|
+
socket.join(`user_${userId}`)
|
|
38
|
+
socket.mioloUser = userId // Attach for convenience
|
|
39
|
+
logger.info(`[socket] Socket ${socket.id} joined room user_${userId}`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
logger.error(`[socket] Error parsing session: ${err.message}`)
|
|
45
|
+
}
|
|
46
|
+
next()
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
io.on("connection", (socket) => {
|
|
51
|
+
logger.info(`[socket] Connection from ... `) // ${i.ip} ${i.id}`)
|
|
52
|
+
|
|
18
53
|
if (config?.connection) {
|
|
19
54
|
config.connection(socket)
|
|
20
55
|
}
|
|
21
|
-
|
|
56
|
+
|
|
22
57
|
for (const ns of config?.namespaces || []) {
|
|
23
58
|
socket.on(ns.name, ns.listener)
|
|
24
59
|
}
|
|
25
60
|
})
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// const getInfo = (ctx) => {
|
|
29
|
-
// let i= {id: '', ip: ''}
|
|
30
|
-
// try {
|
|
31
|
-
// i.id = ctx.socket.id
|
|
32
|
-
// } catch (e) {}
|
|
33
|
-
// try {
|
|
34
|
-
// i.ip = ctx.socket.handshake.address
|
|
35
|
-
// } catch (e) {}
|
|
36
|
-
// return i
|
|
37
|
-
// }
|
|
38
|
-
|
|
39
|
-
// const io = new IO({ origins: '*:*'})
|
|
40
|
-
|
|
41
|
-
// io.on('connection', function (ctx, data) {
|
|
42
|
-
// const logger = ctx.miolo.logger
|
|
43
|
-
// const i= getInfo(ctx)
|
|
44
|
-
// logger.warn(`[socket] Connection from ${i.ip} ${i.id}`)
|
|
45
|
-
// if (config?.connection) {
|
|
46
|
-
// config.connection(ctx, data)
|
|
47
|
-
// }
|
|
48
|
-
// })
|
|
49
|
-
|
|
50
|
-
// io.on('disconnect', function (ctx, data) {
|
|
51
|
-
// const logger = ctx.miolo.logger
|
|
52
|
-
// const i = getInfo(ctx)
|
|
53
|
-
// logger.warn(`[socket] Disconnected ${i.ip} ${i.id} => ${data}`)
|
|
54
|
-
// })
|
|
55
|
-
//
|
|
56
|
-
// io.on('error', function (ctx, data) {
|
|
57
|
-
// const logger = ctx.miolo.logger
|
|
58
|
-
// const i = getInfo(ctx)
|
|
59
|
-
// logger.error(`[socket] Error on ${i.ip} ${i.id} => ${data}`)
|
|
60
|
-
// })
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
// io.attach(app)
|
|
64
61
|
|
|
62
|
+
app.context.miolo.io = io
|
|
65
63
|
}
|
|
66
64
|
|
|
67
|
-
export {init_socket}
|
|
65
|
+
export { init_socket }
|
package/src/index.mjs
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { init_emailer_transporter as miolo_emailer} from
|
|
4
|
-
import { init_logger as miolo_logger } from
|
|
5
|
-
import { init_parser as miolo_parser } from
|
|
1
|
+
import { cacheiro as miolo_cacher } from "cacheiro"
|
|
2
|
+
import { miolo_db_connection_pg, miolo_db_drop_connections } from "./db-conn.mjs"
|
|
3
|
+
import { init_emailer_transporter as miolo_emailer } from "./engines/emailer/index.mjs"
|
|
4
|
+
import { init_logger as miolo_logger } from "./engines/logger/index.mjs"
|
|
5
|
+
import { init_parser as miolo_parser } from "./engines/parser/index.mjs"
|
|
6
|
+
import { with_miolo_input_schema, with_miolo_output_schema } from "./engines/schema/index.mjs"
|
|
7
|
+
import { miolo } from "./server.mjs"
|
|
8
|
+
import { miolo_cron } from "./server-cron.mjs"
|
|
6
9
|
|
|
7
|
-
import {
|
|
8
|
-
import { getConnection as miolo_db_connection} from 'calustra/conn-postgres'
|
|
10
|
+
// import { getConnection as miolo_db_connection_sqlite} from 'calustra/conn-sqlite'
|
|
9
11
|
|
|
10
12
|
export {
|
|
11
13
|
miolo,
|
|
14
|
+
miolo_cacher,
|
|
12
15
|
miolo_cron,
|
|
16
|
+
miolo_db_connection_pg,
|
|
17
|
+
miolo_db_drop_connections,
|
|
13
18
|
miolo_emailer,
|
|
14
19
|
miolo_logger,
|
|
15
20
|
miolo_parser,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
21
|
+
with_miolo_input_schema,
|
|
22
|
+
with_miolo_output_schema
|
|
23
|
+
}
|
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
import koa_mount from
|
|
1
|
+
import koa_mount from "koa-mount"
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
let sauth= req?.headers?.authorization
|
|
3
|
+
const _get_basic_credentials = (req) => {
|
|
4
|
+
let sauth = req?.headers?.authorization
|
|
5
5
|
|
|
6
|
-
if (!
|
|
6
|
+
if (!sauth) {
|
|
7
7
|
return undefined
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
try {
|
|
11
|
-
sauth= sauth.replace(
|
|
12
|
-
try
|
|
13
|
-
sauth= Buffer.from(sauth,
|
|
14
|
-
} catch(_) {
|
|
15
|
-
sauth= atob(sauth)
|
|
11
|
+
sauth = sauth.replace("Basic ", "")
|
|
12
|
+
try {
|
|
13
|
+
sauth = Buffer.from(sauth, "base64").toString()
|
|
14
|
+
} catch (_) {
|
|
15
|
+
sauth = atob(sauth)
|
|
16
16
|
}
|
|
17
|
-
const [username, password]= sauth.split(
|
|
18
|
-
return {username, password}
|
|
19
|
-
|
|
20
|
-
} catch(_) {
|
|
17
|
+
const [username, password] = sauth.split(":")
|
|
18
|
+
return { username, password }
|
|
19
|
+
} catch (_) {
|
|
21
20
|
return undefined
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
const init_basic_auth_middleware = (
|
|
26
|
-
let {auth_user, realm, paths} = options
|
|
27
|
-
if (!
|
|
28
|
-
realm =
|
|
24
|
+
const init_basic_auth_middleware = (app, options) => {
|
|
25
|
+
let { auth_user, realm, paths } = options
|
|
26
|
+
if (!realm) {
|
|
27
|
+
realm = "Secure Area"
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
async function basic_auth_middleware(ctx, next) {
|
|
32
|
-
let
|
|
31
|
+
let aaccount
|
|
33
32
|
try {
|
|
34
|
-
|
|
35
|
-
} catch(_) {}
|
|
33
|
+
aaccount = _get_basic_credentials(ctx.request)
|
|
34
|
+
} catch (_) {}
|
|
36
35
|
|
|
37
36
|
const unauth_err = () => {
|
|
38
37
|
// This will show error logs on the catcher middleware
|
|
@@ -46,45 +45,47 @@ const init_basic_auth_middleware = ( app, options ) => {
|
|
|
46
45
|
// }
|
|
47
46
|
//)
|
|
48
47
|
|
|
49
|
-
ctx.session= {
|
|
48
|
+
ctx.session = {
|
|
50
49
|
user: undefined,
|
|
51
|
-
authenticated: false
|
|
50
|
+
authenticated: false,
|
|
51
|
+
auth_method: undefined
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
ctx.body= {
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
ctx.body = {
|
|
55
|
+
ok: true,
|
|
56
|
+
data: {
|
|
57
|
+
user: undefined,
|
|
58
|
+
authenticated: false
|
|
59
|
+
}
|
|
57
60
|
}
|
|
58
|
-
ctx.response.status= 401
|
|
59
|
-
ctx.response.headers[
|
|
61
|
+
ctx.response.status = 401
|
|
62
|
+
ctx.response.headers["WWW-Authenticate"] = 'Basic realm="' + realm.replace(/"/g, '\\"') + '"'
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
if (!
|
|
65
|
+
if (!aaccount) {
|
|
63
66
|
return unauth_err()
|
|
64
67
|
}
|
|
65
|
-
|
|
66
|
-
const user = await auth_user(au_user.username, au_user.password, app.context.miolo)
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
const user = await auth_user(aaccount.username, aaccount.password, ctx)
|
|
70
|
+
|
|
71
|
+
if (user === false || user === undefined || user === null) {
|
|
69
72
|
return unauth_err()
|
|
70
73
|
}
|
|
71
74
|
|
|
72
|
-
ctx.session= {
|
|
75
|
+
ctx.session = {
|
|
73
76
|
user,
|
|
74
|
-
authenticated: true
|
|
77
|
+
authenticated: true,
|
|
78
|
+
auth_method: "basic"
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
await next()
|
|
78
82
|
}
|
|
79
83
|
|
|
80
|
-
if (paths
|
|
84
|
+
if (paths === undefined || paths.length === 0) {
|
|
81
85
|
app.use(basic_auth_middleware)
|
|
82
86
|
} else {
|
|
83
|
-
paths.map(p =>
|
|
84
|
-
app.use(koa_mount(p, basic_auth_middleware))
|
|
85
|
-
)
|
|
87
|
+
paths.map((p) => app.use(koa_mount(p, basic_auth_middleware)))
|
|
86
88
|
}
|
|
87
|
-
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
export {init_basic_auth_middleware}
|
|
91
|
+
export { init_basic_auth_middleware }
|
|
@@ -1,29 +1,26 @@
|
|
|
1
|
+
import Router from "@koa/router"
|
|
1
2
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const init_custom_auth_middleware = ( app, callback ) => {
|
|
5
|
-
const logger= app.context.miolo.logger
|
|
3
|
+
const init_custom_auth_middleware = (app, callback) => {
|
|
4
|
+
const logger = app.context.miolo.logger
|
|
6
5
|
|
|
7
6
|
try {
|
|
7
|
+
const custom_auth_middleware = callback(app)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (typeof custom_auth_middleware === 'function') {
|
|
9
|
+
if (typeof custom_auth_middleware === "function") {
|
|
12
10
|
app.use(custom_auth_middleware)
|
|
13
11
|
} else if (Array.isArray(custom_auth_middleware)) {
|
|
14
|
-
const custom_auth_router= new Router()
|
|
12
|
+
const custom_auth_router = new Router()
|
|
15
13
|
|
|
16
|
-
custom_auth_middleware.
|
|
17
|
-
const method= r.method.toLowerCase()
|
|
14
|
+
custom_auth_middleware.forEach((r) => {
|
|
15
|
+
const method = r.method.toLowerCase()
|
|
18
16
|
custom_auth_router[method](r.url, r.callback)
|
|
19
17
|
})
|
|
20
18
|
|
|
21
19
|
app.use(custom_auth_router.routes())
|
|
22
20
|
}
|
|
23
|
-
|
|
24
|
-
} catch(err) {
|
|
21
|
+
} catch (err) {
|
|
25
22
|
logger.error(`Custom auth error: ${err}'`)
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
export {init_custom_auth_middleware}
|
|
26
|
+
export { init_custom_auth_middleware }
|
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
import jwt from
|
|
1
|
+
import jwt from "jwt-simple"
|
|
2
2
|
|
|
3
3
|
function _guest_token_make_with_jwt(session, logger) {
|
|
4
|
-
const buid= Math.random().toString()
|
|
4
|
+
const buid = Math.random().toString()
|
|
5
5
|
let secret = session?.secret
|
|
6
6
|
if (!secret) {
|
|
7
|
-
secret=
|
|
8
|
-
logger.error(
|
|
7
|
+
secret = "miolo_unsafe_secret"
|
|
8
|
+
logger.error(
|
|
9
|
+
"Guest token made with an unsafe secret string. Please, configure your own through session.secret."
|
|
10
|
+
)
|
|
9
11
|
}
|
|
10
|
-
|
|
12
|
+
|
|
11
13
|
const payload = {
|
|
12
|
-
admin
|
|
13
|
-
buid
|
|
14
|
+
admin: false,
|
|
15
|
+
buid: buid
|
|
14
16
|
}
|
|
15
17
|
return jwt.encode(payload, secret)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
function _get_cookie_properties(session) {
|
|
19
21
|
let maxAge = session?.options?.maxAge
|
|
20
|
-
if (isNaN(maxAge)) {
|
|
21
|
-
maxAge= 86400
|
|
22
|
+
if (Number.isNaN(Number(maxAge))) {
|
|
23
|
+
maxAge = 86400
|
|
22
24
|
}
|
|
23
|
-
|
|
25
|
+
const expires = new Date()
|
|
24
26
|
expires.setSeconds(expires.getSeconds() + maxAge)
|
|
25
27
|
|
|
26
28
|
return {
|
|
@@ -29,47 +31,45 @@ function _get_cookie_properties(session) {
|
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
const init_guest_auth_middleware = (app, options, session) => {
|
|
35
|
+
const logger = app.context.miolo.logger
|
|
32
36
|
|
|
33
|
-
const init_guest_auth_middleware = ( app, options, session ) => {
|
|
34
|
-
const logger= app.context.miolo.logger
|
|
35
|
-
|
|
36
37
|
const _make_guest_token = () => {
|
|
37
38
|
try {
|
|
38
|
-
|
|
39
|
-
if (make_guest_token
|
|
39
|
+
const { make_guest_token } = options
|
|
40
|
+
if (make_guest_token !== undefined) {
|
|
40
41
|
return make_guest_token(session || {})
|
|
41
42
|
}
|
|
42
|
-
} catch(_) {}
|
|
43
|
+
} catch (_) {}
|
|
43
44
|
|
|
44
45
|
return _guest_token_make_with_jwt(session || {}, logger)
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
async function guest_auth_middleware(ctx, next) {
|
|
48
|
-
|
|
49
49
|
// Try to get our token from headers (server) or cookies (client)
|
|
50
|
-
let token= ctx.cookies.get(
|
|
50
|
+
let token = ctx.cookies.get("token") || ctx.headers.token
|
|
51
51
|
|
|
52
|
-
if (token===undefined || token.length
|
|
52
|
+
if (token === undefined || token.length === 0) {
|
|
53
53
|
token = await _make_guest_token()
|
|
54
54
|
logger.debug(`Guest token conceeded`)
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
const options= _get_cookie_properties(session)
|
|
58
|
-
ctx.cookies.set(
|
|
57
|
+
const options = _get_cookie_properties(session)
|
|
58
|
+
ctx.cookies.set("token", token, options)
|
|
59
59
|
|
|
60
|
-
ctx.session= {
|
|
60
|
+
ctx.session = {
|
|
61
61
|
user: {
|
|
62
|
-
name:
|
|
62
|
+
name: "guest"
|
|
63
63
|
},
|
|
64
64
|
authenticated: true,
|
|
65
|
+
auth_method: "guest",
|
|
65
66
|
token
|
|
66
|
-
}
|
|
67
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
68
69
|
await next()
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
app.use(guest_auth_middleware)
|
|
72
|
-
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
export {init_guest_auth_middleware}
|
|
75
|
+
export { init_guest_auth_middleware }
|