All Projects → pillarjs → Cookies

pillarjs / Cookies

Licence: mit
Signed and unsigned cookies based on Keygrip

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cookies

Immortaldb
🔩 A relentless key-value store for the browser.
Stars: ✭ 2,962 (+171.74%)
Mutual labels:  cookies
Cookie Universal
Universal cookie plugin, perfect for SSR
Stars: ✭ 376 (-65.5%)
Mutual labels:  cookies
Gdpr rails
Rails Engine for the GDPR compliance
Stars: ✭ 580 (-46.79%)
Mutual labels:  cookies
Javascript For Everyone
A step by step guide to learn JavaScript and programming
Stars: ✭ 285 (-73.85%)
Mutual labels:  cookies
Ngx Cookie Service
Angular (4.2+ ...11) service for cookies. Originally based on the `ng2-cookies` library.
Stars: ✭ 363 (-66.7%)
Mutual labels:  cookies
React Native Login
📱 An example React Native project for client login authentication
Stars: ✭ 438 (-59.82%)
Mutual labels:  cookies
Cookieconsent
A free solution to the EU, GDPR, and California Cookie Laws
Stars: ✭ 3,017 (+176.79%)
Mutual labels:  cookies
Cookie Autodelete
Firefox and Chrome WebExtension that deletes cookies and other browsing site data as soon as the tab closes, domain changes, browser restarts, or a combination of those events.
Stars: ✭ 1,015 (-6.88%)
Mutual labels:  cookies
Chromepass
Chromepass - Hacking Chrome Saved Passwords
Stars: ✭ 364 (-66.61%)
Mutual labels:  cookies
Next Iron Session
🛠 Next.js stateless session utility using signed and encrypted cookies to store data
Stars: ✭ 509 (-53.3%)
Mutual labels:  cookies
Samesite Examples
Examples of using the SameSite cookie attribute in a variety of language, libraries, and frameworks.
Stars: ✭ 289 (-73.49%)
Mutual labels:  cookies
Vue Cookies
A simple Vue.js plugin for handling browser cookies
Stars: ✭ 293 (-73.12%)
Mutual labels:  cookies
Polish Ads Filter
CertyficateIT - Oficjalne polskie filtry do Adblock, uBlock Origin, Adguard
Stars: ✭ 462 (-57.61%)
Mutual labels:  cookies
Adamantium Thief
🔑 Decrypt chromium based browsers passwords, cookies, credit cards, history, bookmarks, autofill. Version > 80 is supported.
Stars: ✭ 283 (-74.04%)
Mutual labels:  cookies
React Native Cookies
Cookie manager for React Native
Stars: ✭ 784 (-28.07%)
Mutual labels:  cookies
Auth Boss
🔒 Become an Auth Boss. Learn about different authentication methodologies on the web.
Stars: ✭ 2,879 (+164.13%)
Mutual labels:  cookies
Cookie crimes
Read local Chrome cookies without root or decrypting
Stars: ✭ 434 (-60.18%)
Mutual labels:  cookies
Dragon
⚡A powerful HTTP router and URL matcher for building Deno web servers.
Stars: ✭ 56 (-94.86%)
Mutual labels:  cookies
Hzdtf.foundation.framework
基础框架系统,支持.NET和.NET Core平台,语言:C#,DB支持MySql和SqlServer,主要功能有抽象持久化、服务层,将业务基本的增删改查抽离复用;提供代码生成器从DB生成实体、持久化、服务以及MVC控制器,每层依赖接口,并需要在客户端将对应实现层用Autofac程序集依赖注入,用AOP提供日志跟踪、事务、模型验证等。对Autofac、Redis、RabbitMQ封装扩展;DB访问提供自动主从访问,Redis客户端分区。特别适合管理系统。
Stars: ✭ 22 (-97.98%)
Mutual labels:  cookies
Pycookiecheat
Borrow cookies from your browser's authenticated session for use in Python scripts.
Stars: ✭ 465 (-57.34%)
Mutual labels:  cookies

Cookies

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Cookies is a node.js module for getting and setting HTTP(S) cookies. Cookies can be signed to prevent tampering, using Keygrip. It can be used with the built-in node.js HTTP library, or as Connect/Express middleware.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install cookies

Features

  • Lazy: Since cookie verification against multiple keys could be expensive, cookies are only verified lazily when accessed, not eagerly on each request.

  • Secure: All cookies are httponly by default, and cookies sent over SSL are secure by default. An error will be thrown if you try to send secure cookies over an insecure socket.

  • Unobtrusive: Signed cookies are stored the same way as unsigned cookies, instead of in an obfuscated signing format. An additional signature cookie is stored for each signed cookie, using a standard naming convention (cookie-name.sig). This allows other libraries to access the original cookies without having to know the signing mechanism.

  • Agnostic: This library is optimized for use with Keygrip, but does not require it; you can implement your own signing scheme instead if you like and use this library only to read/write cookies. Factoring the signing into a separate library encourages code reuse and allows you to use the same signing library for other areas where signing is needed, such as in URLs.

API

cookies = new Cookies( request, response, [ options ] )

This creates a cookie jar corresponding to the current request and response, additionally passing an object options.

A Keygrip object or an array of keys can optionally be passed as options.keys to enable cryptographic signing based on SHA1 HMAC, using rotated credentials.

A Boolean can optionally be passed as options.secure to explicitally specify if the connection is secure, rather than this module examining request.

Note that since this only saves parameters without any other processing, it is very lightweight. Cookies are only parsed on demand when they are accessed.

express.createServer( Cookies.express( keys ) )

This adds cookie support as a Connect middleware layer for use in Express apps, allowing inbound cookies to be read using req.cookies.get and outbound cookies to be set using res.cookies.set.

cookies.get( name, [ options ] )

This extracts the cookie with the given name from the Cookie header in the request. If such a cookie exists, its value is returned. Otherwise, nothing is returned.

{ signed: true } can optionally be passed as the second parameter options. In this case, a signature cookie (a cookie of same name ending with the .sig suffix appended) is fetched. If no such cookie exists, nothing is returned.

If the signature cookie does exist, the provided Keygrip object is used to check whether the hash of cookie-name=cookie-value matches that of any registered key:

  • If the signature cookie hash matches the first key, the original cookie value is returned.
  • If the signature cookie hash matches any other key, the original cookie value is returned AND an outbound header is set to update the signature cookie's value to the hash of the first key. This enables automatic freshening of signature cookies that have become stale due to key rotation.
  • If the signature cookie hash does not match any key, nothing is returned, and an outbound header with an expired date is used to delete the cookie.

cookies.set( name, [ value ], [ options ] )

This sets the given cookie in the response and returns the current context to allow chaining.

If the value is omitted, an outbound header with an expired date is used to delete the cookie.

If the options object is provided, it will be used to generate the outbound cookie header as follows:

  • maxAge: a number representing the milliseconds from Date.now() for expiry
  • expires: a Date object indicating the cookie's expiration date (expires at the end of session by default).
  • path: a string indicating the path of the cookie (/ by default).
  • domain: a string indicating the domain of the cookie (no default).
  • secure: a boolean indicating whether the cookie is only to be sent over HTTPS (false by default for HTTP, true by default for HTTPS). Read more about this option below.
  • httpOnly: a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true by default).
  • sameSite: a boolean or string indicating whether the cookie is a "same site" cookie (false by default). This can be set to 'strict', 'lax', 'none', or true (which maps to 'strict').
  • signed: a boolean indicating whether the cookie is to be signed (false by default). If this is true, another cookie of the same name with the .sig suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of cookie-name=cookie-value against the first Keygrip key. This signature key is used to detect tampering the next time a cookie is received.
  • overwrite: a boolean indicating whether to overwrite previously set cookies of the same name (false by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.

Secure cookies

To send a secure cookie, you set a cookie with the secure: true option.

HTTPS is necessary for secure cookies. When cookies.set is called with secure: true and a secure connection is not detected, the cookie will not be set and an error will be thrown.

This module will test each request to see if it's secure by checking:

  • if the protocol property of the request is set to https, or
  • if the connection.encrypted property of the request is set to true.

If your server is running behind a proxy and you are using secure: true, you need to configure your server to read the request headers added by your proxy to determine whether the request is using a secure connection.

For more information about working behind proxies, consult the framework you are using:

If your Koa or Express server is properly configured, the protocol property of the request will be set to match the protocol reported by the proxy in the X-Forwarded-Proto header.

Example

var http = require('http')
var Cookies = require('cookies')

// Optionally define keys to sign cookie values
// to prevent client tampering
var keys = ['keyboard cat']

var server = http.createServer(function (req, res) {
  // Create a cookies object
  var cookies = new Cookies(req, res, { keys: keys })

  // Get a cookie
  var lastVisit = cookies.get('LastVisit', { signed: true })

  // Set the cookie to a value
  cookies.set('LastVisit', new Date().toISOString(), { signed: true })

  if (!lastVisit) {
    res.setHeader('Content-Type', 'text/plain')
    res.end('Welcome, first time visitor!')
  } else {
    res.setHeader('Content-Type', 'text/plain')
    res.end('Welcome back! Nothing much changed since your last visit at ' + lastVisit + '.')
  }
})

server.listen(3000, function () {
  console.log('Visit us at http://127.0.0.1:3000/ !')
})

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].