This project aims to show how to manage Oauth token lifecycle. You could freely reuse the token module in your project. It use Got to handle http requests process.
Based on openid-client to use it in your project, you need to add a dependency to openid-client, and then download the files token.js and retry.js from this project, and add it in yours, feel free to adapt it if needed.
-
Clone the repo: git clone https://github.com/Orange-OpenSource/oauth2-token-manager-for-nodeJS.git
-
Goes to the project folder, and the install project dependencies
With Yarn
yarn install
Or NPM
npm install
-
Subscribe to an API for this sample choose An SMS API
-
Get your client id and secret provided to you
-
Set your client id, secret, API hostname, token endpoint within
.env -
Run the script
With Yarn
yarn run start
Or NPM
npm run start
-
Open http://localhost:3000 in your browser, and voilà.
To check if token calls work, you could try to sent you an SMS phone_number format without country number, neither the 0: http://localhost:3000/sms?phone_number=623456789&message=mylocalappwork
token.js module use environnement variables (loaded via dotenv in that sample project):
- CLIENT_ID
- CLIENT_SECRET
- DISCOVER_URL (optionnal) e.g
DISCOVER_URL = "https://GATEWAY/oauth/v3/.well-known/oauth-authorization-server" - TOKEN_ENDPOINT (optionnal if DISCOVER_URL is provided)
- API_HOSTNAME Hostname to join the API without protocol e.g
GATEWAY - SCOPES (optionnal) Each requested scopes needed separated by a space
When it's done, you could use the module in any file of your project by importing the token and retry module, and call getAccessToken function. This function return a promise, as an example of how to use it you could take a look at sms.js.
Retry module allow to manage token expiration and then intercept the error to allow refreshing token and retrying the request
For any question or remark, please feel free to open an issue