angular-cookies.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @license AngularJS v1.8.2
  3. * (c) 2010-2020 Google LLC. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function(window, angular) {'use strict';
  7. /**
  8. * @ngdoc module
  9. * @name ngCookies
  10. * @description
  11. *
  12. * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
  13. *
  14. * See {@link ngCookies.$cookies `$cookies`} for usage.
  15. */
  16. angular.module('ngCookies', ['ng']).
  17. info({ angularVersion: '1.8.2' }).
  18. /**
  19. * @ngdoc provider
  20. * @name $cookiesProvider
  21. * @description
  22. * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
  23. * */
  24. provider('$cookies', [/** @this */function $CookiesProvider() {
  25. /**
  26. * @ngdoc property
  27. * @name $cookiesProvider#defaults
  28. * @description
  29. *
  30. * Object containing default options to pass when setting cookies.
  31. *
  32. * The object may have following properties:
  33. *
  34. * - **path** - `{string}` - The cookie will be available only for this path and its
  35. * sub-paths. By default, this is the URL that appears in your `<base>` tag.
  36. * - **domain** - `{string}` - The cookie will be available only for this domain and
  37. * its sub-domains. For security reasons the user agent will not accept the cookie
  38. * if the current domain is not a sub-domain of this domain or equal to it.
  39. * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
  40. * or a Date object indicating the exact date/time this cookie will expire.
  41. * - **secure** - `{boolean}` - If `true`, then the cookie will only be available through a
  42. * secured connection.
  43. * - **samesite** - `{string}` - prevents the browser from sending the cookie along with cross-site requests.
  44. * Accepts the values `lax` and `strict`. See the [OWASP Wiki](https://www.owasp.org/index.php/SameSite)
  45. * for more info. Note that as of May 2018, not all browsers support `SameSite`,
  46. * so it cannot be used as a single measure against Cross-Site-Request-Forgery (CSRF) attacks.
  47. *
  48. * Note: By default, the address that appears in your `<base>` tag will be used as the path.
  49. * This is important so that cookies will be visible for all routes when html5mode is enabled.
  50. *
  51. * @example
  52. *
  53. * ```js
  54. * angular.module('cookiesProviderExample', ['ngCookies'])
  55. * .config(['$cookiesProvider', function($cookiesProvider) {
  56. * // Setting default options
  57. * $cookiesProvider.defaults.domain = 'foo.com';
  58. * $cookiesProvider.defaults.secure = true;
  59. * }]);
  60. * ```
  61. **/
  62. var defaults = this.defaults = {};
  63. function calcOptions(options) {
  64. return options ? angular.extend({}, defaults, options) : defaults;
  65. }
  66. /**
  67. * @ngdoc service
  68. * @name $cookies
  69. *
  70. * @description
  71. * Provides read/write access to browser's cookies.
  72. *
  73. * <div class="alert alert-info">
  74. * Up until AngularJS 1.3, `$cookies` exposed properties that represented the
  75. * current browser cookie values. In version 1.4, this behavior has changed, and
  76. * `$cookies` now provides a standard api of getters, setters etc.
  77. * </div>
  78. *
  79. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  80. *
  81. * @example
  82. *
  83. * ```js
  84. * angular.module('cookiesExample', ['ngCookies'])
  85. * .controller('ExampleController', ['$cookies', function($cookies) {
  86. * // Retrieving a cookie
  87. * var favoriteCookie = $cookies.get('myFavorite');
  88. * // Setting a cookie
  89. * $cookies.put('myFavorite', 'oatmeal');
  90. * }]);
  91. * ```
  92. */
  93. this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
  94. return {
  95. /**
  96. * @ngdoc method
  97. * @name $cookies#get
  98. *
  99. * @description
  100. * Returns the value of given cookie key
  101. *
  102. * @param {string} key Id to use for lookup.
  103. * @returns {string} Raw cookie value.
  104. */
  105. get: function(key) {
  106. return $$cookieReader()[key];
  107. },
  108. /**
  109. * @ngdoc method
  110. * @name $cookies#getObject
  111. *
  112. * @description
  113. * Returns the deserialized value of given cookie key
  114. *
  115. * @param {string} key Id to use for lookup.
  116. * @returns {Object} Deserialized cookie value.
  117. */
  118. getObject: function(key) {
  119. var value = this.get(key);
  120. return value ? angular.fromJson(value) : value;
  121. },
  122. /**
  123. * @ngdoc method
  124. * @name $cookies#getAll
  125. *
  126. * @description
  127. * Returns a key value object with all the cookies
  128. *
  129. * @returns {Object} All cookies
  130. */
  131. getAll: function() {
  132. return $$cookieReader();
  133. },
  134. /**
  135. * @ngdoc method
  136. * @name $cookies#put
  137. *
  138. * @description
  139. * Sets a value for given cookie key
  140. *
  141. * @param {string} key Id for the `value`.
  142. * @param {string} value Raw value to be stored.
  143. * @param {Object=} options Options object.
  144. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  145. */
  146. put: function(key, value, options) {
  147. $$cookieWriter(key, value, calcOptions(options));
  148. },
  149. /**
  150. * @ngdoc method
  151. * @name $cookies#putObject
  152. *
  153. * @description
  154. * Serializes and sets a value for given cookie key
  155. *
  156. * @param {string} key Id for the `value`.
  157. * @param {Object} value Value to be stored.
  158. * @param {Object=} options Options object.
  159. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  160. */
  161. putObject: function(key, value, options) {
  162. this.put(key, angular.toJson(value), options);
  163. },
  164. /**
  165. * @ngdoc method
  166. * @name $cookies#remove
  167. *
  168. * @description
  169. * Remove given cookie
  170. *
  171. * @param {string} key Id of the key-value pair to delete.
  172. * @param {Object=} options Options object.
  173. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  174. */
  175. remove: function(key, options) {
  176. $$cookieWriter(key, undefined, calcOptions(options));
  177. }
  178. };
  179. }];
  180. }]);
  181. /**
  182. * @name $$cookieWriter
  183. * @requires $document
  184. *
  185. * @description
  186. * This is a private service for writing cookies
  187. *
  188. * @param {string} name Cookie name
  189. * @param {string=} value Cookie value (if undefined, cookie will be deleted)
  190. * @param {Object=} options Object with options that need to be stored for the cookie.
  191. */
  192. function $$CookieWriter($document, $log, $browser) {
  193. var cookiePath = $browser.baseHref();
  194. var rawDocument = $document[0];
  195. function buildCookieString(name, value, options) {
  196. var path, expires;
  197. options = options || {};
  198. expires = options.expires;
  199. path = angular.isDefined(options.path) ? options.path : cookiePath;
  200. if (angular.isUndefined(value)) {
  201. expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
  202. value = '';
  203. }
  204. if (angular.isString(expires)) {
  205. expires = new Date(expires);
  206. }
  207. var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
  208. str += path ? ';path=' + path : '';
  209. str += options.domain ? ';domain=' + options.domain : '';
  210. str += expires ? ';expires=' + expires.toUTCString() : '';
  211. str += options.secure ? ';secure' : '';
  212. str += options.samesite ? ';samesite=' + options.samesite : '';
  213. // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
  214. // - 300 cookies
  215. // - 20 cookies per unique domain
  216. // - 4096 bytes per cookie
  217. var cookieLength = str.length + 1;
  218. if (cookieLength > 4096) {
  219. $log.warn('Cookie \'' + name +
  220. '\' possibly not set or overflowed because it was too large (' +
  221. cookieLength + ' > 4096 bytes)!');
  222. }
  223. return str;
  224. }
  225. return function(name, value, options) {
  226. rawDocument.cookie = buildCookieString(name, value, options);
  227. };
  228. }
  229. $$CookieWriter.$inject = ['$document', '$log', '$browser'];
  230. angular.module('ngCookies').provider('$$cookieWriter', /** @this */ function $$CookieWriterProvider() {
  231. this.$get = $$CookieWriter;
  232. });
  233. })(window, window.angular);