-
-
Save arvindkgs/21c6c120f172a4362fbb92b677dc8484 to your computer and use it in GitHub Desktop.
[Postman] Postman pre-request scripts that populates `currentAccessToken` with Bearer token for oauth authentication when switching between cluster by using environment variables.
This file contains 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
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']); | |
} | |
}); | |
} |
This file contains 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
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