Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * EntitlementEngine
  5. *
  6. * Simplified EntitlementEngine for authentication with the Ericsson Exposure API
  7. *
  8. * @param {Object} config Configuration object
  9. *
  10. * @constructor
  11. */
  12. var EntitlementEngine = function(config) {
  13. if (typeof config === 'undefined') {
  14. throw new TypeError("EntitlementEngineError: Config can not be undefined");
  15. }
  16.  
  17. if (!config.isValid()) {
  18. throw new TypeError("EntitlementEngineError: Configuration invalid");
  19. }
  20.  
  21. this.config = config;
  22. return this;
  23. }
  24.  
  25. /**
  26. * Authentication Token
  27. */
  28. EntitlementEngine.prototype.authenticationToken = null;
  29.  
  30. /**
  31. * Performs an anonymous authentication with the configured EricssonExposure API
  32. * @param {String} username Username used for authentication
  33. * @param {String} password Password used for authentication
  34. * @param {Function=} callback Function to be called when authentication completes
  35. */
  36. EntitlementEngine.prototype.authenticate = function(username, password, callback) {
  37. if (!username) {
  38. throw new TypeError("EntitlementEngineError: Username must not be empty");
  39. }
  40.  
  41. if (!password) {
  42. throw new TypeError("EntitlementEngineError: Password must not be empty");
  43. }
  44.  
  45.  
  46. var request = new XMLHttpRequest();
  47.  
  48. var requestURL = this.config.serverUrl + '/v' + this.config.apiVersion + '/customer/' + this.config.customer + '/businessunit/' + this.config.businessUnit + '/auth/login';
  49. var requestData = {
  50. deviceId: this.config.device + "_" + username,
  51. device: {
  52. type: 'WEB'
  53. },
  54. username: username,
  55. password: password
  56. };
  57.  
  58. var requestObject = JSON.stringify(requestData);
  59. request.onreadystatechange = function() {
  60. if (request.readyState == 4) {
  61. if (request.status < 400) {
  62. if (typeof callback !== undefined) {
  63. callback({
  64. state: "success",
  65. data: JSON.parse(request.responseText)
  66. });
  67. }
  68. } else {
  69. if (typeof callback !== undefined) {
  70. callback({
  71. state: "error",
  72. data: JSON.parse(request.responseText)
  73. });
  74. }
  75. }
  76. }
  77. }
  78.  
  79. request.open('POST', requestURL);
  80. request.setRequestHeader('Content-Type', 'application/json');
  81. request.send(requestObject);
  82. };
  83.  
  84. /**
  85. * EntitlementEngineConfiguration
  86. *
  87. * Simple data object that store entilementconfiguration data
  88. */
  89. var EntitlementEngineConfiguration = function() {
  90. }
  91.  
  92. EntitlementEngineConfiguration.prototype.serverUrl = null;
  93. EntitlementEngineConfiguration.prototype.apiVersion = null;
  94. EntitlementEngineConfiguration.prototype.customer = null;
  95. EntitlementEngineConfiguration.prototype.businessUnit = null;
  96. EntitlementEngineConfiguration.prototype.device = "WEB";
  97.  
  98. /**
  99. * Set the Exposure API Url
  100. * @throws TypeError
  101. * @param serverUrl
  102. */
  103. EntitlementEngineConfiguration.prototype.setServerUrl = function(serverUrl) {
  104. if (!serverUrl) {
  105. throw new TypeError("Entitlement Configuration Error: Server URL can not be null");
  106. }
  107. if (!new RegExp(/^https?:\/\//).test(serverUrl)) {
  108. throw new TypeError("Entitlement Configuration Error: Server URL must specify either http or https");
  109. }
  110. this.serverUrl = serverUrl;
  111. };
  112.  
  113. /**
  114. * Set the exposure API version
  115. * @throws TypeError
  116. * @param apiVersion
  117. */
  118. EntitlementEngineConfiguration.prototype.setApiVersion = function(apiVersion) {
  119. if (!apiVersion) {
  120. throw new TypeError("Entitlement Configuration Error: API Version can not be null");
  121. }
  122. this.apiVersion = apiVersion;
  123. };
  124.  
  125. /**
  126. * Set the Customer Unit
  127. * @throws TypeError
  128. * @param customer
  129. */
  130. EntitlementEngineConfiguration.prototype.setCustomer = function(customer) {
  131. if (!customer) {
  132. throw new TypeError("Entitlement Configuration Error: customer can not be empty");
  133. }
  134. this.customer = customer;
  135. };
  136.  
  137. /**
  138. * Set the BusinessUnit
  139. * @throws TypeError
  140. * @param businessUnit
  141. */
  142. EntitlementEngineConfiguration.prototype.setBusinessUnit = function(businessUnit) {
  143. if (!businessUnit) {
  144. throw new TypeError("Entitlement Configuration Error: BusinessUnit can not be empty");
  145. }
  146. this.businessUnit = businessUnit;
  147. };
  148.  
  149. /**
  150. * Set the device type
  151. * @throws TypeError
  152. * @param device
  153. */
  154. EntitlementEngineConfiguration.prototype.setDevice = function(device) {
  155. if (!device) {
  156. throw new TypeError("Entitlement Configuration Error: Device can not be empty");
  157. }
  158. this.device = device;
  159. };
  160.  
  161. /**
  162. * Simple validation method ensuring that all the required data is set.
  163. * @returns {boolean}
  164. */
  165. EntitlementEngineConfiguration.prototype.isValid = function() {
  166. return this.serverUrl != null && this.apiVersion != null && this.customer != null && this.businessUnit != null && this.device != null;
  167. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement