0.0.10 • Published 2 years ago

@vs-org/cookie v0.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

vs-cookie

This is simple cookie helper for handling cookie operations.

Usage

  1. Create cookie

    Note using this function depends on browser, if combination is used that is not validated but also not accepted by browser then cookie will not be saved. Make sure of options before using this function.

const { createCookie } = require("@vs-org/cookie");

const cookie = createCookie({
    name: "test",
    value: "test"
  });

console.log(cookie); // test=test;Priority=Medium
  1. Get unsigned cookie value
const { getCookie } = require("@vs-org/cookie");

const cookie = getCookie("test1=test1; test=test", "test");

console.log(cookie); // "test"
  1. Get signed cookie value
const { getCookie } = require("@vs-org/cookie");

const cookie = getCookie("test1=test1; test=test.hWtzMM7E4KTirRm3N8GZ4DB5E1b9j4DVtMYh4zkwvQ", "test", {secret: "This is cookie signing secret"});

console.log(cookie); // "test%3AhWtzMM7E4KTirRm3N8GZ4DB5E1b9j4DVtMYh4zkwvQ"
  1. Parse all cookies
const { parse } = require("@vs-org/cookie");

const parsedCookies = parse("test1=test1; test=test%3AhWtzMM7E4KTirRm3N8GZ4DB5E1b9j4DVtMYh4zkwvQ");

console.log(parsedCookies); // { test1: 'test1', test: 'test%3AhWtzMM7E4KTirRm3N8GZ4DB5E1b9j4DVtMYh4zkwvQ' }
  1. sign cookie
const { sign } = require("@vs-org/cookie");

const signedCookie = sign("cookieValue","This is cookie signing secret");

console.log(signedCookie); // cookieValue%3A2V92ZahIZBNWU5aJSVZBeFNSMfNTqOl2crexQyKo
  1. sign cookie but use different separtor

    a) Package uses : as default separator for cookie and cookie signature. b) If application has cookies containing : then separator can be passed in option to use it to sign cookie. Make sure same separator is used while verifying

const { sign } = require("@vs-org/cookie");

const signedCookie = sign("Cookie :test value", "This is cookie signing secret", { separator: "-" });

console.log(signedCookie); // Cookie%20%3Atest%20value-3KV68YGLG0GrgscHSlFoRyDgvzxaN3o0gT3oBTr7EM
  1. verify cookie signature
const { verify } = require("@vs-org/cookie");

const isValidCookie1 = verify("cookieValue%3A2V92ZahIZBNWU5aJSVZBeFNSMfNTqOl2crexQyKo","This is cookie signing secret");

console.log(isValidCookie1); // true


const isValidCookie2 = verify("cookieValue%3Aabcdefg","This is cookie signing secret");

console.log(isValidCookie2); // false
  1. verify cookie signature with different separator

    a) Package uses : as default separator for cookie and cookie signature. b) If application has signed cookies with different separator then separator can be passed in option which will be used for verifying signature.

const { verify } = require("@vs-org/cookie");

const isValidCookie1 = verify("Cookie%20%3Atest%20value-3KV68YGLG0GrgscHSlFoRyDgvzxaN3o0gT3oBTr7EM","This is cookie signing secret", { separator: "-" });

console.log(isValidCookie1); // true

Options

optiontype / accepted valuesDescription
namestringCookie name, should not contain ( ) < > @ , ; : " /[ ]?={} or spaces
valuestringCookie value
encodefunctionBy default value will be encoded with encodeURIComponent but if custom encoding is required this option can accept encoding function. Note encoding function should always return string or else there will unexpected behaviours
PathstringCookie path, by default current path will be assigned by browser
DomainstringCookie domain, by default current domain will be assigned by browser
HttpOnlybooleanThis attribute indicates, if cookie will accessible to javascript or not
Max-AgenumberCookie expiry in seconds
Prefix__Secure- , __Host-Cookie prefix can be used only when secure is set as true and for HTTPs origins. __Secure- (this prefix cookies must be set with secure flag and from HTTPs origin), __Host- (can have only path as / and cannot have domain. Package will throw error if domain and path is provided along side this option)
PriorityHight, Mediym, LowPrioriy can be set in Chrome browser only as of today. It helps browser decides cookie priority in order to strip cookies in case of limit exceeds
Securebooleanonly send cookies with HTTPS and not HTTP
SameSitetrue, Strict, Lax, NoneCookies used for storing sensetive information like authentication / authenticated session should have short lifetime with SameSite as "Strict" or "Lax"

VS Cookie function signatures

NameFunction signature
createCookie(cookieOption: VsCookieOption) => string \| never
getCookie(cookies: string, cookieName: string, options?: {decode?: Function; secret?: string; separator?: string;}) => string \| never
parse(cookies: string, decode: Function = decodeURIComponent) => object \| never
sign(cookie: string, secret: string, options: { separator?: string; encode?: Function }) => string \| never
verify(cookie: string, secret: string, options: { separator?: string; decode?: Function }) => boolean \| never

Note

This package is experimental and not production ready. Should only be used for developement or POC. Also this package is not actively maintained.

License

MIT (see LICENSE)

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago