loginScriptSample.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2015, 2016, 2017
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or disclosure
  8. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *+------------------------------------------------------------------------+
  10. */
  11. // Login via the CMS (Cognos Mashup Service) API
  12. function login() {
  13. var xmlhttp = new XMLHttpRequest();
  14. var oForm = document.forms[0];
  15. // grab the login form input values
  16. var namespace = oForm.elements.namespace.value;
  17. var userID = oForm.elements.userid.value;
  18. var password = oForm.elements.password.value;
  19. var canAuthenticate = true;
  20. // sent a POST request to the CMS Login API
  21. xmlhttp.onreadystatechange = function () {
  22. if (xmlhttp.readyState === XMLHttpRequest.DONE) {
  23. // if not logged in attempt to login
  24. if (xmlhttp.status === 200) {
  25. // Redirect to page with embedded iFrames after authenticated
  26. window.location ='iFrameSample.html';
  27. } else if (xmlhttp.status === 441 || xmlhttp.status === 403) {
  28. var loginContext = getLoginContext(namespace, userID, password);
  29. canAuthenticate = false;
  30. xmlhttp.open("POST", "/bi/v1/disp/rds/auth/logon", false);
  31. xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  32. xmlhttp.send(loginContext);
  33. }
  34. }
  35. };
  36. // attempt to GET home perspective to trigger a 441 if not authenticated
  37. xmlhttp.open("GET", '/bi/v1/perspectives/home' + '?' + Date.now(), true);
  38. xmlhttp.send();
  39. }
  40. function getLoginContext(nameSpace, userID, password) {
  41. var context = "xmlData=<credentials>"
  42. +"<credentialElements><name>CAMNamespace</name><label>Namespace:</label>"
  43. +"<value><actualValue>"+nameSpace+"</actualValue></value>"
  44. +"</credentialElements><credentialElements><name>CAMUsername</name><label>User ID:</label>"
  45. +"<value><actualValue>"+userID+"</actualValue></value>"
  46. +"</credentialElements><credentialElements><name>CAMPassword</name><label>Password:</label>"
  47. +"<value><actualValue>"+password+"</actualValue></value>"
  48. +"</credentialElements></credentials>";
  49. return context;
  50. }