Skip to content

Commit

Permalink
add jwt expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Jan 22, 2024
1 parent 7966a8b commit 9d85996
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ rules:
"@typescript-eslint/consistent-type-imports": off
"@typescript-eslint/no-misused-promises": off
"@typescript-eslint/no-non-null-assertion": off
"@typescript-eslint/no-floating-promises": off

"react/react-in-jsx-scope": off
9 changes: 7 additions & 2 deletions apps/cli/dev_server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import startServer from './src/commands/start_server';
import exitWithMessage from './src/utils/exit_with_message';
import { Mode } from './src/config';
import { DEFAULT_CONFIG, Mode } from './src/config';

const data = process.env.CICADA_DATA;
if (!data) {
Expand All @@ -12,4 +12,9 @@ if (!data) {
const absoluteData = path.isAbsolute(data!)
? data!
: path.resolve(process.cwd(), data!);
startServer({ mode: Mode.DEVELOPMENT, port: 8000, data: absoluteData });
startServer({
mode: Mode.DEVELOPMENT,
port: 8000,
data: absoluteData,
jwtExpiry: DEFAULT_CONFIG.jwtExpiry,
});
8 changes: 6 additions & 2 deletions apps/cli/src/commands/start_server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ import { getFormApp } from './form_app';
import { getPwaApp } from './pwa_app';
import { getBaseApp } from './base_app';
import i18n from './middlewares/i18n';
import ms from 'ms';

function printConfig() {
const config = getConfig();
const printConfigKeys: (keyof Config)[] = ['mode', 'port', 'data'];
const printConfigKeys: Array<keyof Config> = ['mode', 'port', 'data'];
console.log('---');
for (const key of printConfigKeys) {
console.log(`${key}: ${config[key]}`);
}
console.log(`jwtExpiry: ${ms(config.jwtExpiry)}`);
console.log('---');
}

export default async ({
mode,
port,
data,
jwtExpiry,
}: {
mode: Mode;
port: number;
data: string;
jwtExpiry: number;
}) => {
updateConfig({ mode, port, data });
updateConfig({ mode, port, data, jwtExpiry });
printConfig();

await initialize();
Expand Down
3 changes: 3 additions & 0 deletions apps/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ export interface Config {

data: string;
port: number;

jwtExpiry: number;
}

export const DEFAULT_CONFIG: Config = {
mode: Mode.PRODUCTION,

data: `${process.cwd()}/cicada`,
port: 8000,
jwtExpiry: 1000 * 60 * 60 * 24 * 180,
};

let config: Config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
Expand Down
12 changes: 10 additions & 2 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import upgradeData from './commands/upgrade_data';
import fixData from './commands/fix_data';
import { FIRST_USER_ID } from './constants';
import { DEFAULT_CONFIG, Mode } from './config';
import ms from 'ms';

const program = new Command()
.name('cicada')
Expand All @@ -25,15 +26,21 @@ program
.option('--mode [mode]', 'development or production')
.option('--data [data]', 'data directory location')
.option('--port [port]', 'port of http server')
.option(
'--jwt-expiry [jwtExpiry]',
'expiry for jwt, use [ms](https://www.npmjs.com/package/ms) to parse string to millisecond',
)
.action(
async ({
mode,
data,
port,
jwtExpiry,
}: {
mode?: Mode;
data?: string;
port?: string;
jwtExpiry?: string;
}) => {
if (mode && !Object.values(Mode).includes(mode)) {
return exitWithMessage(`[ ${mode} ] is not a valid mode`);
Expand All @@ -44,10 +51,11 @@ program
? data
: path.resolve(process.cwd(), data)
: DEFAULT_CONFIG.data;
return startServer({
mode: mode || DEFAULT_CONFIG.mode,
return await startServer({
mode: mode ?? DEFAULT_CONFIG.mode,
data: absoluteData,
port: port ? Number(port) : DEFAULT_CONFIG.port,
jwtExpiry: jwtExpiry ? ms(jwtExpiry) : DEFAULT_CONFIG.jwtExpiry,
});
},
);
Expand Down
12 changes: 6 additions & 6 deletions apps/cli/src/platform/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from 'fs';
import jwt from 'jsonwebtoken';
import generateRandomString from '#/utils/generate_random_string';
import { getJWTSecretFilePath } from '@/config';

const JWT_TTL = 1000 * 60 * 60 * 24 * 180;
import { getConfig, getJWTSecretFilePath } from '@/config';

let secret: string = '';
const getSecret = () => {
Expand All @@ -27,15 +25,17 @@ export function sign({
tokenIdentifier: string;
}) {
return jwt.sign({ userId, tokenIdentifier }, getSecret(), {
expiresIn: JWT_TTL / 1000,
expiresIn: getConfig().jwtExpiry / 1000,
});
}

export function verify(token: string): {
userId: string;
tokenIdentifier: string;
} {
const payload = jwt.verify(token, getSecret());
// @ts-expect-error
const payload = jwt.verify(token, getSecret()) as {
userId: string;
tokenIdentifier: string;
};
return payload;
}
2 changes: 1 addition & 1 deletion apps/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"@/*": ["src/*"]
}
},
"include": ["src/**/*", "../../global.d.ts"]
"include": ["dev_server.ts", "src/**/*", "../../global.d.ts"]
}
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"koa-etag": "^4.0.0",
"koa-send": "^5.0.1",
"md5": "^2.3.0",
"ms": "^2.1.3",
"multiparty": "^4.2.3",
"nanospinner": "^1.1.0",
"negotiator": "^0.6.3",
Expand Down

0 comments on commit 9d85996

Please sign in to comment.