From 58fdc2575b4b7a4aef29e71e225fd417be549880 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Thu, 3 Nov 2022 16:10:49 +0100 Subject: [PATCH 1/2] feat: make port and token-expires-in configurable --- README.md | 4 +++- src/config.ts | 4 ++++ src/index.ts | 9 ++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aa43cf8..d3b7444 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); From 53dab136249d08d2312f6bd47406b914683181cf Mon Sep 17 00:00:00 2001 From: Anbraten Date: Thu, 3 Nov 2022 16:13:35 +0100 Subject: [PATCH 2/2] fix format --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d3b7444..47ed075 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,6 @@ You can adjust the default configuration by placing a file `oauth-mock-server.js "name": "Herbert" } ], - "tokenExpiresIn": 86400, // 24 hours in seconds + "tokenExpiresIn": 86400 // 24 hours in seconds } ```