Skip to content

Instantly share code, notes, and snippets.

@arvindkgs

arvindkgs/oauth.js Secret

Last active Jan 12, 2021
Embed
What would you like to do?
[Postman] Postman pre-request scripts that populates `currentAccessToken` with Bearer token for oauth authentication when switching between cluster by using environment variables.
const echoPostRequest = {
url: pm.environment.get('cluster')+'/auth/token',
method: 'POST',
header: 'Content-Type:application/json',
body: {
mode: 'application/json',
raw: JSON.stringify(
{
username:pm.environment.get('username'),
password:pm.environment.get('password'),
'grant-type':'client_credentials'
})
}
};
var getToken = true;
if(!pm.environment.get('clusterMap')){
pm.environment.set('clusterMap', new Object());
}
var clusterMap = pm.environment.get('clusterMap');
if(!clusterMap.hasOwnProperty(pm.environment.get('cluster'))){
clusterMap[pm.environment.get('rainier-cluster')] = new Object();
}
var credMap = clusterMap[pm.environment.get('cluster')];
if(!credMap.hasOwnProperty(pm.environment.get('username'))){
credMap[pm.environment.get('username')] = new Object();
}
var tokens = credMap[pm.environment.get('username')];
if(!tokens.hasOwnProperty('currentAccessToken') ||
!tokens.hasOwnProperty('accessTokenExpiry')){
console.log('Token or expiry date are missing')
}
else if(tokens.accessTokenExpiry <= (new Date()).getTime()){
console.log('Token is expired')
}
else{
getToken = false;
console.log('Token and expiry date are all good');
pm.environment.set('currentAccessToken', tokens['currentAccessToken']);
pm.environment.set('accessTokenExpiry', tokens['accessTokenExpiry']);
}
if (getToken === true) {
pm.sendRequest(echoPostRequest, function (err, res) {
console.log(err ? err : res.json());
if (err === null) {
console.log('Saving the token and expiry date')
var responseJson = res.json();
// pm.environment.set('currentAccessToken', responseJson.access_token)
tokens['currentAccessToken'] = responseJson.access_token;
var expiryDate = new Date();
expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);
// pm.environment.set('accessTokenExpiry', expiryDate.getTime());
tokens['accessTokenExpiry'] = expiryDate.getTime();
pm.environment.set('currentAccessToken', tokens['currentAccessToken']);
pm.environment.set('accessTokenExpiry', tokens['accessTokenExpiry']);
}
});
}
const echoPostRequest = {
url: pm.environment.get('my-cluster')+'/iam/auth/token',
method: 'POST',
header: 'Content-Type:application/json',
body: {
mode: 'application/json',
raw: JSON.stringify(
{
'client_id':pm.environment.get('service_client_id'),
'client_secret':pm.environment.get('service_client_secret'),
'grant_type':'client_credentials'
})
}
};
var getToken = true;
if(!pm.environment.get('clusterMap')){
pm.environment.set('clusterMap', new Object());
}
var clusterMap = pm.environment.get('clusterMap');
if(!clusterMap.hasOwnProperty(pm.environment.get('my-cluster'))){
clusterMap[pm.environment.get('rmy-cluster')] = new Object();
}
var credMap = clusterMap[pm.environment.get('my-cluster')];
if(!credMap.hasOwnProperty(pm.environment.get('service_client_id'))){
credMap[pm.environment.get('service_client_id')] = new Object();
}
var tokens = credMap[pm.environment.get('service_client_id')];
if(!tokens.hasOwnProperty('currentServiceAccessToken') ||
!tokens.hasOwnProperty('accessServiceTokenExpiry')){
console.log('Token or expiry date are missing')
}
else if(tokens.accessServiceTokenExpiry <= (new Date()).getTime()){
console.log('Token is expired')
}
else{
getToken = false;
console.log('Token and expiry date are all good');
pm.environment.set('currentServiceAccessToken', tokens['currentServiceAccessToken']);
pm.environment.set('accessServiceTokenExpiry', tokens['accessServiceTokenExpiry']);
}
if (getToken === true) {
pm.sendRequest(echoPostRequest, function (err, res) {
console.log(err ? err : res.json());
if (err === null) {
console.log('Saving the token and expiry date')
var responseJson = res.json();
// pm.environment.set('currentServiceAccessToken', responseJson.access_token)
tokens['currentServiceAccessToken'] = responseJson.access_token;
var expiryDate = new Date();
expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);
// pm.environment.set('accessServiceTokenExpiry', expiryDate.getTime());
tokens['accessServiceTokenExpiry'] = expiryDate.getTime();
pm.environment.set('currentServiceAccessToken', tokens['currentServiceAccessToken']);
pm.environment.set('accessServiceTokenExpiry', tokens['accessServiceTokenExpiry']);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment