123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. * Copyright (c) 2015-2016, Okta, Inc. and/or its affiliates. All rights reserved.
  3. * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
  4. *
  5. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
  6. * Unless required by applicable law or agreed to in writing, software
  7. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  8. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. *
  10. * See the License for the specific language governing permissions and limitations under the License.
  11. */
  12. /* eslint-env es6 */
  13. var util = module.exports;
  14. util.base64UrlToBase64 = function(b64u) {
  15. return b64u.replace(/\-/g, '+').replace(/_/g, '/');
  16. };
  17. util.base64UrlToString = function(b64u) {
  18. var b64 = util.base64UrlToBase64(b64u);
  19. switch (b64.length % 4) {
  20. case 0:
  21. break;
  22. case 2:
  23. b64 += '==';
  24. break;
  25. case 3:
  26. b64 += '=';
  27. break;
  28. default:
  29. throw 'Not a valid Base64Url';
  30. }
  31. var utf8 = atob(b64);
  32. try {
  33. return decodeURIComponent(escape(utf8));
  34. } catch (e) {
  35. return utf8;
  36. }
  37. };
  38. util.stringToBuffer = function(str) {
  39. var buffer = new Uint8Array(str.length);
  40. for (var i = 0; i < str.length; i++) {
  41. buffer[i] = str.charCodeAt(i);
  42. }
  43. return buffer;
  44. };
  45. util.base64UrlDecode = function(str) {
  46. return atob(util.base64UrlToBase64(str));
  47. };
  48. util.bind = function(fn, ctx) {
  49. var additionalArgs = Array.prototype.slice.call(arguments, 2);
  50. return function() {
  51. var args = Array.prototype.slice.call(arguments);
  52. args = additionalArgs.concat(args);
  53. return fn.apply(ctx, args);
  54. };
  55. };
  56. util.isAbsoluteUrl = function(url) {
  57. return /^(?:[a-z]+:)?\/\//i.test(url);
  58. };
  59. util.isString = function(obj) {
  60. return Object.prototype.toString.call(obj) === '[object String]';
  61. };
  62. util.isObject = function(obj) {
  63. return Object.prototype.toString.call(obj) === '[object Object]';
  64. };
  65. util.isNumber = function(obj) {
  66. return Object.prototype.toString.call(obj) === '[object Number]';
  67. };
  68. util.isArray = function(obj) {
  69. return Object.prototype.toString.call(obj) === '[object Array]';
  70. };
  71. util.isoToUTCString = function(str) {
  72. var parts = str.match(/\d+/g),
  73. isoTime = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]),
  74. isoDate = new Date(isoTime);
  75. return isoDate.toUTCString();
  76. };
  77. util.toQueryParams = function(obj) {
  78. var str = [];
  79. if (obj !== null) {
  80. for (var key in obj) {
  81. if (obj.hasOwnProperty(key) &&
  82. obj[key] !== undefined &&
  83. obj[key] !== null) {
  84. str.push(key + '=' + encodeURIComponent(obj[key]));
  85. }
  86. }
  87. }
  88. if (str.length) {
  89. return '?' + str.join('&');
  90. } else {
  91. return '';
  92. }
  93. };
  94. util.genRandomString = function(length) {
  95. var randomCharset = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  96. var random = '';
  97. for (var c = 0, cl = randomCharset.length; c < length; ++c) {
  98. random += randomCharset[Math.floor(Math.random() * cl)];
  99. }
  100. return random;
  101. };
  102. util.extend = function() {
  103. var obj1 = arguments[0];
  104. var objArray = [].slice.call(arguments, 1);
  105. objArray.forEach(function(obj) {
  106. for (var prop in obj) {
  107. if (obj.hasOwnProperty(prop)) {
  108. obj1[prop] = obj[prop];
  109. }
  110. }
  111. });
  112. };
  113. util.removeNils = function(obj) {
  114. var cleaned = {};
  115. for (var prop in obj) {
  116. if (obj.hasOwnProperty(prop)) {
  117. var value = obj[prop];
  118. if (value !== null && value !== undefined) {
  119. cleaned[prop] = value;
  120. }
  121. }
  122. }
  123. return cleaned;
  124. };
  125. util.clone = function(obj) {
  126. if (obj) {
  127. var str = JSON.stringify(obj);
  128. if (str) {
  129. return JSON.parse(str);
  130. }
  131. }
  132. return obj;
  133. };
  134. // Analogous to _.omit
  135. util.omit = function(obj) {
  136. var props = Array.prototype.slice.call(arguments, 1);
  137. var newobj = {};
  138. for (var p in obj) {
  139. if (obj.hasOwnProperty(p) && props.indexOf(p) == -1) {
  140. newobj[p] = obj[p];
  141. }
  142. }
  143. return util.clone(newobj);
  144. };
  145. util.find = function(collection, searchParams) {
  146. var c = collection.length;
  147. while (c--) {
  148. var item = collection[c];
  149. var found = true;
  150. for (var prop in searchParams) {
  151. if (!searchParams.hasOwnProperty(prop)) {
  152. continue;
  153. }
  154. if (item[prop] !== searchParams[prop]) {
  155. found = false;
  156. break;
  157. }
  158. }
  159. if (found) {
  160. return item;
  161. }
  162. }
  163. };
  164. util.getLink = function(obj, linkName, altName) {
  165. if (!obj || !obj._links) {
  166. return;
  167. }
  168. var link = util.clone(obj._links[linkName]);
  169. // If a link has a name and we have an altName, return if they match
  170. if (link && link.name && altName) {
  171. if (link.name === altName) {
  172. return link;
  173. }
  174. } else {
  175. return link;
  176. }
  177. };
  178. util.getNativeConsole = function() {
  179. return window.console;
  180. };
  181. util.getConsole = function() {
  182. var nativeConsole = util.getNativeConsole();
  183. if (nativeConsole && nativeConsole.log) {
  184. return nativeConsole;
  185. }
  186. return {
  187. log: function() {}
  188. };
  189. };
  190. util.warn = function(text) {
  191. /* eslint-disable no-console */
  192. util.getConsole().log('[okta-auth-sdk] WARN: ' + text);
  193. /* eslint-enable */
  194. };
  195. util.deprecate = function(text) {
  196. /* eslint-disable no-console */
  197. util.getConsole().log('[okta-auth-sdk] DEPRECATION: ' + text);
  198. /* eslint-enable */
  199. };
  200. util.deprecateWrap = function(text, fn) {
  201. return function() {
  202. util.deprecate(text);
  203. return fn.apply(null, arguments);
  204. };
  205. };
  206. util.removeTrailingSlash = function(path) {
  207. if (!path) {
  208. return;
  209. }
  210. if (path.slice(-1) === '/') {
  211. return path.slice(0, -1);
  212. }
  213. return path;
  214. };
  215. util.isIE11OrLess = function() {
  216. return !!document.documentMode && document.documentMode <= 11;
  217. };