Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -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
}
```
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ export type ConfigUser = {
};

export type Config = {
port: number;
realm: string;
users: ConfigUser[];
tokenExpiresIn: number;
};

export function getConfig(): Config {
const defaultConfig = <Config>{
port: 5000,
realm: process.env.REALM || 'my-project',
users: [
{
Expand All @@ -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');
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
});

Expand All @@ -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);
Expand Down