Guest User

Untitled

a guest
Oct 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. /**********************************************************************************
  2. * WP.idcard
  3. **********************************************************************************/
  4. WP.namespace('WP.idcard');
  5. WP.idcard = (function (){
  6.  
  7. //dependencies
  8. var constant = WP.constants;
  9.  
  10. //one-time initialization procedures
  11. function getVirtualCardInformation(){
  12.  
  13. WP.busy.show();
  14. var invocationData = {
  15. adapter : 'VirtualIDCard',
  16. procedure : 'getVirtualIDCard',
  17. parameters : []
  18. };
  19.  
  20. WL.Client.invokeProcedure(invocationData, {
  21. onSuccess : getIDCardSuccess,
  22. onFailure : getIDCardFailure
  23. });
  24. }
  25.  
  26. function getIDCardSuccess(response){
  27. WP.busy.hide();
  28. WL.Logger.debug("[CARD] getIDCardSuccess");
  29.  
  30. var card_information = response.invocationResult.item;
  31. WL.Logger.debug(JSON.stringify(card_information));
  32.  
  33. populateCardPage(card_information);
  34.  
  35. WL.EncryptedCache.write(constant.WP_CARD_INFO, JSON.stringify(card_information), onWriteSuccess, onWriteFailure);
  36. function onWriteSuccess(status){
  37. WL.Logger.debug("Succesfully encrypted card information into cache. Card Info:" + JSON.stringify(card_information));
  38. }
  39. function onWriteFailure(status){
  40. if (status == WL.EncryptedCache.ERROR_EOC_CLOSED){
  41. somethingWentWrong();
  42. }
  43. }
  44. }
  45.  
  46. function getIDCardFailure(){
  47. WP.busy.hide();
  48. WL.Logger.debug("[ID CARD] Unable to populate ID card.");
  49.  
  50. }
  51.  
  52. function populateIdCardForOffline(){
  53. WL.Logger.debug("[CARD] getIDCarSuccess");
  54. WL.EncryptedCache.read(constant.WP_CARD_INFO, onDecryptReadSuccess, onDecryptReadFailure);
  55. function onDecryptReadSuccess(value){
  56. WL.Logger.debug("Read success. Retrieved value = " + value);
  57. var card_information = JSON.parse(value);
  58. populateCardPage(card_information);
  59.  
  60. $("#login-page").hide();
  61. $("#id-page").show();
  62. changePageGotoMyIdCard();
  63. }
  64. function onDecryptReadFailure(status){
  65. somethingWentWrong();
  66. }
  67. }
  68.  
  69. function populateCardPage(card_information){
  70. WL.Logger.debug("Populate card page" + card_information.name);
  71.  
  72. $('#name').html("Name: "+card_information.name);
  73. $('#group').html("Group: "+card_information.group);
  74. $('#planCode').html("Plan Code: "+card_information.planCode);
  75. $('#rxBin').html("rxBin: "+card_information.rxBin);
  76. $('#coverages').html("Coverage: "+card_information.coverages);
  77. $('#insId a').attr('href', "tel:"+card_information.insId).html("Phone: "+card_information.insId);
  78. }
  79.  
  80. function clearEOC(){
  81. localStorage.clear(); //TODO: Replace with EOC.destroy()
  82. }
  83.  
  84. function somethingWentWrong(){
  85. WP.msg.alert("Oops, something went wrong! Try again.");
  86. WL.Logger.debug("Encrypted cache closed, write failed. error code= "+ status);
  87. clearEOC();
  88. changePageGotoIndex();
  89. }
  90.  
  91. //public API
  92. return {
  93. populateCardOffline : function(){
  94. populateIdCardForOffline();
  95. },
  96. getCardInfo : function(){
  97. getVirtualCardInformation();
  98. }
  99. };
  100.  
  101. }());//end WP.idcard
  102.  
  103. /**********************************************************************************
  104. * WP.eoc
  105. **********************************************************************************/
  106.  
  107. // EOC
  108. WP.namespace('WP.eoc');
  109. WP.eoc = (function(){
  110. //dependencies
  111. var constant = WP.constants,
  112. idcard = WP.idcard,
  113. passcodeAttempts = 3,
  114. correctPasscodeHasBeenEntered = false;
  115.  
  116. $('#submitPasscodeButton').live('click', function(){
  117. var passcode = $('#passcode').val();
  118. var passcode_confirmation = $('#confirm_passcode').val();
  119.  
  120. if( passcode.length > 0 && (passcode === passcode_confirmation) ){
  121. WL.Logger.debug("[EOC] The passcode is valid. Creating EOC. Passcode: " + passcode);
  122. correctPasscodeHasBeenEntered = true;
  123.  
  124. // TODO handle failure
  125. WP.busy.show();
  126. createEOC(passcode, showLoginPage, function(){});
  127. }else{
  128. WL.Logger.debug("[EOC] The passcodes entered are invalid Passcode1: " + passcode + " Passcode2: " + passcode_confirmation);
  129. WP.msg.alert("Your passcodes did not match. Please try again.");
  130. // TODO remove/change this
  131. clearEOC();
  132.  
  133. $('#passcode').val('');
  134. $('#confirm_passcode').val('');
  135. }
  136. });
  137.  
  138. $('#validatePasscodeButton').live('click', function(){
  139. WL.Logger.debug("[EOC] Validating user entered passcode");
  140. var passcode = $('#enter_passcode').val();
  141.  
  142. WP.busy.show();
  143. // Try to open the EOC with
  144. WL.EncryptedCache.open(passcode, true, onOpenComplete, onOpenError);
  145. function onOpenComplete(status){
  146. WP.busy.hide();
  147. WL.Logger.debug("[EOC] Passcode entered correct. Getting credentials from EOC.");
  148. correctPasscodeHasBeenEntered = true;
  149.  
  150. // Open success, get credentials
  151. WL.EncryptedCache.read(constant.KEY_CREDENTIALS, onDecryptReadSuccess, onDecryptReadFailure);
  152. function onDecryptReadSuccess(value){
  153. WL.Logger.debug("Read success. Retrieved value = " + value);
  154. var credentials = JSON.parse(value);
  155. $("#username").val(credentials.j_username);
  156. $("#password").val(credentials.j_password);
  157.  
  158. if(isOnline){
  159. showLoginPage();
  160. }else{
  161. idcard.populateCardOffline();
  162. }
  163. }
  164. function onDecryptReadFailure(status){
  165. WL.Logger.debug("Encrypted cache closed, reading failed");
  166. }
  167. }
  168. function onOpenError(status){
  169. WP.busy.hide();
  170. WL.Logger.debug("[EOC] Open cache with passcode failed. Clearing the EOC");
  171.  
  172. failedPasscodeAttempt();
  173. }
  174.  
  175. });
  176.  
  177. function showLoginPage(){
  178. WL.Logger.debug("[Login] Showing login page");
  179. WP.busy.hide();
  180.  
  181. WL.Client.login("WellPointAppRealm", {
  182. onSuccess: function(){
  183. WL.Logger.debug("[Authentication] Login was successful.");
  184. idcard.getCardInfo();
  185. changePageGotoMyIdCard();
  186. },
  187. onFailure: function(){
  188. WL.Logger.debug("[Authentication] Login failed.");
  189. }
  190. });
  191. };
  192.  
  193. function showIDCard(){
  194. //TODO: Add support for devices that don't support localstorage
  195. var eocExists = isEocCreated();
  196.  
  197. if(WL.Client.isUserAuthenticated("WellPointAppRealm") || correctPasscodeHasBeenEntered){
  198. changePageGotoMyIdCard();
  199. }
  200. else if( eocExists ){
  201. WL.Logger.debug("[EOC] EOC exists, prompting for passcode to use EOC");
  202. changePageGotoPasswordPrompt();
  203. }else if( !isOnline ){ //=== no EOC, offline
  204. WL.Logger.debug("[EOC] EOC offline and user online. Cannot access anything.");
  205. WP.msg.alert("You must be online the first time you access this feature.");
  206.  
  207. }else{ // === online, no EOC
  208. WL.Logger.debug("[EOC] The user is online but the EOC has not been created. Prompting for passcode.");
  209. changePageGotoCreatePassword();
  210. }
  211. }
  212.  
  213. function createEOC(passcode, onSuccess, onFailure){
  214. WL.EncryptedCache.open(passcode, true, onSuccess, onFailure);
  215. function onOpenComplete(status){
  216. onSuccess();
  217. }
  218. function onOpenError(status){
  219. switch(status){
  220. case WL.EncryptedCache.ERROR_KEY_CREATION_IN_PROGRESS:
  221. WL.Logger.debug("ERROR: KEY CREATION IN PROGRESS");
  222. break;
  223. case WL.EncryptedCache.ERROR_LOCAL_STORAGE_NOT_SUPPORTED:
  224. WL.Logger.debug("ERROR: LOCAL STORAGE NOT SUPPORTED");
  225. break;
  226. case WL.EncryptedCache.ERROR_NO_EOC:
  227. WL.Logger.debug("ERROR: NO EOC");
  228. break;
  229. case WL.EncryptedCache.ERROR_COULD_NOT_GENERATE_KEY:
  230. WL.Logger.debug("ERROR: COULD NOT GENERATE KEY");
  231. break;
  232. case WL.EncryptedCache.ERROR_CREDENTIALS_MISMATCH:
  233. WL.Logger.debug("ERROR: CREDENTIALS MISMATCH");
  234. break;
  235. }
  236. }
  237. }
  238.  
  239. function saveCredentialsToEOC(params){
  240. WL.EncryptedCache.write(constant.KEY_CREDENTIALS, JSON.stringify(params), onWriteSuccess, onWriteFailure);
  241. function onWriteSuccess(status){
  242. WL.Logger.debug("Succesfully encrypted credentials into cache. Credentials:" + JSON.stringify(params));
  243. }
  244. function onWriteFailure(status){
  245. if (status == WL.EncryptedCache.ERROR_EOC_CLOSED){
  246. WL.Logger.debug("Encrypted cache closed, write failed. error code= "+ status);
  247. clearEOC();
  248. changePageGotoIndex();
  249. }
  250. }
  251. }
  252.  
  253. function clearEOC(){
  254. localStorage.clear(); //TODO: Replace with EOC.destroy()
  255. }
  256.  
  257. function failedPasscodeAttempt(){
  258. passcodeAttempts--;
  259. if(passcodeAttempts === 0){
  260. WP.msg.alert("You have entered the passcode incorrectly 3 times. The cache will now be destroyed");
  261. clearEOC();
  262. changePageGotoIndex();
  263. }else{
  264. WP.msg.alert("Incorrect passcode. You have " + passcodeAttempts + " more attempt(s) before the cache will be destroyed.");
  265. }
  266. }
  267.  
  268. function isEocCreated(){
  269. var STORAGE_PREFIX = "__$WLEOC__";
  270. var SALT_KEY = "__$WLEOC_SALT";
  271. var CIPHER_KEY = "__$WLEOC_CIPHER";
  272. var i = localStorage.length;
  273.  
  274. while( i-- ) {
  275. var key = localStorage.key(i);
  276. if (key.indexOf(STORAGE_PREFIX) === 0 ||
  277. key.indexOf(SALT_KEY) === 0 ||
  278. key.indexOf(CIPHER_KEY) === 0) {
  279. //TODO: Try checkin just whatever I end up using to store the user/pass obj
  280. return true;
  281. }
  282. }//end loop
  283. return false;
  284. }
  285.  
  286. //public API
  287. return {
  288.  
  289. show : function(){
  290. showIDCard();
  291. },
  292.  
  293. saveCredentials : function(params){
  294. saveCredentialsToEOC(params);
  295. }
  296.  
  297. };
  298.  
  299. }());//WP.eoc
  300.  
  301.  
  302.  
  303.  
  304. //////////////////////////////////////// AUTH.JS //////////////////////////////////////////
  305.  
  306. function onFormSubmit() {
  307. var reqURL = './' + LOGIN_PAGE_SECURITY_INDICATOR;
  308.  
  309. var params = {
  310. j_username : $(USERNAME_INPUT_ID).val(),
  311. j_password : $(PASSWORD_INPUT_ID).val()
  312. };
  313.  
  314. WL.Logger.debug("[Authenication] Auth saving credentials to EOC. Credentials: " + JSON.stringify(params));
  315. WP.eoc.saveCredentials(params);
  316. onSubmitCallback(reqURL, {parameters:params});
  317. }
Add Comment
Please, Sign In to add comment