This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch feature/cookies into main (#88)
* Implement CookieManager - Implement Cookie class - Implement CookieManager class - Let CookieManager be available through Services * Fully implement Cookie framework - Fully implement Cookie class - Fully implement CookieManager - Let engine load cookies on start automatically
- Loading branch information
Showing
4 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class Cookie { | ||
private _value: string; | ||
private _expires: Date | null; | ||
private _sameSite: "Strict" | "Lax" | "None" | null; | ||
private _secure: boolean | null; | ||
private _updated: boolean; | ||
|
||
public get value(): string { | ||
return this._value; | ||
} | ||
|
||
public get expires(): Date | null { | ||
return this._expires; | ||
} | ||
|
||
public get sameSite(): "Strict" | "Lax" | "None" | null { | ||
return this._sameSite; | ||
} | ||
|
||
public get secure(): boolean | null { | ||
return this._secure; | ||
} | ||
|
||
public get updated(): boolean { | ||
return this._updated; | ||
} | ||
|
||
public set updated(value: boolean) { | ||
this._updated = value; | ||
} | ||
|
||
public get fullySpecified(): boolean { | ||
return ( | ||
this._value.length > 0 && | ||
this._expires !== null && | ||
this._sameSite !== null && | ||
this._secure !== null | ||
); | ||
} | ||
|
||
public constructor( | ||
value: string, | ||
expires: Date | null, | ||
sameSite: "Strict" | "Lax" | "None" | null, | ||
secure: boolean | null, | ||
updated: boolean = true | ||
) { | ||
this._value = value; | ||
this._expires = expires; | ||
this._sameSite = sameSite; | ||
this._secure = secure; | ||
this._updated = updated; | ||
} | ||
|
||
public update( | ||
value: string, | ||
expires: Date, | ||
sameSite: "Strict" | "Lax" | "None", | ||
secure: boolean | ||
): void { | ||
this._value = value; | ||
this._expires = expires; | ||
this._sameSite = sameSite; | ||
this._secure = secure; | ||
this._updated = true; | ||
} | ||
} | ||
|
||
export default Cookie; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Cookie from "./cookie.js"; | ||
|
||
class CookieManager { | ||
private readonly _firstDate1970UTC: string; | ||
|
||
private _cookies: Map<string, Cookie>; | ||
|
||
public get cookies(): Map<string, Cookie> { | ||
return this._cookies; | ||
} | ||
|
||
public constructor() { | ||
this._firstDate1970UTC = new Date(0).toUTCString(); | ||
this._cookies = new Map<string, Cookie>(); | ||
} | ||
|
||
public has(name: string): boolean { | ||
return this._cookies.has(name); | ||
} | ||
|
||
public get(name: string): Cookie { | ||
if (!this.has(name)) { | ||
throw new Error(`Cookie with name '${name}' does not exist.`); | ||
} | ||
|
||
return this._cookies.get(name) as Cookie; | ||
} | ||
|
||
public set(name: string, cookie: Cookie): void { | ||
this._cookies.set(name, cookie); | ||
} | ||
|
||
public getOrSet(name: string, cookie: Cookie): Cookie { | ||
if (this.has(name)) { | ||
return this.get(name); | ||
} | ||
|
||
this.set(name, cookie); | ||
return cookie; | ||
} | ||
|
||
public delete(name: string): void { | ||
this._cookies.delete(name); | ||
} | ||
|
||
public clear(): void { | ||
this._cookies.clear(); | ||
} | ||
|
||
public load(): void { | ||
let newCookies: Map<string, Cookie> = new Map<string, Cookie>(); | ||
|
||
let cookieString = document.cookie.trim(); | ||
if (cookieString) { | ||
let split = cookieString.split(";"); | ||
for (let part of split) { | ||
let [name, value] = part.split("="); | ||
|
||
if (!name) { | ||
throw new Error("Failed to load cookie: Name is empty."); | ||
} | ||
|
||
if (!value) { | ||
throw new Error(`Failed to load cookie ${name}: Value is empty.`); | ||
} | ||
let cookie: Cookie = new Cookie( | ||
decodeURI(value), | ||
null, | ||
null, | ||
null, | ||
false | ||
); | ||
newCookies.set(name.trim(), cookie); | ||
} | ||
} | ||
|
||
this._cookies = newCookies; | ||
} | ||
|
||
public cleanBrowserCookies(): void { | ||
let cookieString = document.cookie.trim(); | ||
|
||
if (cookieString) { | ||
let split = document.cookie.split(";"); | ||
for (let part of split) { | ||
let name = part.split("=")[0]; | ||
if (!this.has(name)) { | ||
document.cookie = `${name.trim()};Expires=${ | ||
this._firstDate1970UTC | ||
};SameSite=Lax`; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public save(): void { | ||
this.cleanBrowserCookies(); | ||
|
||
for (let [name, cookie] of this._cookies) { | ||
if (cookie.updated) { | ||
if (!cookie.fullySpecified) { | ||
console.warn( | ||
`Cookie ${name} was updated but not fully specified. Can't save updated value to disk!` | ||
); | ||
continue; | ||
} | ||
|
||
let expires: string = cookie.expires!.toUTCString(); | ||
let sameSite: string = cookie.sameSite!; | ||
let secure: string = cookie.secure! ? "true" : "false"; | ||
|
||
document.cookie = `${name}=${encodeURI( | ||
cookie.value | ||
)};Expires=${expires};SameSite=${sameSite};Secure=${secure}`; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default CookieManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters