fix: session expiration

Massimo Melina committed Jan 7, 2022 at 13:38 UTC c271b393188e93b136a72cb8441d7ab7c4ff4cad
3 files changed +13 -5
src/apis.ts
+4 -2
@@ -7,8 +7,10 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
7 return async (ctx, next) => {
8 const params = ctx.method === 'POST' ? ctx.request.body : ctx.request.query
9 console.debug('API', ctx.method, ctx.path, { ...params })
10 - if (!(ctx.path in apis))
11 - return ctx.throw(404, 'invalid api')
10 + if (!(ctx.path in apis)) {
11 + ctx.body = 'invalid api'
12 + return ctx.status = 404
13 + }
14 const cb = (apis as any)[ctx.path]
15 let res
16 try {
src/frontEndApis.ts
+6 -2
@@ -69,7 +69,7 @@ export const frontEndApis: ApiHandlers = {
69 return ctx.status = 401
70 if (ctx.session)
71 ctx.session.user = user
72 - return true
72 + return makeExp()
73 },
74
75 async logout({}, ctx) {
@@ -80,7 +80,7 @@ export const frontEndApis: ApiHandlers = {
80 },
81
82 async refresh_session({}, ctx) {
83 - return { user: ctx.session?.user }
83 + return { user: ctx.session?.user, ...makeExp() }
84 },
85
86 async change_pwd({ newPassword }, ctx) {
@@ -128,3 +128,7 @@ async function nodeToDirEntry(node: VfsNode): Promise<DirEntry | null> {
128 return null
129 }
130 }
131 +
132 +function makeExp() {
133 + return { exp: new Date(Date.now() + SESSION_DURATION) }
134 +}
src/index.ts
+3 -1
@@ -20,6 +20,8 @@ import { pluginsMiddleware } from './plugins'
20
21 const BUILD_TIMESTAMP = ""
22
23 +export const SESSION_DURATION = 30*60_000
24 +
25 console.log('started', new Date().toLocaleString())
26 const app = new Koa()
27 app.keys = ['hfs-keys-test']
@@ -27,7 +29,7 @@ app.use(session({
29 key: 'hfs_$id',
30 signed: true,
31 rolling: true,
30 - maxAge: 30*60_000,
32 + maxAge: SESSION_DURATION,
33 }, app))
34 app.use(log())
35 app.use(pluginsMiddleware())