parent
180aa54800
commit
de2e8e45e1
5 changed files with 63 additions and 2 deletions
@ -0,0 +1 @@ |
||||
export const VNAME_SESSION_SECRET = 'striker-ui-api::session::secret'; |
@ -0,0 +1,2 @@ |
||||
export const ECODE_DROP_PRIVILEGES = 1; |
||||
export const ECODE_SESSION_SECRET = 2; |
@ -1,2 +1,4 @@ |
||||
export * from './AN_VARIABLE_NAME_LIST'; |
||||
export * from './EXIT_CODE_LIST'; |
||||
export * from './PROCESS_OWNER'; |
||||
export * from './SERVER_PORT'; |
||||
|
@ -0,0 +1,56 @@ |
||||
import assert from 'assert'; |
||||
|
||||
import { ECODE_SESSION_SECRET, VNAME_SESSION_SECRET } from './consts'; |
||||
|
||||
import { dbQuery, variable } from './accessModule'; |
||||
import { openssl, stderr, stdout } from './shell'; |
||||
|
||||
export const getSessionSecret = (): string => { |
||||
let sessionSecret: string; |
||||
|
||||
try { |
||||
const rows: [sessionSecret: string][] = dbQuery( |
||||
`SELECT variable_value
|
||||
FROM variables |
||||
WHERE variable_name = '${VNAME_SESSION_SECRET}';`,
|
||||
).stdout; |
||||
|
||||
assert(rows.length > 0, 'No existing session secret found.'); |
||||
|
||||
({ |
||||
0: [sessionSecret], |
||||
} = rows); |
||||
|
||||
stdout('Found an existing session secret.'); |
||||
|
||||
return sessionSecret; |
||||
} catch (queryError) { |
||||
stderr(`Failed to get session secret from database; CAUSE: ${queryError}`); |
||||
} |
||||
|
||||
try { |
||||
sessionSecret = openssl('rand', '-base64', '32').trim(); |
||||
|
||||
stdout('Generated a new session secret.'); |
||||
} catch (sysError) { |
||||
stderr(`Failed to generate session secret; CAUSE: ${sysError}`); |
||||
|
||||
process.exit(ECODE_SESSION_SECRET); |
||||
} |
||||
|
||||
try { |
||||
const vuuid = variable({ |
||||
file: __filename, |
||||
variable_name: VNAME_SESSION_SECRET, |
||||
variable_value: sessionSecret, |
||||
}); |
||||
|
||||
stdout(`Recorded session secret as variable identified by ${vuuid}.`); |
||||
} catch (subError) { |
||||
stderr(`Failed to record session secret; CAUSE: ${subError}`); |
||||
|
||||
process.exit(ECODE_SESSION_SECRET); |
||||
} |
||||
|
||||
return sessionSecret; |
||||
}; |
Loading…
Reference in new issue