env SESSION_DURATION

Massimo Melina committed Apr 11, 2023 at 22:31 UTC 93045de5b8a3b458a72fd5e91b82eb36f02e2d19
4 files changed +5 -13
README.md
+1
@@ -167,6 +167,7 @@ This is enough, but you may want to configure generated links accordingly:
167
168 - Appending `#LOGIN` to address will bring up the login dialog
169 - Appending ?lang=CODE to address will force a specific language
170 +- env `SESSION_DURATION` can be set to any number of seconds, or to "session" to make it expire when session/browser is closed
171
172 ## Contribute
173
src/const.ts
+1 -1
@@ -15,7 +15,7 @@ export const BUILD_TIMESTAMP = fs.statSync(PKG_PATH).mtime.toISOString()
15 const pkg = JSON.parse(fs.readFileSync(PKG_PATH,'utf8'))
16 export const VERSION = pkg.version
17 export const DAY = 86_400_000
18 -export const SESSION_DURATION = DAY
18 +export const SESSION_DURATION = Number(process.env.SESSION_DURATION) * 1000 || DAY
19
20 export const API_VERSION = 8 // entry.uri + script.plugin
21 export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise is made equal to API_VERSION
src/index.ts
+3 -3
@@ -4,7 +4,7 @@
4 import Koa from 'koa'
5 import mount from 'koa-mount'
6 import { apiMiddleware } from './apiMiddleware'
7 -import { API_URI, DEV } from './const'
7 +import { API_URI, DEV, SESSION_DURATION } from './const'
8 import { frontEndApis } from './frontEndApis'
9 import { log } from './log'
10 import { pluginsMiddleware } from './plugins'
@@ -12,7 +12,6 @@ import { throttler } from './throttler'
12 import {
13 headRequests,
14 gzipper,
15 - sessions,
15 serveGuiAndSharedFiles,
16 someSecurity,
17 prepareState,
@@ -25,13 +24,14 @@ import { defineConfig } from './config'
24 import { ok } from 'assert'
25 import _ from 'lodash'
26 import { randomId } from './misc'
27 +import session from 'koa-session'
28
29 ok(_.intersection(Object.keys(frontEndApis), Object.keys(adminApis)).length === 0) // they share same endpoints
30
31 const keys = process.env.COOKIE_SIGN_KEYS?.split(',') || [randomId(30)]
32 export const app = new Koa({ keys })
33 app.use(someSecurity)
34 - .use(sessions(app))
34 + .use(session({ key: 'hfs_$id', signed: true, rolling: true, maxAge: SESSION_DURATION }, app))
35 .use(prepareState)
36 .use(headRequests)
37 .use(log())
src/middlewares.ts
-9
@@ -2,12 +2,10 @@
2
3 import compress from 'koa-compress'
4 import Koa from 'koa'
5 -import session from 'koa-session'
5 import {
6 ADMIN_URI, API_URI,
7 BUILD_TIMESTAMP,
8 DEV, DAY,
10 - SESSION_DURATION,
9 HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_FOOL,
10 } from './const'
11 import { FRONTEND_URI } from './const'
@@ -60,13 +58,6 @@ export const headRequests: Koa.Middleware = async (ctx, next) => {
58 ctx.response.length = length
59 }
60
63 -export const sessions = (app: Koa) => session({
64 - key: 'hfs_$id',
65 - signed: true,
66 - rolling: true,
67 - maxAge: SESSION_DURATION,
68 -}, app)
69 -
61 const serveFrontendFiles = serveGuiFiles(process.env.FRONTEND_PROXY, FRONTEND_URI)
62 const serveFrontendPrefixed = mount(FRONTEND_URI.slice(0,-1), serveFrontendFiles)
63 const serveAdminFiles = serveGuiFiles(process.env.ADMIN_PROXY, ADMIN_URI)