|
|
|
@ -1,16 +1,14 @@ |
|
|
|
|
import assert from 'assert'; |
|
|
|
|
import expressSession, { |
|
|
|
|
SessionData, |
|
|
|
|
Store as BaseSessionStore, |
|
|
|
|
} from 'express-session'; |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
awrite, |
|
|
|
|
dbQuery, |
|
|
|
|
getLocalHostUUID, |
|
|
|
|
timestamp, |
|
|
|
|
} from './lib/accessModule'; |
|
|
|
|
import { DELETED } from './lib/consts'; |
|
|
|
|
|
|
|
|
|
import { getLocalHostUUID, query, timestamp, write } from './lib/accessModule'; |
|
|
|
|
import { getSessionSecret } from './lib/getSessionSecret'; |
|
|
|
|
import { stderr, stdout, stdoutVar, uuidgen } from './lib/shell'; |
|
|
|
|
import { stderr, stdout, stdoutVar, uuid } from './lib/shell'; |
|
|
|
|
|
|
|
|
|
const DEFAULT_COOKIE_ORIGINAL_MAX_AGE = 3600000; |
|
|
|
|
|
|
|
|
@ -19,38 +17,33 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
super(options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public destroy( |
|
|
|
|
public async destroy( |
|
|
|
|
sid: string, |
|
|
|
|
done?: ((err?: unknown) => void) | undefined, |
|
|
|
|
): void { |
|
|
|
|
): Promise<void> { |
|
|
|
|
stdout(`Destroy session ${sid}`); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
awrite(`DELETE FROM sessions WHERE session_uuid = '${sid}';`, { |
|
|
|
|
onClose({ wcode }) { |
|
|
|
|
if (wcode !== 0) { |
|
|
|
|
stderr( |
|
|
|
|
`SQL script failed during destroy session ${sid}; code: ${wcode}`, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onError(error) { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in destroy session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
const wcode = await write( |
|
|
|
|
`UPDATE sessions SET session_salt = '${DELETED}' WHERE session_uuid = '${sid}';`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert(wcode === 0, `Write exited with code ${wcode}`); |
|
|
|
|
} catch (error) { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in destroy session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return done?.call(null, error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return done?.call(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public get( |
|
|
|
|
public async get( |
|
|
|
|
sid: string, |
|
|
|
|
done: (err: unknown, session?: SessionData | null | undefined) => void, |
|
|
|
|
): void { |
|
|
|
|
): Promise<void> { |
|
|
|
|
stdout(`Get session ${sid}`); |
|
|
|
|
|
|
|
|
|
let rows: [ |
|
|
|
@ -60,7 +53,7 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
][]; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
rows = dbQuery( |
|
|
|
|
rows = await query( |
|
|
|
|
`SELECT
|
|
|
|
|
s.session_uuid, |
|
|
|
|
u.user_uuid, |
|
|
|
@ -69,7 +62,7 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
JOIN users AS u |
|
|
|
|
ON s.session_user_uuid = u.user_uuid |
|
|
|
|
WHERE s.session_uuid = '${sid}';`,
|
|
|
|
|
).stdout; |
|
|
|
|
); |
|
|
|
|
} catch (queryError) { |
|
|
|
|
return done(queryError); |
|
|
|
|
} |
|
|
|
@ -90,20 +83,18 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
maxAge: cookieMaxAge, |
|
|
|
|
originalMaxAge: DEFAULT_COOKIE_ORIGINAL_MAX_AGE, |
|
|
|
|
}, |
|
|
|
|
passport: { |
|
|
|
|
user: userUuid, |
|
|
|
|
}, |
|
|
|
|
passport: { user: userUuid }, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return done(null, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public set( |
|
|
|
|
public async set( |
|
|
|
|
sid: string, |
|
|
|
|
session: SessionData, |
|
|
|
|
done?: ((err?: unknown) => void) | undefined, |
|
|
|
|
): void { |
|
|
|
|
stdout(`Set session ${sid}`); |
|
|
|
|
): Promise<void> { |
|
|
|
|
stdoutVar({ session }, `Set session ${sid}`); |
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
passport: { user: userUuid }, |
|
|
|
@ -113,7 +104,7 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
const localHostUuid = getLocalHostUUID(); |
|
|
|
|
const modifiedDate = timestamp(); |
|
|
|
|
|
|
|
|
|
awrite( |
|
|
|
|
const wcode = await write( |
|
|
|
|
`INSERT INTO
|
|
|
|
|
sessions ( |
|
|
|
|
session_uuid, |
|
|
|
@ -131,56 +122,39 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
'${modifiedDate}' |
|
|
|
|
) |
|
|
|
|
ON CONFLICT (session_uuid) |
|
|
|
|
DO UPDATE SET session_host_uuid = '${localHostUuid}', |
|
|
|
|
modified_date = '${modifiedDate}';`,
|
|
|
|
|
{ |
|
|
|
|
onClose: ({ wcode }) => { |
|
|
|
|
if (wcode !== 0) { |
|
|
|
|
stderr( |
|
|
|
|
`SQL script failed during set session ${sid}; code: ${wcode}`, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onError: (error) => { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in set session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
DO UPDATE SET modified_date = '${modifiedDate}';`,
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert(wcode === 0, `Write exited with code ${wcode}`); |
|
|
|
|
} catch (error) { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in set session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return done?.call(null, error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return done?.call(null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public touch( |
|
|
|
|
public async touch( |
|
|
|
|
sid: string, |
|
|
|
|
session: SessionData, |
|
|
|
|
done?: ((err?: unknown) => void) | undefined, |
|
|
|
|
): void { |
|
|
|
|
stdout(`Touch session ${sid}`); |
|
|
|
|
): Promise<void> { |
|
|
|
|
stdoutVar({ session }, `Touch session ${sid}`); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
awrite( |
|
|
|
|
const wcode = await write( |
|
|
|
|
`UPDATE sessions SET modified_date = '${timestamp()}' WHERE session_uuid = '${sid}';`, |
|
|
|
|
{ |
|
|
|
|
onClose: ({ wcode }) => { |
|
|
|
|
if (wcode !== 0) { |
|
|
|
|
stderr( |
|
|
|
|
`SQL script failed during touch session ${sid}; code: ${wcode}`, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onError: (error) => { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in touch session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
assert(wcode === 0, `Write exited with code ${wcode}`); |
|
|
|
|
} catch (error) { |
|
|
|
|
stderr( |
|
|
|
|
`Failed to complete DB write in touch session ${sid}; CAUSE: ${error}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return done?.call(null, error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -201,19 +175,18 @@ export class SessionStore extends BaseSessionStore { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const session = expressSession({ |
|
|
|
|
cookie: { maxAge: DEFAULT_COOKIE_ORIGINAL_MAX_AGE }, |
|
|
|
|
genid: ({ path }) => { |
|
|
|
|
const sid = uuidgen('--random').trim(); |
|
|
|
|
|
|
|
|
|
stdout(`Generated session identifier ${sid}; request.path=${path}`); |
|
|
|
|
|
|
|
|
|
return sid; |
|
|
|
|
}, |
|
|
|
|
resave: false, |
|
|
|
|
saveUninitialized: false, |
|
|
|
|
secret: getSessionSecret(), |
|
|
|
|
store: new SessionStore(), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
export default session; |
|
|
|
|
export default (async () => |
|
|
|
|
expressSession({ |
|
|
|
|
cookie: { maxAge: DEFAULT_COOKIE_ORIGINAL_MAX_AGE }, |
|
|
|
|
genid: ({ path }) => { |
|
|
|
|
const sid = uuid(); |
|
|
|
|
|
|
|
|
|
stdout(`Generated session identifier ${sid}; request.path=${path}`); |
|
|
|
|
|
|
|
|
|
return sid; |
|
|
|
|
}, |
|
|
|
|
resave: false, |
|
|
|
|
saveUninitialized: false, |
|
|
|
|
secret: await getSessionSecret(), |
|
|
|
|
store: new SessionStore(), |
|
|
|
|
}))(); |
|
|
|
|