|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var config = require( './config.json' ), |
| 4 | + defaults = { |
| 5 | + prefix: config.prefix, |
| 6 | + domain: config.domain, |
| 7 | + path: config.path, |
| 8 | + expires: config.expires, |
| 9 | + secure: false, |
| 10 | + sameSite: '', |
| 11 | + sameSiteLegacy: config.sameSiteLegacy |
| 12 | + }; |
| 13 | + |
| 14 | +/** |
| 15 | + * Manage cookies in a way that is syntactically and functionally similar |
| 16 | + * to the `WebRequest#getCookie` and `WebResponse#setcookie` methods in PHP. |
| 17 | + * |
| 18 | + * @author Sam Smith <samsmith@wikimedia.org> |
| 19 | + * @author Matthew Flaschen <mflaschen@wikimedia.org> |
| 20 | + * |
| 21 | + * @class mw.cookie |
| 22 | + * @singleton |
| 23 | + */ |
| 24 | +mw.cookie = { |
| 25 | + |
| 26 | + /** |
| 27 | + * Set or delete a cookie. |
| 28 | + * |
| 29 | + * **Note:** If explicitly passing `null` or `undefined` for an options key, |
| 30 | + * that will override the default. This is natural in JavaScript, but noted |
| 31 | + * here because it is contrary to MediaWiki's `WebResponse#setcookie()` method |
| 32 | + * in PHP. |
| 33 | + * |
| 34 | + * When using this for persistent storage of identifiers (e.g. for tracking |
| 35 | + * sessions), be aware that persistence may vary slightly across browsers and |
| 36 | + * browser versions, and can be affected by a number of factors such as |
| 37 | + * storage limits (cookie eviction) and session restore features. |
| 38 | + * |
| 39 | + * Without an expiry, this creates a session cookie. In a browser, session cookies persist |
| 40 | + * for the lifetime of the browser *process*. Including across tabs, page views, and windows, |
| 41 | + * until the browser itself is *fully* closed, or until the browser clears all storage for |
| 42 | + * a given website. An exception to this is if the user evokes a "restore previous |
| 43 | + * session" feature that some browsers have. |
| 44 | + * |
| 45 | + * @param {string} key |
| 46 | + * @param {string|null} value Value of cookie. If `value` is `null` then this method will |
| 47 | + * instead remove a cookie by name of `key`. |
| 48 | + * @param {Object|Date|number} [options] Options object, or expiry date |
| 49 | + * @param {Date|number|null} [options.expires=wgCookieExpiration] The expiry date of the cookie, |
| 50 | + * or lifetime in seconds. If `options.expires` is null or 0, then a session cookie is set. |
| 51 | + * @param {string} [options.prefix=wgCookiePrefix] The prefix of the key |
| 52 | + * @param {string} [options.domain=wgCookieDomain] The domain attribute of the cookie |
| 53 | + * @param {string} [options.path=wgCookiePath] The path attribute of the cookie |
| 54 | + * @param {boolean} [options.secure=false] Whether or not to include the secure attribute. |
| 55 | + * (Does **not** use the wgCookieSecure configuration variable) |
| 56 | + * @param {string} [options.sameSite=''] The SameSite flag of the cookie ('None' / 'Lax' |
| 57 | + * / 'Strict', case-insensitive; default is to omit the flag, which results in Lax on |
| 58 | + * modern browsers). Set to None AND set secure=true if the cookie needs to be visible on |
| 59 | + * cross-domain requests. |
| 60 | + * @param {boolean} [options.sameSiteLegacy=$wgUseSameSiteLegacyCookies] If true, sameSite=None |
| 61 | + * cookies will also be sent as a non-SameSite cookie with an 'ss0-' prefix, to work around |
| 62 | + * old browsers interpreting the standard differently. |
| 63 | + */ |
| 64 | + set: function ( key, value, options ) { |
| 65 | + var prefix, date, sameSiteLegacy; |
| 66 | + |
| 67 | + // The 'options' parameter may be a shortcut for the expiry. |
| 68 | + if ( arguments.length > 2 && ( !options || options instanceof Date || typeof options === 'number' ) ) { |
| 69 | + options = { expires: options }; |
| 70 | + } |
| 71 | + // Apply defaults |
| 72 | + options = $.extend( {}, defaults, options ); |
| 73 | + |
| 74 | + // Don't pass invalid option to $.cookie |
| 75 | + prefix = options.prefix; |
| 76 | + delete options.prefix; |
| 77 | + |
| 78 | + if ( !options.expires ) { |
| 79 | + // Session cookie (null or zero) |
| 80 | + // Normalize to absent (undefined) for $.cookie. |
| 81 | + delete options.expires; |
| 82 | + } else if ( typeof options.expires === 'number' ) { |
| 83 | + // Lifetime in seconds |
| 84 | + date = new Date(); |
| 85 | + date.setTime( Number( date ) + ( options.expires * 1000 ) ); |
| 86 | + options.expires = date; |
| 87 | + } |
| 88 | + |
| 89 | + sameSiteLegacy = options.sameSiteLegacy; |
| 90 | + delete options.sameSiteLegacy; |
| 91 | + |
| 92 | + if ( value !== null ) { |
| 93 | + value = String( value ); |
| 94 | + } |
| 95 | + |
| 96 | + $.cookie( prefix + key, value, options ); |
| 97 | + if ( sameSiteLegacy && options.sameSite && options.sameSite.toLowerCase() === 'none' ) { |
| 98 | + // Make testing easy by not changing the object passed to the first $.cookie call |
| 99 | + options = $.extend( {}, options ); |
| 100 | + delete options.sameSite; |
| 101 | + $.cookie( prefix + 'ss0-' + key, value, options ); |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the value of a cookie. |
| 107 | + * |
| 108 | + * @param {string} key |
| 109 | + * @param {string} [prefix=wgCookiePrefix] The prefix of the key. If `prefix` is |
| 110 | + * `undefined` or `null`, then `wgCookiePrefix` is used |
| 111 | + * @param {Mixed} [defaultValue=null] |
| 112 | + * @return {string|null|Mixed} If the cookie exists, then the value of the |
| 113 | + * cookie, otherwise `defaultValue` |
| 114 | + */ |
| 115 | + get: function ( key, prefix, defaultValue ) { |
| 116 | + var result; |
| 117 | + |
| 118 | + if ( prefix === undefined || prefix === null ) { |
| 119 | + prefix = defaults.prefix; |
| 120 | + } |
| 121 | + |
| 122 | + // Was defaultValue omitted? |
| 123 | + if ( arguments.length < 3 ) { |
| 124 | + defaultValue = null; |
| 125 | + } |
| 126 | + |
| 127 | + result = $.cookie( prefix + key ); |
| 128 | + |
| 129 | + return result !== null ? result : defaultValue; |
| 130 | + }, |
| 131 | + |
| 132 | + /** |
| 133 | + * Get the value of a SameSite=None cookie, using the legacy ss0- cookie if needed. |
| 134 | + * |
| 135 | + * @param {string} key |
| 136 | + * @param {string} [prefix=wgCookiePrefix] The prefix of the key. If `prefix` is |
| 137 | + * `undefined` or `null`, then `wgCookiePrefix` is used |
| 138 | + * @param {Mixed} [defaultValue=null] |
| 139 | + * @return {string|null|Mixed} If the cookie exists, then the value of the |
| 140 | + * cookie, otherwise `defaultValue` |
| 141 | + */ |
| 142 | + getCrossSite: function ( key, prefix, defaultValue ) { |
| 143 | + var value; |
| 144 | + |
| 145 | + value = this.get( key, prefix, null ); |
| 146 | + if ( value === null ) { |
| 147 | + value = this.get( 'ss0-' + key, prefix, null ); |
| 148 | + } |
| 149 | + if ( value === null ) { |
| 150 | + value = defaultValue; |
| 151 | + } |
| 152 | + return value; |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +if ( window.QUnit ) { |
| 157 | + module.exports = { |
| 158 | + setDefaults: function ( value ) { |
| 159 | + var prev = defaults; |
| 160 | + defaults = value; |
| 161 | + return prev; |
| 162 | + } |
| 163 | + }; |
| 164 | +} |
0 commit comments