Skip to content

Commit 801fd2e

Browse files
authored
Merge pull request #52 from axa-group/em/events
Support Nodejs 14 LTS... and minor doco updates
2 parents 9f2eb55 + 3f20695 commit 801fd2e

File tree

7 files changed

+83
-47
lines changed

7 files changed

+83
-47
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macos-latest, windows-latest]
13-
node-version: [^10, ^12]
13+
node-version: [^10.13, ^12.13, ^14.15]
1414
runs-on: ${{ matrix.os }}
1515
steps:
1616
- uses: actions/checkout@v2

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
66

7+
## [3.0.2](https://github.com/axa-group/oauth2-mock-server/compare/v3.0.1...v3.0.2) — 2020-10-29
8+
9+
### Added
10+
11+
- Support Nodejs 14.15 LTS
12+
713
## [3.0.1](https://github.com/axa-group/oauth2-mock-server/compare/v3.0.0...v3.0.1) — 2020-10-23
814

915
### Fixed

README.md

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The purpose of this package is to provide an easily configurable OAuth 2 server,
1818

1919
## How to use
2020

21+
### Installation
22+
2123
Add it to your Node.js project as a development dependency:
2224

2325
With yarn...
@@ -30,6 +32,8 @@ yarn add -D oauth2-mock-server
3032
npm install --save-dev oauth2-mock-server
3133
```
3234

35+
### Quickstart
36+
3337
Here is an example for creating and running a server instance with a single random RSA key:
3438

3539
```js
@@ -84,54 +88,67 @@ request.get(
8488
);
8589
```
8690

91+
### Customization hooks
92+
8793
It also provides a convenient way, through event emitters, to programmatically customize:
8894

8995
- The JWT access token
96+
97+
```js
98+
//Modify the expiration time on next token produced
99+
service.once('beforeTokenSigning', (token, _req) => {
100+
const timestamp = Math.floor(Date.now() / 1000);
101+
token.payload.exp = timestamp + 400;
102+
});
103+
```
104+
105+
```js
106+
//Add the client ID to a token
107+
const basicAuth = require('basic-auth');
108+
service.once('beforeTokenSigning', (token, req) => {
109+
const credentials = basicAuth(req);
110+
const clientId = credentials ? credentials.name : req.body.client_id;
111+
token.payload.client_id = clientId;
112+
});
113+
```
114+
90115
- The token endpoint response body and status
91-
- The userinfo endpoint response body and status
92-
- The revoke endpoint response body and status
93116

94-
This is particularly useful when expecting the oidc service to behave in a specific way on one single test.
117+
```js
118+
//Force the oidc service to provide an invalid_grant response on next call to the token endpoint
119+
service.once('beforeResponse', (tokenEndpointResponse, req) => {
120+
tokenEndpointResponse.body = {
121+
error: 'invalid_grant',
122+
};
123+
tokenEndpointResponse.statusCode = 400;
124+
});
125+
```
95126

96-
```js
97-
//Force the oidc service to provide an invalid_grant response on next call to the token endpoint
98-
service.once('beforeResponse', (tokenEndpointResponse, req) => {
99-
tokenEndpointResponse.body = {
100-
error: 'invalid_grant'
101-
};
102-
tokenEndpointResponse.statusCode = 400;
103-
});
127+
- The userinfo endpoint response body and status
104128

105-
//Force the oidc service to provide an error on next call to userinfo endpoint
106-
service.once('beforeUserinfo', (userInfoResponse, req) => {
107-
userInfoResponse.body = {
108-
error: 'invalid_token',
109-
error_message: 'token is expired',
110-
};
111-
userInfoResponse.statusCode = 401;
112-
});
129+
```js
130+
//Force the oidc service to provide an error on next call to userinfo endpoint
131+
service.once('beforeUserinfo', (userInfoResponse, req) => {
132+
userInfoResponse.body = {
133+
error: 'invalid_token',
134+
error_message: 'token is expired',
135+
};
136+
userInfoResponse.statusCode = 401;
137+
});
138+
```
113139

114-
//Add the client ID to a token
115-
const basicAuth = require('basic-auth');
116-
service.once('beforeTokenSigning', (token, req) => {
117-
const credentials = basicAuth(req);
118-
const clientId = credentials ? credentials.name : req.body.client_id;
119-
token.payload.client_id = clientId;
120-
});
140+
- The revoke endpoint response body and status
121141

122-
//Modify the expiration time on next token produced
123-
service.issuer.once('beforeSigning', (token) => {
124-
const timestamp = Math.floor(Date.now() / 1000);
125-
token.payload.exp = timestamp + 400;
126-
});
142+
```js
143+
//Simulates a custom token revocation body
144+
service.once('beforeRevoke', (revokeResponse, req) => {
145+
revokeResponse.body = {
146+
result: 'revoked',
147+
};
148+
});
149+
```
127150

128-
//Simulates a custom token revocation body
129-
service.once('beforeRevoke', (revokeResponse, req) => {
130-
revokeResponse.body = {
131-
result: 'revoked'
132-
};
133-
});
134-
```
151+
This is particularly useful when expecting the oidc service to behave in a specific way on one single test.
135152

136153
## Supported endpoints
137154

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oauth2-mock-server",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "OAuth 2 mock server",
55
"keywords": [
66
"oauth",
@@ -19,7 +19,7 @@
1919
},
2020
"license": "MIT",
2121
"engines": {
22-
"node": "^10.13 || ^12.13",
22+
"node": "^10.13 || ^12.13 || ^14.15",
2323
"yarn": "^1.15.2"
2424
},
2525
"repository": {

src/lib/oauth2-issuer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { JWK } from 'node-jose';
2626
import { JWKStore } from './jwk-store';
2727
import { assertIsAlgorithm, assertIsString } from './helpers';
2828
import type { Header, MutableToken, Payload, ScopesOrTransform } from './types';
29+
import { InternalEvents } from './types';
2930

3031
/**
3132
* Represents an OAuth 2 issuer.
@@ -117,7 +118,7 @@ export class OAuth2Issuer extends EventEmitter {
117118
* @event OAuth2Issuer#beforeSigning
118119
* @param {MutableToken} token The JWT header and payload.
119120
*/
120-
this.emit('beforeSigning', token);
121+
this.emit(InternalEvents.BeforeSigning, token);
121122

122123
const options: jwt.SignOptions = {
123124
algorithm: arguments.length === 0 || signed ? getKeyAlg(key) : 'none',

src/lib/oauth2-service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import type {
3535
MutableToken,
3636
ScopesOrTransform,
3737
} from './types';
38+
import { InternalEvents, PublicEvents } from './types';
3839

3940
const OPENID_CONFIGURATION_PATH = '/.well-known/openid-configuration';
4041
const TOKEN_ENDPOINT_PATH = '/token';
@@ -93,15 +94,15 @@ export class OAuth2Service extends EventEmitter {
9394
expiresIn: number,
9495
req: IncomingMessage
9596
): string {
96-
this.issuer.once('beforeSigning', (token: MutableToken) => {
97+
this.issuer.once(InternalEvents.BeforeSigning, (token: MutableToken) => {
9798
/**
9899
* Before token signing event.
99100
*
100101
* @event OAuth2Service#beforeTokenSigning
101102
* @param {MutableToken} token The unsigned JWT header and payload.
102103
* @param {IncomingMessage} req The incoming HTTP request.
103104
*/
104-
this.emit('beforeTokenSigning', token, req);
105+
this.emit(PublicEvents.BeforeTokenSigning, token, req);
105106
});
106107

107108
return this.issuer.buildToken(
@@ -262,7 +263,7 @@ export class OAuth2Service extends EventEmitter {
262263
* @param {MutableResponse} response The response body and status code.
263264
* @param {IncomingMessage} req The incoming HTTP request.
264265
*/
265-
this.emit('beforeResponse', tokenEndpointResponse, req);
266+
this.emit(PublicEvents.BeforeResponse, tokenEndpointResponse, req);
266267

267268
return res
268269
.status(tokenEndpointResponse.statusCode)
@@ -327,7 +328,7 @@ export class OAuth2Service extends EventEmitter {
327328
* @param {MutableResponse} response The response body and status code.
328329
* @param {IncomingMessage} req The incoming HTTP request.
329330
*/
330-
this.emit('beforeUserinfo', userInfoResponse, req);
331+
this.emit(PublicEvents.BeforeUserinfo, userInfoResponse, req);
331332

332333
res.status(userInfoResponse.statusCode).json(userInfoResponse.body);
333334
};
@@ -345,7 +346,7 @@ export class OAuth2Service extends EventEmitter {
345346
* @param {MutableResponse} response The response body and status code.
346347
* @param {IncomingMessage} req The incoming HTTP request.
347348
*/
348-
this.emit('beforeRevoke', revokeResponse, req);
349+
this.emit(PublicEvents.BeforeRevoke, revokeResponse, req);
349350

350351
return res.status(revokeResponse.statusCode).json(revokeResponse.body);
351352
};

src/lib/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ export type ScopesOrTransform = string | string[] | JwtTransform;
4444
export interface JwtTransform {
4545
(header: Header, payload: Payload): void;
4646
}
47+
48+
export enum PublicEvents {
49+
BeforeTokenSigning = 'beforeTokenSigning',
50+
BeforeResponse = 'beforeResponse',
51+
BeforeUserinfo = 'beforeUserinfo',
52+
BeforeRevoke = 'beforeRevoke',
53+
}
54+
55+
export enum InternalEvents {
56+
BeforeSigning = 'beforeSigning',
57+
}

0 commit comments

Comments
 (0)