1class PiecesPHPGenericHandlerSession { 2 3 constructor(tokenName, onLogout, expectedLang) { 4 this.tokenName = tokenName 5 this.onLogout = typeof onLogout == 'function' ? onLogout : () => { } 6 this.expectedLang = typeof expectedLang == 'string' && expectedLang.trim().length > 0 ? expectedLang : null 7 this.verifyPairLocalStorageAndCookieSession() 8 } 9 10 verifyPairLocalStorageAndCookieSession() { 11 12 const currentJWT = this.getJWT() 13 let currentJWTFromCookie = this.getCookie(this.tokenName) 14 15 /** 16 * Dado que pueden diferir los valores del cookie en "domain" puede replicarse el cookie con el mismo nombre. 17 * De modo que se convierte siempre en un array de strings. 18 * Si ya es un array porque se dió la duplicación se valida que solo contenga strings. 19 * Finalmente se valida que todos los valores sean idénticos (un mismo token) y se reduce a uno solo. 20 * Si es null, no se hace nada 21 */ 22 currentJWTFromCookie = typeof currentJWTFromCookie == 'string' ? [currentJWTFromCookie] : currentJWTFromCookie 23 currentJWTFromCookie = Array.isArray(currentJWTFromCookie) ? currentJWTFromCookie.filter((value) => { return typeof value === "string" && value.length > 0 }) : currentJWTFromCookie 24 currentJWTFromCookie = Array.isArray(currentJWTFromCookie) && currentJWTFromCookie.every(value => value === currentJWTFromCookie[0]) ? currentJWTFromCookie[0] : null 25 26 if (currentJWT == '' && currentJWTFromCookie !== null) { 27 //Si hay JWT en cookies pero no en localstorage se copia el de cookie 28 this.setJWT(currentJWTFromCookie) 29 } else if (currentJWT != currentJWTFromCookie) { 30 //Si hay JWT en ambos pero difieren se borra 31 this.deleteSession() 32 } 33 } 34 35 setJWT(JWT) { 36 if (typeof JWT != 'string') { 37 JWT = '' 38 } 39 document.cookie = `${this.tokenName}=${encodeURI(JWT)};path=/`; 40 document.cookie = `${this.tokenName}=${encodeURI(JWT)};path=/;domain=${location.hostname}`; 41 document.cookie = `${this.tokenName}=${encodeURI(JWT)};path=/;domain=.${location.hostname}`; 42 localStorage.setItem(this.tokenName, encodeURI(JWT)) 43 } 44 45 getJWT() { 46 let JWT = localStorage.getItem(this.tokenName) 47 if (typeof JWT != 'string') { 48 JWT = '' 49 } 50 return JWT 51 } 52 53 deleteSession() { 54 55 let now = new Date().getTime() 56 let JWT = this.getJWT() 57 let JWTFromCookie = this.getCookie(this.tokenName)
58 JWTFromCookie = typeof JWTFromCookie == 'string' ? JWTFromCookie.trim() : '' 59 JWT = JWT.length > 0 ? JWT : JWTFromCookie 60 this.onLogout() 61 62 if (JWT.length > 0) { 63 document.cookie = `${this.tokenName}=;expires=${now};path=/`; 64 document.cookie = `${this.tokenName}=;expires=${now};path=/;domain=${location.hostname}`; 65 document.cookie = `${this.tokenName}=;expires=${now};path=/;domain=.${location.hostname}`; 66 localStorage.removeItem(this.tokenName) 67 window.location.reload() 68 } 69 70 } 71 72 getCookies() { 73 74 let cookies = document.cookie 75 let parsedCookies = {} 76 let hasValues = false 77 let valueParsed = null 78 79 cookies = cookies.split(';') 80 81 if (Array.isArray(cookies)) { 82 83 for (let cookie of cookies) { 84 85 let pair = cookie.split('=') 86 87 if (Array.isArray(pair) && pair.length === 2) { 88 89 let name = pair[0].trim() 90 let value = pair[1] 91 92 if (typeof parsedCookies[name] === 'undefined') { 93 parsedCookies[name] = [] 94 } 95 96 parsedCookies[name].push(value) 97 hasValues = true 98 99 } 100 101 } 102 103 for (let name in parsedCookies) { 104 105 let values = parsedCookies[name] 106 107 if (Array.isArray(values) && values.length === 1) { 108 109 parsedCookies[name] = values[0] 110 111 } 112 113 } 114 115 } 116 117 if (hasValues) { 118 valueParsed = parsedCookies 119 } 120 121 return valueParsed 122 } 123 124 getCookie(name) { 125 126 let cookies = this.getCookies() 127 let value = null 128 129 if (cookies !== null && typeof name == 'string' && typeof cookies[name] != 'undefined') { 130 value = cookies[name] 131 } 132 133 return value 134 } 135 136 /** 137 * @param {String|URL} url 138 * @param {String|HTMLFormElement} data 139 * @param {Object} headers 140 * @returns {Promise<Response>} 141 */ 142 post(url, data, headers = {}) { 143 144 url = (typeof url == 'string' && url.trim().length > 0) || url instanceof URL ? url : '' 145 data = typeof data == 'object' || data instanceof FormData ? data : {} 146 headers = typeof headers == 'object' ? headers : {} 147 148 let options = { 149 method: 'POST', 150 headers: new Headers(), 151 mode: 'cors', 152 credentials: 'include', 153 } 154 155 let parsedHeaders = this.parseHeaders(headers) 156 157 if (data instanceof FormData) { 158 159 options.cache = 'no-store' 160 options.body = data 161 162 } else if (typeof data == 'object') { 163 164 options.body = JSON.stringify(data) 165 parsedHeaders.set('Content-Type', 'application/json') 166 167 } 168 169 for (let name of parsedHeaders.keys()) { 170 options.headers.append(name, parsedHeaders.get(name)) 171 } 172 173 return this.ajax(url, options) 174 } 175 176 /** 177 * @param {String|URL} url 178 * @param {String|HTMLFormElement} data 179 * @param {Object} headers 180 * @returns {Promise<Response>} 181 */ 182 get(url, data, headers = {}) { 183 184 url = (typeof url == 'string' && url.trim().length > 0) || url instanceof URL ? url : '' 185 data = typeof data == 'string' || data instanceof HTMLFormElement ? data : '' 186 headers = typeof headers == 'object' ? headers : {} 187 188 let options = { 189 method: 'GET', 190 headers: new Headers(), 191 mode: 'cors', 192 credentials: 'include', 193 } 194 195 let parsedHeaders = this.parseHeaders(headers) 196 parsedHeaders.set('Content-Type', 'application/x-www-form-urlencoded') 197 198 let urlParams = new URLSearchParams() 199 200 if (data instanceof HTMLFormElement) { 201 202 let formData = new FormData(data) 203 204 for (let key of formData.keys()) { 205 urlParams.append(key, formData.get(key)) 206 } 207 208 url += `?${urlParams.toString()}` 209 210 } else if (typeof data == 'string') { 211 url += `?${data}` 212 } 213 214 for (let name of parsedHeaders.keys()) { 215 options.headers.append(name, parsedHeaders.get(name)) 216 } 217 218 return this.ajax(url, options) 219 } 220 221 parseHeaders(headers = {}) { 222 223 let mapHeaders = new Map() 224 225 if (typeof headers == 'object') { 226 227 for (let name in headers) { 228 229 let value = headers[name] 230 let valueString = '' 231 232 if (Array.isArray(value)) { 233 234 let length = value.length 235 let lastIndexValue = 0 236 237 if (length == 1) { 238 lastIndexValue = 0 239 } else if (length > 1) { 240 lastIndexValue = length - 1 241 } 242 243 for (let i = 0; i < length; i++) { 244 if (i == lastIndexValue) { 245 valueString += value[i] 246 } else { 247 valueString += value[i] + "\r\n" 248 } 249 } 250 251 } else if (typeof value == 'string') { 252 valueString = value 253 } 254 255 let normalizedName = this.normalizeHeaderName(name) 256 mapHeaders.set(normalizedName, valueString) 257 258 } 259 260 } 261 262 mapHeaders.set(this.tokenName, this.getJWT()) 263 mapHeaders.set('X-Requested-With', 'XMLHttpRequest') 264 if (this.expectedLang !== null) { 265 mapHeaders.set('PCSPHP-Response-Expected-Language', this.expectedLang) 266 } 267 268 return mapHeaders 269 270 } 271 272 normalizeHeaderName(name) { 273 return typeof name == 'string' ? name = name.trim().replace(/\s{1,}/gmi, '').split('-').map(e => this.capitalize(e)).join('-') : name 274 } 275 276 capitalize(str) { 277 return typeof str == 'string' ? str.split('').map(e, i => i == 0 ? e.toUpperCase() : e).join('') : '' 278 } 279 280 /** 281 * @param {RequestInfo} url 282 * @param {RequestInit} [options] 283 * @returns {Promise<Response>} 284 */ 285 ajax(url, options) { 286 return fetch(url, options) 287 } 288 289}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.