camPassportCookie.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. *
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: ccl4j
  5. * (C) Copyright IBM Corp. 2009, 2010
  6. * US Government Users Restricted Rights – Use, duplication or disclosure restricted
  7. * by GSA ADP Schedule Contract with IBM Corp.
  8. *
  9. */
  10. /**
  11. * A Javascript file that provides a set of functions to retrieve the parameters of the cam passport cookie. A web64 encoded
  12. * value is expected.
  13. *
  14. * Functions provided:
  15. * canCurrentUserCallLogon
  16. * getPassportGeneration
  17. * getPassportID
  18. * getVersion
  19. * isLoggingEnabled
  20. * isCamConfiguredForAnonymous
  21. * isCurrentUserAnonymous
  22. * areCamNamespacesConfigured
  23. *
  24. */
  25. var web64EncodedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*-";
  26. var web64PadChar = '_';
  27. // delimiter for fields in the cookie.
  28. var DELIMITER = ";";
  29. // indicates that a feature is enabled.
  30. var FLAG_SET = "1";
  31. var PID_HEADER_SIZE = 4;
  32. var PID_HEADER_NAMESPACES_CONFIGURED_FLAG_START = 0;
  33. var PID_HEADER_NAMESPACES_CONFIGURED_FLAG_END = 1;
  34. var PID_HEADER_ANONYMOUS_CONFIGURED_FLAG_START = 1;
  35. var PID_HEADER_ANONYMOUS_CONFIGURED_FLAG_END = 2;
  36. var PID_HEADER_NAMED_USER_FLAG_START = 2;
  37. var PID_HEADER_NAMED_USER_FLAG_END = 3;
  38. // The cookie can have 2 formats.
  39. // The new format is:
  40. // version;passportID;loggingEnabled;passportGeneration;canUserCallLogon;
  41. // i.e.
  42. // 1;010:d1c8943e-0cf8-94f4-a5dc-a052647fc099:0231092414;0;3;0;
  43. // The old format is:
  44. // passportID
  45. // i.e. 010:d1c8943e-0cf8-94f4-a5dc-a052647fc099:0231092414
  46. function web64Decode(text)
  47. {
  48. if(text == null || text.length == 0 || text.length % 4 != 0)
  49. return text;
  50. //web64 Decoding. Note: Does currently not support UTF8, or charCodeAt() values above 255.
  51. //Initialize the decode object based on the encode string.
  52. var decodeChars = {};
  53. for (var i=0; i<64; i++)
  54. {
  55. decodeChars[web64EncodedChars.charAt(i)] = i;
  56. }
  57. decodeChars[web64PadChar] = 64;
  58. var decodedString = "";
  59. var sourceLength = text.length;
  60. for (var i=0; i<sourceLength; i+=4)
  61. {
  62. //Get the next 4 characters. The last two characters could be padding characters, which will have a value of 64.
  63. var charValue1 = decodeChars[text.charAt(i)];
  64. var charValue2 = decodeChars[text.charAt(i+1)];
  65. var charValue3 = decodeChars[text.charAt(i+2)];
  66. var charValue4 = decodeChars[text.charAt(i+3)];
  67. decodedString += String.fromCharCode((charValue1 << 2) + (charValue2 >> 4));
  68. charValue2 = (charValue2 & 15) << 4;
  69. if (charValue3 != 64)
  70. {
  71. decodedString += String.fromCharCode(charValue2 + (charValue3 >> 2));
  72. charValue3 = (charValue3 & 3) << 6;
  73. if (charValue4 != 64)
  74. {
  75. decodedString += String.fromCharCode(charValue3 + charValue4);
  76. }
  77. }
  78. }
  79. return decodedString;
  80. }
  81. function parse(camPassportCookie)
  82. {
  83. // set defaults
  84. var cookieObj = new Object();
  85. cookieObj.version = 0;
  86. cookieObj.passportId = null;
  87. cookieObj.loggingEnabled = true;
  88. cookieObj.passportGeneration = 0;
  89. cookieObj.canUserCallLogon = true;
  90. var values = new Array();
  91. try
  92. {
  93. if (camPassportCookie != null && camPassportCookie.length > 0)
  94. {
  95. if (camPassportCookie.charAt(3) == ':')
  96. {
  97. // old format
  98. cookieObj.passportId = camPassportCookie;
  99. cookieObj.passportGeneration = 1;
  100. }
  101. else
  102. {
  103. // get version
  104. var startPos = 0;
  105. var endPos = camPassportCookie.indexOf(DELIMITER, startPos);
  106. if(endPos == -1)
  107. throw "";
  108. values[0] = camPassportCookie.substring(startPos, endPos);
  109. startPos = endPos + 1;
  110. // get passportId
  111. endPos = camPassportCookie.indexOf(DELIMITER, startPos);
  112. if(endPos == -1)
  113. throw "";
  114. values[1] = camPassportCookie.substring(startPos, endPos);
  115. startPos = endPos + 1;
  116. // get loggingEnabled
  117. endPos = camPassportCookie.indexOf(DELIMITER, startPos);
  118. if(endPos == -1)
  119. throw "";
  120. values[2] = camPassportCookie.substring(startPos, endPos);
  121. startPos = endPos + 1;
  122. // get passportGeneration
  123. endPos = camPassportCookie.indexOf(DELIMITER, startPos);
  124. if(endPos == -1)
  125. throw "";
  126. values[3] = camPassportCookie.substring(startPos, endPos);
  127. startPos = endPos + 1;
  128. // get canUserCallLogon
  129. endPos = camPassportCookie.indexOf(DELIMITER, startPos);
  130. if(endPos == -1)
  131. throw "";
  132. values[4] = camPassportCookie.substring(startPos, endPos);
  133. }
  134. }
  135. }
  136. catch(err)
  137. {
  138. }
  139. if (values.length == 5)
  140. {
  141. // cam passport cookie is valid, copy the values
  142. cookieObj.version = values[0];
  143. cookieObj.passportId = values[1];
  144. cookieObj.loggingEnabled = values[2];
  145. cookieObj.passportGeneration = values[3];
  146. cookieObj.canUserCallLogon = values[4];
  147. }
  148. return cookieObj;
  149. }
  150. function canCurrentUserCallLogon(/* web64 encoded string */ camPassportCookie)
  151. {
  152. var decodedValue = web64Decode(camPassportCookie);
  153. if (parse(decodedValue).canUserCallLogon == FLAG_SET)
  154. return true;
  155. else
  156. return false;
  157. }
  158. function getPassportGeneration(/* web64 encoded string */ camPassportCookie)
  159. {
  160. var decodedValue = web64Decode(camPassportCookie);
  161. return parse(decodedValue).passportGeneration;
  162. }
  163. function getPassportID(/* web64 encoded string */ camPassportCookie)
  164. {
  165. var decodedValue = web64Decode(camPassportCookie);
  166. return parse(decodedValue).passportId;
  167. }
  168. function getVersion(/* web64 encoded string */ camPassportCookie)
  169. {
  170. var decodedValue = web64Decode(camPassportCookie);
  171. return parse(decodedValue).version;
  172. }
  173. function isLoggingEnabled(/* web64 encoded string */ camPassportCookie)
  174. {
  175. var decodedValue = web64Decode(camPassportCookie);
  176. if (parse(decodedValue).loggingEnabled == FLAG_SET)
  177. return true;
  178. else
  179. return false;
  180. }
  181. function isCamConfiguredForAnonymous(/* web64 encoded string */ camPassportCookie)
  182. {
  183. var decodedValue = web64Decode(camPassportCookie);
  184. var passportId = parse(decodedValue).passportId;
  185. if (passportId != null && passportId.length > PID_HEADER_SIZE)
  186. {
  187. var flagBit =
  188. passportId.substring(PID_HEADER_ANONYMOUS_CONFIGURED_FLAG_START, PID_HEADER_ANONYMOUS_CONFIGURED_FLAG_END);
  189. if (flagBit == FLAG_SET)
  190. return true;
  191. }
  192. return false;
  193. }
  194. function isCurrentUserAnonymous(/* web64 encoded string */ camPassportCookie)
  195. {
  196. var decodedValue = web64Decode(camPassportCookie);
  197. var passportId = parse(decodedValue).passportId;
  198. if (passportId != null && passportId.length > PID_HEADER_SIZE)
  199. {
  200. var flagBit = passportId.substring(PID_HEADER_NAMED_USER_FLAG_START, PID_HEADER_NAMED_USER_FLAG_END);
  201. if (flagBit == FLAG_SET)
  202. return false;
  203. else
  204. return true;
  205. }
  206. return false;
  207. }
  208. function areCamNamespacesConfigured(/* web64 encoded string */ camPassportCookie)
  209. {
  210. var decodedValue = web64Decode(camPassportCookie);
  211. var passportId = parse(decodedValue).passportId;
  212. if (passportId != null && passportId.length > PID_HEADER_SIZE)
  213. {
  214. var flagBit =
  215. passportId.substring(PID_HEADER_NAMESPACES_CONFIGURED_FLAG_START, PID_HEADER_NAMESPACES_CONFIGURED_FLAG_END);
  216. if (flagBit == FLAG_SET)
  217. return true;
  218. }
  219. return false;
  220. }