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,18 +40,24 @@ function auth($http, settings) { ...@@ -40,18 +40,24 @@ 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. // Exchange the JWT grant token for an access token.
// See https://tools.ietf.org/html/rfc7523#section-4 // See https://tools.ietf.org/html/rfc7523#section-4
function exchangeToken(grantToken) { function exchangeToken(grantToken) {
var data = queryString.stringify({ var data = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: grantToken, assertion: grantToken,
});
var requestConfig = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}; };
return $http.post(tokenUrl, data, requestConfig) return postToTokenUrl(data).then(function (response) {
.then(function (response) {
if (response.status !== 200) { if (response.status !== 200) {
throw new Error('Failed to retrieve access token'); throw new Error('Failed to retrieve access token');
} }
......
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