@trimble-oss/trimble-id
Version:
Trimble Identity SDK for JavaScript/ TypeScript
213 lines (195 loc) • 9.77 kB
JavaScript
;
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// AMD
define(['./HttpClient', './AnalyticsHttpClient'], (HttpClient, AnalyticsHttpClient) => {
return factory({
HttpClient: HttpClient,
AnalyticsHttpClient: AnalyticsHttpClient,
});
});
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory({
HttpClient: require('./HttpClient'),
AnalyticsHttpClient: require('./AnalyticsHttpClient'),
});
} else {
// Browser globals (Note: root is window)
root.OpenIdEndpointProvider = factory(root);
}
}(this, function (imports) {
/**
* @implements {IEndpointProvider}
* @description OpenIdEndpointProvider helps to discover various endpoints like authorization_endpoint, token_endpoint, userinfo_endpoint etc.
*/
class OpenIdEndpointProvider {
/**
* @description Public constructor for OpenIdEndpointProvider class
* @param {string} configurationEndpoint The URL for the Trimble Identity OpenID well known configuration endpoint
* Production : https://id.trimble.com/.well-known/openid-configuration
* @param {string=} tokenRevocationEndpoint The URL for the Trimble Identity token revocation endpoint, if not supplied this is computed relative to the token endpoint
*/
constructor(configurationEndpoint, tokenRevocationEndpoint = null) {
this._configurationEndpoint = configurationEndpoint;
this._authorizationEndpoint = null;
this._tokenEndpoint = null;
this._userInfoEndpoint = null;
this._jwksEndpoint = null;
this._tokenRevocationEndpoint = tokenRevocationEndpoint;
//Send analytics
this._analyticshttpclient = imports.AnalyticsHttpClient;
this._analyticshttpclient.sendInitEvent(this.constructor.name);
}
/**
* @description Retrieves a URL for the Trimble Identity authorization endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveAuthorizationEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveAuthorizationEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._authorizationEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._authorizationEndpoint); })
.catch((err) => {
self._analyticshttpclient.sendExceptionEvent(self.RetrieveAuthorizationEndpoint.name, err);
reject(err);
});
else
resolve(self._authorizationEndpoint);
});
}
/**
* @description Retrieves a URL for the Trimble Identity authorization endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveTokenEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveTokenEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._tokenEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._tokenEndpoint); })
.catch((err) => {
self._analyticshttpclient.sendExceptionEvent(self.RetrieveTokenEndpoint.name, err);
reject(err);
});
else
resolve(self._tokenEndpoint);
});
}
/**
* @description Retrieves a URL for the Trimble Identity authorization endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveUserInfoEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveUserInfoEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._userInfoEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._userInfoEndpoint); })
.catch((err) => {
self._analyticshttpclient.sendExceptionEvent(self.RetrieveUserInfoEndpoint.name, err);
reject(err);
});
else
resolve(self._userInfoEndpoint);
});
}
/**
* @description Retrieves a URL for the Trimble Identity token revocation endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveTokenRevocationEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveTokenRevocationEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._tokenRevocationEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._tokenRevocationEndpoint); })
.catch((err) => {
self._analyticshttpclient.sendExceptionEvent(self.RetrieveTokenRevocationEndpoint.name, err);
reject(err);
});
else
resolve(self._tokenRevocationEndpoint);
});
}
/**
* @description Retrieves a URL for the Trimble Identity end session endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveEndSessionEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveEndSessionEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._endSessionEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._endSessionEndpoint); })
.catch((err) => { reject(err); });
else
resolve(self._endSessionEndpoint);
});
}
/**
* @description Retrieves a URL for the Trimble Identity authorization endpoint
* @returns {PromiseLike<string>} A Task that resolves to the value of the URL on completion
* @exception Thrown if the configuration endpoint returns an error
*/
RetrieveJSONWebKeySetEndpoint() {
//Send analytics
this._analyticshttpclient.sendMethodEvent(this.RetrieveJSONWebKeySetEndpoint.name);
var self = this;
return new Promise(function (resolve, reject) {
if (!self._jwksEndpoint)
self._loadConfiguration()
.then(() => { resolve(self._jwksEndpoint); })
.catch((err) => {
self._analyticshttpclient.sendExceptionEvent(self.RetrieveJSONWebKeySetEndpoint.name, err);
reject(err);
});
else
resolve(self._jwksEndpoint);
});
}
_loadConfiguration() {
var self = this;
return new Promise(function (resolve, reject) {
new imports.HttpClient().httpGetJSON(self._configurationEndpoint)
.then((result) => {
self._authorizationEndpoint = result.authorization_endpoint;
self._tokenEndpoint = result.token_endpoint;
self._userInfoEndpoint = result.userinfo_endpoint;
self._jwksEndpoint = result.jwks_uri;
// the following two endpoints were not provided in the Trimble Identity v3 well known configuration
// if they are not available we compute them relative to the token endpoint
if ('revocation_endpoint' in result)
self._tokenRevocationEndpoint = result.revocation_endpoint;
else
self._tokenRevocationEndpoint = self._tokenRevocationEndpoint || new URL(self._configurationEndpoint).origin + "/revoke";
if ('end_session_endpoint' in result)
self._endSessionEndpoint = result.end_session_endpoint;
else
self._endSessionEndpoint = new URL(self._configurationEndpoint).origin + "/i/commonauth";
resolve();
})
.catch((err) => { reject('Failed to load configuration endpoint: ' + err); });
});
}
}
// Exposed public methods
return OpenIdEndpointProvider;
}));