Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- /**
- * EntitlementEngine
- *
- * Simplified EntitlementEngine for authentication with the Ericsson Exposure API
- *
- * @param {Object} config Configuration object
- *
- * @constructor
- */
- var EntitlementEngine = function(config) {
- if (typeof config === 'undefined') {
- throw new TypeError("EntitlementEngineError: Config can not be undefined");
- }
- if (!config.isValid()) {
- throw new TypeError("EntitlementEngineError: Configuration invalid");
- }
- this.config = config;
- return this;
- }
- /**
- * Authentication Token
- */
- EntitlementEngine.prototype.authenticationToken = null;
- /**
- * Performs an anonymous authentication with the configured EricssonExposure API
- * @param {String} username Username used for authentication
- * @param {String} password Password used for authentication
- * @param {Function=} callback Function to be called when authentication completes
- */
- EntitlementEngine.prototype.authenticate = function(username, password, callback) {
- if (!username) {
- throw new TypeError("EntitlementEngineError: Username must not be empty");
- }
- if (!password) {
- throw new TypeError("EntitlementEngineError: Password must not be empty");
- }
- var request = new XMLHttpRequest();
- var requestURL = this.config.serverUrl + '/v' + this.config.apiVersion + '/customer/' + this.config.customer + '/businessunit/' + this.config.businessUnit + '/auth/login';
- var requestData = {
- deviceId: this.config.device + "_" + username,
- device: {
- type: 'WEB'
- },
- username: username,
- password: password
- };
- var requestObject = JSON.stringify(requestData);
- request.onreadystatechange = function() {
- if (request.readyState == 4) {
- if (request.status < 400) {
- if (typeof callback !== undefined) {
- callback({
- state: "success",
- data: JSON.parse(request.responseText)
- });
- }
- } else {
- if (typeof callback !== undefined) {
- callback({
- state: "error",
- data: JSON.parse(request.responseText)
- });
- }
- }
- }
- }
- request.open('POST', requestURL);
- request.setRequestHeader('Content-Type', 'application/json');
- request.send(requestObject);
- };
- /**
- * EntitlementEngineConfiguration
- *
- * Simple data object that store entilementconfiguration data
- */
- var EntitlementEngineConfiguration = function() {
- }
- EntitlementEngineConfiguration.prototype.serverUrl = null;
- EntitlementEngineConfiguration.prototype.apiVersion = null;
- EntitlementEngineConfiguration.prototype.customer = null;
- EntitlementEngineConfiguration.prototype.businessUnit = null;
- EntitlementEngineConfiguration.prototype.device = "WEB";
- /**
- * Set the Exposure API Url
- * @throws TypeError
- * @param serverUrl
- */
- EntitlementEngineConfiguration.prototype.setServerUrl = function(serverUrl) {
- if (!serverUrl) {
- throw new TypeError("Entitlement Configuration Error: Server URL can not be null");
- }
- if (!new RegExp(/^https?:\/\//).test(serverUrl)) {
- throw new TypeError("Entitlement Configuration Error: Server URL must specify either http or https");
- }
- this.serverUrl = serverUrl;
- };
- /**
- * Set the exposure API version
- * @throws TypeError
- * @param apiVersion
- */
- EntitlementEngineConfiguration.prototype.setApiVersion = function(apiVersion) {
- if (!apiVersion) {
- throw new TypeError("Entitlement Configuration Error: API Version can not be null");
- }
- this.apiVersion = apiVersion;
- };
- /**
- * Set the Customer Unit
- * @throws TypeError
- * @param customer
- */
- EntitlementEngineConfiguration.prototype.setCustomer = function(customer) {
- if (!customer) {
- throw new TypeError("Entitlement Configuration Error: customer can not be empty");
- }
- this.customer = customer;
- };
- /**
- * Set the BusinessUnit
- * @throws TypeError
- * @param businessUnit
- */
- EntitlementEngineConfiguration.prototype.setBusinessUnit = function(businessUnit) {
- if (!businessUnit) {
- throw new TypeError("Entitlement Configuration Error: BusinessUnit can not be empty");
- }
- this.businessUnit = businessUnit;
- };
- /**
- * Set the device type
- * @throws TypeError
- * @param device
- */
- EntitlementEngineConfiguration.prototype.setDevice = function(device) {
- if (!device) {
- throw new TypeError("Entitlement Configuration Error: Device can not be empty");
- }
- this.device = device;
- };
- /**
- * Simple validation method ensuring that all the required data is set.
- * @returns {boolean}
- */
- EntitlementEngineConfiguration.prototype.isValid = function() {
- return this.serverUrl != null && this.apiVersion != null && this.customer != null && this.businessUnit != null && this.device != null;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement