Skip to content

Commit c68d715

Browse files
authored
feat: make port and token-expires-in configurable (#10)
1 parent 12f7038 commit c68d715

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ You can adjust the default configuration by placing a file `oauth-mock-server.js
1010

1111
```json
1212
{
13+
"port": 5000,
1314
"realm": "my-project",
1415
"users": [
1516
{
@@ -30,6 +31,7 @@ You can adjust the default configuration by placing a file `oauth-mock-server.js
3031
"email": "her@bert.de",
3132
"name": "Herbert"
3233
}
33-
]
34+
],
35+
"tokenExpiresIn": 86400 // 24 hours in seconds
3436
}
3537
```

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ export type ConfigUser = {
1010
};
1111

1212
export type Config = {
13+
port: number;
1314
realm: string;
1415
users: ConfigUser[];
16+
tokenExpiresIn: number;
1517
};
1618

1719
export function getConfig(): Config {
1820
const defaultConfig = <Config>{
21+
port: 5000,
1922
realm: process.env.REALM || 'my-project',
2023
users: [
2124
{
@@ -37,6 +40,7 @@ export function getConfig(): Config {
3740
name: 'Herbert',
3841
},
3942
],
43+
tokenExpiresIn: 24 * 60 * 60, // 24 hours in seconds
4044
};
4145

4246
const configPath = path.join(cwd(), 'oauth-mock-server.json');

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ server.post(`/auth/realms/${config.realm}/protocol/openid-connect/token`, async
122122
aud: config.realm,
123123
};
124124

125-
const accessToken = jwt.sign(payload, jwtSecret, { expiresIn: '1h' });
125+
const accessToken = jwt.sign(payload, jwtSecret, { expiresIn: config.tokenExpiresIn });
126126

127127
return {
128128
access_token: accessToken,
129129
token_type: 'Bearer',
130130
id_token: accessToken,
131-
expires_in: 3600,
131+
expires_in: config.tokenExpiresIn,
132132
};
133133
});
134134

@@ -146,11 +146,10 @@ server.get(`/auth/realms/${config.realm}/protocol/openid-connect/logout`, async
146146
});
147147

148148
async function start() {
149-
const port = 5000; // TODO: support custom port
150149
try {
151150
// eslint-disable-next-line no-console
152-
console.log(`Starting server http://localhost:${port} ...`);
153-
await server.listen({ port, host: '0.0.0.0' });
151+
console.log(`Starting server http://localhost:${config.port} ...`);
152+
await server.listen({ port: config.port, host: '0.0.0.0' });
154153
} catch (err) {
155154
server.log.error(err);
156155
process.exit(1);

0 commit comments

Comments
 (0)