Last active
August 25, 2021 10:41
-
-
Save miton18/628d67dcb7cc65facef9833a348b4099 to your computer and use it in GitHub Desktop.
Postman OVH auth support
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
// environments to define | |
// endpoint -> https://api.ovh.com/1.0 | |
// applicationKey | |
// applicationSecret | |
// consumerKey | |
// pre request script | |
pm.request.headers.add({ | |
key: 'X-Ovh-Application', | |
value: pm.environment.get('applicationKey') | |
}) | |
pm.request.headers.add({ | |
key: 'X-Ovh-Consumer', | |
value: pm.environment.get('consumerKey') | |
}) | |
function timeRelatedHeaders() { | |
var apiTimeDiff = pm.variables.get("apiTimeDiff") | |
var now = Math.round(Date.now()/1000) + apiTimeDiff | |
var toSign = pm.environment.get('applicationSecret') + '+' + pm.environment.get('consumerKey') + '+' + pm.request.method + '+' + pm.variables.replaceIn(pm.request.url.toString()) + '+' + (pm.request.body.toString() || '') + '+' + now; | |
var signature = '$1$' + CryptoJS.SHA1(toSign); | |
pm.request.headers.add({ | |
key: 'X-Ovh-Signature', | |
value: signature | |
}) | |
pm.request.headers.add({ | |
key: 'X-Ovh-Timestamp', | |
value: now | |
}) | |
} | |
if (!pm.collectionVariables.has('apiTimeDiff')) { | |
pm.sendRequest({ | |
url: "https://api.ovh.com/1.0/auth/time", | |
method: "GET", | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8' | |
}, | |
body: {} | |
}, | |
function (err, res) { | |
pm.expect(err).to.not.be.ok; | |
pm.expect(res).to.have.property('code', 200); | |
pm.expect(res).to.have.property('status', 'OK'); | |
var serverTimestamp = res.text(); | |
var apiTimeDiff = serverTimestamp - Math.round(Date.now()/1000); | |
pm.collectionVariables.set('apiTimeDiff', apiTimeDiff) | |
timeRelatedHeaders() | |
}); | |
} else { | |
console.log('skipped') | |
timeRelatedHeaders() | |
} | |
console.debug(pm.request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment