UNPKG

@trimble-oss/trimble-id

Version:

Trimble Identity SDK for JavaScript/ TypeScript

75 lines (70 loc) 3.1 kB
'use strict'; (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.OpenIdKeysetProvider = factory(root); } }(this, function (imports) { /** * @implements {IKeysetProvider} * @description OpenIdKeysetProvider provides a keyset for JSON web token */ class OpenIdKeysetProvider { /** * @description Public constructor for OpenIdKeySetProvider class * @param {IEndpointProvider} endpointProvider An endpoint provider that provides the URL for the Trimble Identity JSON web keyset endpoint. * It can be be OpenIdEndpointProvider/FixedEndpointProvider */ constructor(endpointProvider) { this._endpointProvider = endpointProvider; //Send analytics this._analyticshttpclient = imports.AnalyticsHttpClient; this._analyticshttpclient.sendInitEvent(this.constructor.name); } /** * @description Retrieves an dictionary of named keys * @returns {PromiseLike<any>} A Task that resolves to a dictionary of named keys on completion * @exception Thrown when a JSON web keyset endpoint is not provided by the endpoint provider * @exception Thrown when a call to the JSON web keyset endpoint fails */ RetrieveKeyset() { //Send analytics this._analyticshttpclient.sendMethodEvent(this.RetrieveKeyset.name); var _this = this; return new Promise((resolve, reject) => { _this._endpointProvider.RetrieveJSONWebKeySetEndpoint() .then((endpoint) => { new imports.HttpClient().httpGetJSON(endpoint) .then((result) => { resolve(result.keys); }) .catch((err) => { _this._analyticshttpclient.sendExceptionEvent(_this.RetrieveKeyset.name, err); reject('Failed to retrieve JSON web key set: ' + err); }); }) .catch((err) => { _this._analyticshttpclient.sendExceptionEvent(_this.RetrieveKeyset.name, err); reject('Failed to retrieve JSON web key set endpoint: ' + err); }); }); } } // Exposed public methods return OpenIdKeysetProvider; }));