Created
September 21, 2018 17:41
-
-
Save aliasmrchips/f33a812fba1ed9bacc5e516b7f81e6d0 to your computer and use it in GitHub Desktop.
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
var getToken = true; | |
var client_id = pm.environment.get('CLIENT_ID'); | |
var client_secret = pm.environment.get('CLIENT_SECRET'); | |
if (!pm.environment.get('JWT_TOKEN')) { | |
console.log('Token missing') | |
} else { | |
var decoded = (pm.environment.get('JWT_TOKEN')).split('.')[1]; | |
var claims = JSON.parse(Buffer.from(decoded, 'base64')); | |
var ts = Math.round((new Date()).getTime() / 1000); | |
if (ts + 500 < claims.exp) { | |
getToken = false; | |
} | |
} | |
if (getToken === true) { | |
console.log('Token expired missing') | |
var params = { | |
'scope': ['openid', 'name', 'groups', 'org', 'email'].join(' '), | |
'client_id': client_id, | |
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', | |
'target': client_id, | |
'api_type': 'app', | |
'refresh_token': client_secret | |
} | |
const echoPostRequest = { | |
url: 'https://accounts.descarteslabs.com/token', | |
method: 'POST', | |
header: 'Content-Type:application/json', | |
body: { | |
mode: 'application/json', | |
raw: JSON.stringify(params) | |
} | |
}; | |
var response = pm.sendRequest(echoPostRequest, function (err, res) { | |
console.log(err ? err : res.json()); | |
if (err === null) { | |
console.log('Saving the token') | |
var responseJson = res.json(); | |
pm.environment.set('JWT_TOKEN', responseJson.id_token); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment