Commit 4331bfd5 authored by Sean Hammond's avatar Sean Hammond

Factor out postToTokenUrl() function

Just separating concerns, and also future code is going to reuse this
function as well, reducing duplication.
parent f10b01ec
......@@ -40,23 +40,29 @@ function auth($http, settings) {
};
}
// Post the given data to the tokenUrl endpoint as a form submission.
// Return a Promise for the access token response.
function postToTokenUrl(data) {
data = queryString.stringify(data);
var requestConfig = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
};
return $http.post(tokenUrl, data, requestConfig);
}
// Exchange the JWT grant token for an access token.
// See https://tools.ietf.org/html/rfc7523#section-4
function exchangeToken(grantToken) {
var data = queryString.stringify({
var data = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: grantToken,
});
var requestConfig = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
};
return $http.post(tokenUrl, data, requestConfig)
.then(function (response) {
if (response.status !== 200) {
throw new Error('Failed to retrieve access token');
}
return tokenInfoFrom(response);
});
return postToTokenUrl(data).then(function (response) {
if (response.status !== 200) {
throw new Error('Failed to retrieve access token');
}
return tokenInfoFrom(response);
});
}
function tokenGetter() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment