@@ -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+
2123Add it to your Node.js project as a development dependency:
2224
2325With yarn...
@@ -30,6 +32,8 @@ yarn add -D oauth2-mock-server
3032npm install --save-dev oauth2-mock-server
3133```
3234
35+ ### Quickstart
36+
3337Here 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+
8793It 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
0 commit comments