diff --git a/README.md b/README.md index aa43cf8..47ed075 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ You can adjust the default configuration by placing a file `oauth-mock-server.js ```json { + "port": 5000, "realm": "my-project", "users": [ { @@ -30,6 +31,7 @@ You can adjust the default configuration by placing a file `oauth-mock-server.js "email": "her@bert.de", "name": "Herbert" } - ] + ], + "tokenExpiresIn": 86400 // 24 hours in seconds } ``` diff --git a/src/config.ts b/src/config.ts index 55e9932..6a78696 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,12 +10,15 @@ export type ConfigUser = { }; export type Config = { + port: number; realm: string; users: ConfigUser[]; + tokenExpiresIn: number; }; export function getConfig(): Config { const defaultConfig = { + port: 5000, realm: process.env.REALM || 'my-project', users: [ { @@ -37,6 +40,7 @@ export function getConfig(): Config { name: 'Herbert', }, ], + tokenExpiresIn: 24 * 60 * 60, // 24 hours in seconds }; const configPath = path.join(cwd(), 'oauth-mock-server.json'); diff --git a/src/index.ts b/src/index.ts index 0a47e0a..285ca07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -122,13 +122,13 @@ server.post(`/auth/realms/${config.realm}/protocol/openid-connect/token`, async aud: config.realm, }; - const accessToken = jwt.sign(payload, jwtSecret, { expiresIn: '1h' }); + const accessToken = jwt.sign(payload, jwtSecret, { expiresIn: config.tokenExpiresIn }); return { access_token: accessToken, token_type: 'Bearer', id_token: accessToken, - expires_in: 3600, + expires_in: config.tokenExpiresIn, }; }); @@ -146,11 +146,10 @@ server.get(`/auth/realms/${config.realm}/protocol/openid-connect/logout`, async }); async function start() { - const port = 5000; // TODO: support custom port try { // eslint-disable-next-line no-console - console.log(`Starting server http://localhost:${port} ...`); - await server.listen({ port, host: '0.0.0.0' }); + console.log(`Starting server http://localhost:${config.port} ...`); + await server.listen({ port: config.port, host: '0.0.0.0' }); } catch (err) { server.log.error(err); process.exit(1);