admin/options: session duration
Massimo Melina committed
May 10, 2023 at 23:46 UTC
5be422975b95538ae34d82fbbd3d015b32d10a3d
5 files changed
+11
-13
admin/src/OptionsPage.ts
+2
-1
@@ -144,7 +144,8 @@ export default function OptionsPage() {
144
{ k: 'min_available_mb', comp: NumberField, md: 3, min : 0, unit: "MBytes", placeholder: "None",
145
label: "Min. available disk space", helperText: "Reject uploads that don't comply" },
146
{ k: 'keep_session_alive', comp: BoolField, helperText: "Keeps you logged in while the page is left open and the computer is on" },
147
- { k: 'zip_calculate_size_for_seconds', comp: NumberField, label: "Calculate ZIP size for", unit: "seconds",
147
+ { k: 'session_duration', comp: NumberField, sm: 3, unit: "seconds" },
148
+ { k: 'zip_calculate_size_for_seconds', comp: NumberField, sm: 3, label: "Calculate ZIP size for", unit: "seconds",
149
helperText: "If time is not enough, the browser will not show download percentage" },
150
{ k: 'admin_net', comp: NetmaskField, label: "Admin-panel accessible from", placeholder: "any address", md: 12,
151
helperText: h(Fragment, {}, "IP address of browser machine. ", h(WildcardsSupported))
src/api.auth.ts
+3
-9
@@ -6,19 +6,13 @@ import { ApiError, ApiHandler } from './apiMiddleware'
6
import { SRPParameters, SRPRoutines, SRPServerSession, SRPServerSessionStep1 } from 'tssrp6a'
7
import {
8
ADMIN_URI,
9
- SESSION_DURATION,
10
- HTTP_UNAUTHORIZED,
11
- HTTP_BAD_REQUEST,
12
- HTTP_SERVER_ERROR,
13
- HTTP_NOT_ACCEPTABLE,
14
- HTTP_CONFLICT,
15
- HTTP_NOT_FOUND
9
+ HTTP_UNAUTHORIZED, HTTP_BAD_REQUEST, HTTP_SERVER_ERROR, HTTP_NOT_ACCEPTABLE, HTTP_CONFLICT, HTTP_NOT_FOUND
10
} from './const'
11
import { randomId } from './misc'
12
import Koa from 'koa'
13
import { changeSrpHelper, changePasswordHelper } from './api.helpers'
14
import { ctxAdminAccess } from './adminApis'
21
-import { prepareState } from './middlewares'
15
+import { prepareState, sessionDuration } from './middlewares'
16
import { defineConfig } from './config'
17
18
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters())
@@ -43,7 +37,7 @@ async function loggedIn(ctx:Koa.Context, username: string | false) {
37
38
function makeExp() {
39
return !keepSessionAlive.get() ? undefined
46
- : { exp: new Date(Date.now() + SESSION_DURATION) }
40
+ : { exp: new Date(Date.now() + sessionDuration.compiled()) }
41
}
42
43
export const login: ApiHandler = async ({ username, password }, ctx) => {
src/const.ts
-1
@@ -15,7 +15,6 @@ 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 = Number(process.env.SESSION_DURATION) * 1000 || DAY
18
19
export const API_VERSION = 8.1 // entry.uri + script.plugin + absolute frontend_*
20
export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
src/index.ts
+2
-2
@@ -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, SESSION_DURATION } from './const'
7
+import { API_URI, DEV } from './const'
8
import { frontEndApis } from './frontEndApis'
9
import { log } from './log'
10
import { pluginsMiddleware } from './plugins'
@@ -31,7 +31,7 @@ ok(_.intersection(Object.keys(frontEndApis), Object.keys(adminApis)).length ===
31
const keys = process.env.COOKIE_SIGN_KEYS?.split(',') || [randomId(30)]
32
export const app = new Koa({ keys })
33
app.use(someSecurity)
34
- .use(session({ key: 'hfs_$id', signed: true, rolling: true, maxAge: SESSION_DURATION }, app))
34
+ .use(session({ key: 'hfs_$id', signed: true, rolling: true }, app))
35
.use(prepareState)
36
.use(headRequests)
37
.use(log())
src/middlewares.ts
+4
@@ -42,6 +42,8 @@ import { getLangData } from './lang'
42
43
const forceHttps = defineConfig('force_https', true)
44
const ignoreProxies = defineConfig('ignore_proxies', false)
45
+export const sessionDuration = defineConfig('session_duration', Number(process.env.SESSION_DURATION) || DAY/1000,
46
+ v => v * 1000)
47
48
export const gzipper = compress({
49
threshold: 2048,
@@ -217,6 +219,8 @@ export function getProxyDetected() {
219
&& { from: proxyDetected.ip, for: proxyDetected.get('X-Forwarded-For') }
220
}
221
export const prepareState: Koa.Middleware = async (ctx, next) => {
222
+ if (ctx.session)
223
+ ctx.session.maxAge = sessionDuration.compiled()
224
// calculate these once and for all
225
ctx.state.account = await getHttpAccount(ctx) ?? getAccount(ctx.session?.username, false)
226
const conn = ctx.state.connection = socket2connection(ctx.socket)