@zeroid/trimble.identity.authorizationcodegranttokenprovider
Version:
An implementation of ITokenProvider for OAuth authorization code grant type
91 lines (82 loc) • 4.11 kB
JavaScript
;
// implements ITokenProvider
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['@zeroid/trimble.identity.refreshabletokenprovider', '@zeroid/trimble.httpclient'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('@zeroid/trimble.identity.refreshabletokenprovider'), require('@zeroid/trimble.httpclient'));
} else {
// Browser globals (Note: root is window)
root.authorizationCodeGrantTokenProvider = factory(root.RefreshableTokenProvider, root.HttpClient);
}
}(this, function (RefreshableTokenProvider, HttpClient) {
var _authorizationCodeGrantTokenProvider = function (endpointProvider, consumerKey, consumerSecret, redirectUrl) {
this._endpointProvider = endpointProvider;
this._consumerKey = consumerKey;
this._consumerSecret = consumerSecret;
this._redirectUrl = redirectUrl;
this._refreshableTokenProvider;
this._state = null;
}
_authorizationCodeGrantTokenProvider.prototype.getOAuthRedirect = function (state) {
var self = this;
return new Promise((resolve, reject) => {
self._endpointProvider.retrieveAuthorizationEndpoint()
.then((endpoint) => {
resolve(endpoint +
'?response_type=code&scope=openid' +
'&client_id=' + encodeURIComponent(self._consumerKey) +
'&redirect_uri=' + encodeURIComponent(self._redirectUrl) +
'&state=' + encodeURIComponent(state));
})
.catch(() => { reject(); });
});
}
_authorizationCodeGrantTokenProvider.prototype.validateQuery = function (hash) {
if (hash.startsWith('#')) hash = hash.substr(1);
var query = {};
hash.split('&').forEach((parameter) => {
var parts = parameter.split('=');
query[parts[0]] = decodeURIComponent(parts[1]);
});
this._state = query.state;
return this._validateCode(query.code);
};
_authorizationCodeGrantTokenProvider.prototype._validateCode = function (code) {
var self = this;
return new Promise(function (resolve, reject) {
self._endpointProvider.retrieveAuthorizationEndpoint()
.then((endpoint) => {
var basicHeader = 'Basic ' + btoa(self._consumerKey + ':' + self._consumerSecret);
var requestSettings = {
headers: {
authorization: basicHeader,
"content-type": 'application/x-www-form-urlencoded',
accept: 'application/json'
}
};
var content =
'grant_type=authorization_code' +
'&tenantDomain=trimble.com' +
'&code=' + encodeURIComponent(code) +
'&redirect_uri=' + encodeURIComponent(self._redirectUrl);
new HttpClient().httpPost(endpoint, content, requestSettings)
.then((json) => {
var result = JSON.parse(json);
var now = new Date();
_refreshableTokenProvider = new RefreshableTokenProvider(self._endpointProvider, self._consumerKey, self._consumerSecret, result.access_token, new Date(now.getTime() + result.expires_in*1000), result.refresh_token);
resolve();
})
.catch(() => { reject(); });
})
.catch(() => { reject(); });
});
};
_authorizationCodeGrantTokenProvider.prototype.retrieveToken = function () {
return this._refreshableTokenProvider.retrieveToken();
};
// Exposed public methods
return _authorizationCodeGrantTokenProvider;
}));