Last active
July 1, 2022 16:06
-
-
Save lflucasferreira/51463491503b28e29108a605c78d7d41 to your computer and use it in GitHub Desktop.
How to Automate OAuth2 Token Renewal in Postman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ########################################################################### | |
// Define required packages | |
// ########################################################################### | |
let moment = require("moment"); // Used to create/format date | |
// ########################################################################### | |
// Define variables | |
// ########################################################################### | |
let AUTH = pm.environment.get("AUTH"); // Get current JWT token value | |
let AUTH_CREATED_DATE = pm.environment.get("AUTH_CREATED_DATE"); // Get JWT token creation date | |
let AUTH_EXPIRES_IN = pm.environment.get("AUTH_EXPIRES_IN"); // Get JWT token expiration time (in seconds) | |
let AUTH_TYPE = pm.environment.get("AUTH_TYPE"); // Get JWT token type (e.g. Bearer) | |
let NOW = moment(); // Create a new datetime | |
let AUTH_EXPIRED_DATE; // Define this variable for the expiration calculation | |
let AUTH_CLIENT_ID = pm.environment.get("AUTH_CLIENT_ID") // Get JWT token client ID | |
let AUTH_USERNAME = pm.environment.get("AUTH_USERNAME"); // Get JWT token username | |
let AUTH_PASSWORD = pm.environment.get("AUTH_PASSWORD"); // Get JWT token password | |
// ########################################################################### | |
// Check if there is any environment variable empty or undefined | |
// ########################################################################### | |
if (!AUTH_CREATED_DATE || !AUTH_TYPE || !AUTH_EXPIRES_IN || !AUTH) { | |
AUTH_EXPIRED_DATE = NOW; | |
} else { | |
AUTH_EXPIRED_DATE = moment(new Date(AUTH_CREATED_DATE)).add(AUTH_EXPIRES_IN, 'seconds'); | |
} | |
// ########################################################################### | |
// Generate Token | |
// ########################################################################### | |
if(AUTH_EXPIRED_DATE <= NOW) { | |
console.info("Updating JWT Token... wait!"); | |
pm.sendRequest({ | |
url: "PUT_YOUR_URL_HERE", | |
method: "POST", | |
header: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
}, | |
body: { | |
mode: "urlencoded", | |
urlencoded: [ | |
{key: "username", value: `${AUTH_USERNAME}`, disabled: false}, | |
{key: "password", value: `${AUTH_PASSWORD}`, disabled: false}, | |
{key: "grant_type", value: "password", disabled: false}, | |
{key: "client_id", value: `${AUTH_CLIENT_ID}`, disabled: false}, | |
{key: "response_type", value: "token id_token", disabled: false}, | |
{key: "scope", value: `openid ${AUTH_CLIENT_ID} offline_access`, disabled: false}, | |
] | |
} | |
}, function (error, response) { | |
if (error !== null) { | |
console.error(error); | |
} else { | |
pm.environment.set("AUTH", response.json().access_token); | |
pm.environment.set("AUTH_TYPE", response.json().token_type); | |
pm.environment.set("AUTH_CREATED_DATE", response.headers.get("Date")) | |
pm.environment.set("AUTH_EXPIRES_IN", response.json().expires_in); | |
console.info("The AUTH environment was updated"); | |
pm.test('The AUTH TOKEN was created', function() { | |
pm.expect(response.code).to.be.equal(200); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment