improve session cookie handling

This commit is contained in:
jkhsjdhjs 2019-08-15 01:06:22 +02:00
parent 9f498864bb
commit 7e7a19403c
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
3 changed files with 15 additions and 15 deletions

View File

@ -55,8 +55,8 @@ Returns an iterator over all domains currently stored cookies for.
#### *cookiesDomain(domain) #### *cookiesDomain(domain)
Returns an iterator over all cookies currently stored for `domain`. Returns an iterator over all cookies currently stored for `domain`.
#### *cookiesValid() #### *cookiesValid(withSession)
Returns an iterator over all valid (non-expired) cookies. Returns an iterator over all valid (non-expired) cookies. Includes session cookies if `withSession` is `true`.
#### *cookiesAll() #### *cookiesAll()
Returns an iterator over all cookies currently stored. Returns an iterator over all cookies currently stored.
@ -65,10 +65,10 @@ Returns an iterator over all cookies currently stored.
Returns an iterator over all cookies valid for a request to `url`. Returns an iterator over all cookies valid for a request to `url`.
#### deleteExpired() #### deleteExpired()
Removes all expired cookies from the jar. Removes all expired cookies from the jar (including session cookies).
#### save() #### save()
Saves the cookie jar to disk. Only non-expired cookies are saved. Saves the cookie jar to disk. Only non-expired non-session cookies are saved.
### Class: Cookie ### Class: Cookie
@ -77,11 +77,11 @@ An abstract representation of a cookie.
#### Properties #### Properties
- `name` The identifier of the cookie. - `name` The identifier of the cookie.
- `value` The value of the cookie. - `value` The value of the cookie.
- `expiry` A [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object of the cookies expiry date. - `expiry` A [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object of the cookies expiry date or `null`, if the cookie expires with the session.
- `domain` The domain the cookie is valid for. - `domain` The domain the cookie is valid for.
- `path` The path the cookie is valid for. - `path` The path the cookie is valid for.
- `secure` A boolean value representing the cookie's secure attribute. If set the cookie will only be used for `https` requests. - `secure` A boolean value representing the cookie's secure attribute. If set the cookie will only be used for `https` requests.
- `subdomains` A boolean value specifying whether the cookie should be used for subdomains of the domain or not. - `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not.
#### new Cookie(cookie, url) #### new Cookie(cookie, url)
- `cookie` The string representation of a cookie as send by a webserver. - `cookie` The string representation of a cookie as send by a webserver.
@ -93,8 +93,8 @@ Creates a cookie instance from an already existing object with the same properti
#### serialize() #### serialize()
Serializes the cookie, transforming it to `name=value` so it can be used in requests. Serializes the cookie, transforming it to `name=value` so it can be used in requests.
#### hasExpired() #### hasExpired(sessionEnded)
Returns whether the cookie has expired or not. Returns whether the cookie has expired or not. `sessionEnded` specifies whether the current session has ended, meaning if set to `true`, the function will return `true` for session cookies.
#### isValidForRequest(url) #### isValidForRequest(url)
Returns whether the cookie is valid for a request to `url`. If not, it won't be send by the fetch wrapper. Returns whether the cookie is valid for a request to `url`. If not, it won't be send by the fetch wrapper.

View File

@ -45,9 +45,9 @@ export default class CookieJar {
for(const cookie of (this.cookies.get(domain) || []).values()) for(const cookie of (this.cookies.get(domain) || []).values())
yield cookie; yield cookie;
} }
*cookiesValid() { *cookiesValid(withSession) {
for(const cookie of this.cookiesAll()) for(const cookie of this.cookiesAll())
if(!cookie.hasExpired()) if(!cookie.hasExpired(!withSession))
yield cookie; yield cookie;
} }
*cookiesAll() { *cookiesAll() {
@ -73,7 +73,7 @@ export default class CookieJar {
} }
} }
deleteExpired() { deleteExpired() {
const validCookies = [...this.cookiesValid()]; const validCookies = [...this.cookiesValid(false)];
this.cookies = new Map(); this.cookies = new Map();
validCookies.forEach(c => this.addCookie(c)); validCookies.forEach(c => this.addCookie(c));
} }
@ -81,6 +81,6 @@ export default class CookieJar {
if(typeof this.file !== "string") if(typeof this.file !== "string")
throw new Error("No file has been specified for this cookie jar!"); throw new Error("No file has been specified for this cookie jar!");
// only save cookies that haven't expired // only save cookies that haven't expired
fs.writeFileSync(this.file, JSON.stringify([...this.cookiesValid()])); fs.writeFileSync(this.file, JSON.stringify([...this.cookiesValid(false)]));
} }
}; };

View File

@ -109,11 +109,11 @@ export default class Cookie {
serialize() { serialize() {
return this.name + "=" + this.value; return this.name + "=" + this.value;
} }
hasExpired() { hasExpired(sessionEnded) {
return this.expiry && this.expiry < new Date(); return sessionEnded && this.expiry === null || this.expiry < new Date();
} }
isValidForRequest(requestURL) { isValidForRequest(requestURL) {
if(this.hasExpired()) if(this.hasExpired(false))
return false; return false;
const parsedURL = url.parse(requestURL); const parsedURL = url.parse(requestURL);
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:" if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:"