CompleteAuthentication.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Glass
  5. * (C) Copyright IBM Corp. 2017, 2022
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. require(['baglass/app/AppController', 'caglass/ajax/CAAuthentication', 'baglass/nls/StringResources', 'baglass/core-client/js/core-client/utils/UrlUtils', 'caglass/utils/CAUtils', 'baglass/core-client/js/core-client/utils/BrowserUtils', 'caglass/ajax/PrepareRequest', 'caglass/ajax/HandleError', 'caglass/ajax/HandleResponse', 'underscore', 'jquery', 'jquery-ui', 'jquery-bbq', 'promise'], function (AppController, CAAuthentication, StringResources, urlUtils, CAUtils, BrowserUtils, PrepareRequest, HandleError, HandleResponse, _, $) {
  9. var appController = new AppController({
  10. '$rootPane': $('#main')
  11. });
  12. window.__glassAppController = appController;
  13. var glassContext = appController.glassContext;
  14. var ajaxSrv = appController.glassContext.getCoreSvc('.Ajax');
  15. ajaxSrv.addRequestHandler(new PrepareRequest(glassContext));
  16. ajaxSrv.setErrorHandler(new HandleError(glassContext));
  17. ajaxSrv.addResponseHandler(new HandleResponse(glassContext));
  18. var caAuthentication = new CAAuthentication();
  19. var state = {};
  20. try {
  21. state = JSON.parse(urlUtils.base64Decode(_.unescape($('#authState').text())));
  22. } catch (error) {
  23. appController.glassContext.getCoreSvc('.Logger').error('Cannot parse state', error);
  24. }
  25. if (state.skipCALogin === true) {
  26. window.opener.postMessage({
  27. data: {}
  28. }, window.location.origin);
  29. } else {
  30. state.code = _.unescape($('#authCode').text());
  31. caAuthentication.submitLoginTokens(appController.glassContext, state).then(function (result) {
  32. return doPostLoginRedirect(result, state, appController);
  33. })["catch"](function (error) {
  34. appController.glassContext.getCoreSvc('.Logger').error('Cannot login, reached dead end ', error);
  35. var resolvedErrorString = StringResources.get('unableLogin');
  36. var loginErrorDialogCallback = function loginErrorDialogCallback() {
  37. appController.glassContext.getCoreSvc('.Logger').error('dialog closing callback during login dead end');
  38. return doPostLoginRedirect(error, state, appController);
  39. };
  40. if (error.jqXHR && error.jqXHR.responseJSON && error.jqXHR.responseJSON.errorCodeString === 'camAuthUnrecoverable') {
  41. resolvedErrorString = '';
  42. if (error.jqXHR.responseJSON.promptInfo && error.jqXHR.responseJSON.promptInfo.captions) {
  43. var promptInfoCaptions = error.jqXHR.responseJSON.promptInfo.captions;
  44. for (var i = 0; i < promptInfoCaptions.length; ++i) {
  45. resolvedErrorString += promptInfoCaptions[i];
  46. }
  47. }
  48. showErrorMessage(resolvedErrorString, StringResources.get('CALoginAuthError'), loginErrorDialogCallback, true);
  49. } else {
  50. showErrorMessage(resolvedErrorString, StringResources.get('CALoginAuthError'), loginErrorDialogCallback);
  51. }
  52. });
  53. }
  54. var showErrorMessage = function showErrorMessage(error, title, callback, htmlContent) {
  55. var mobileService = glassContext.getSvcSync('.MobileCompleteAuthentication');
  56. if (state.isMobile && mobileService) {
  57. mobileService.onError(error);
  58. return;
  59. }
  60. appController.showErrorMessage(error, title, callback, htmlContent);
  61. };
  62. var isUpdateDCO = function isUpdateDCO() {
  63. return appController.glassContext.getCoreSvc('.Config').getConfigValue('Glass.digitalContextEnabled');
  64. };
  65. var postMessageToOriginalBrowserTab = function postMessageToOriginalBrowserTab(result, appController) {
  66. if (BrowserUtils.isIE()) {
  67. try {
  68. // IE11 postMessage doesn't work if the domains are different.
  69. // To work around this, we return the data via localStorage
  70. // which triggers a StorageEvent that the parent window can listen to.
  71. window.localStorage['externalLoginResult'] = JSON.stringify(result.data);
  72. setTimeout(function () {
  73. window.close();
  74. }, 1000);
  75. } catch (err) {
  76. appController.glassContext.getCoreSvc('.Logger').error(err);
  77. if (window.opener) {
  78. // One last try if localStorage is disabled...
  79. window.opener.postMessage(result.data, window.location.origin);
  80. }
  81. }
  82. } else {
  83. window.opener.postMessage(result.data, window.location.origin);
  84. }
  85. };
  86. var redirectUsingSameBrowserTab = function redirectUsingSameBrowserTab(state) {
  87. var mobileService = glassContext.getSvcSync('.MobileCompleteAuthentication');
  88. state.redirectUrl = CAUtils.removeCAMNamespaceHref(state.redirectUrl);
  89. state.redirectUrl = CAUtils.addUpdateDcoToQuerystring(state.redirectUrl, state.updatedco);
  90. if (state.isMobile && mobileService) {
  91. mobileService.onSuccess();
  92. return;
  93. }
  94. var targetWindow = glassContext.window || window;
  95. targetWindow.location = state.redirectUrl;
  96. }; // result can be a 200 response object or an error object. We pass either on to the original browser tab
  97. var doPostLoginRedirect = function doPostLoginRedirect(result, state, appController) {
  98. return isUpdateDCO().then(function (isDcoConfig) {
  99. if (_typeof(result) == Error) {
  100. result.data = false;
  101. }
  102. if (state.newWindow === true) {
  103. result.data = _.extend({
  104. updatedco: isDcoConfig
  105. });
  106. return postMessageToOriginalBrowserTab(result, appController);
  107. } else if (state.redirectUrl) {
  108. // Use same window
  109. state.updatedco = isDcoConfig;
  110. return redirectUsingSameBrowserTab(state);
  111. } else {
  112. appController.glassContext.getCoreSvc('.Logger').error('redirect fallback');
  113. window.location = window.location.origin;
  114. }
  115. });
  116. };
  117. });
  118. //# sourceMappingURL=CompleteAuthentication.js.map