Tag browsers like farmers tag livestock
eartag lets you tag browsers like farmers tag livestock. It attaches an unforgeable cookie to the browser, allowing you to identify subsequent visits. Think of it as a WeakMap for browsers: instead of using cookies to store authentication and other client state, you store an ID unique to that browser, from which state can be retrieved externally.
var http = require"http"var eartag = require"eartag"var tag = eartag"YOUR-SECRET-KEY"httpcreateServer// If an eartag cookie exists, the existing ID is returned.// Otherwise, a new ID is generated and returned.var id = tagreq res// Once you have an ID, use it with your backend to retrieve// client-specific information such as the user or locale.dbgetClientDataidreswriteHead200 "Content-Type": "text/plain"resend"The current user is: " + clientDatauserfullnamelisten8000
npm install eartag
Returns a tagging function.
secret a required string used as the key for signing and encryption.
options is an optional object which can contain the following keys:
hmacAlgorithm: The algorithm used to sign the browser's ID (
cipherAlgorithm: The algorithm used to encrypt the signed ID (
cookieName: The name of the cookie used for persistence (
Returns the ID of the client, as parsed, decrypted, and verified from the incoming cookie. If no cookie exists, or the cookie is invalid, a new ID will be generated, returned, and added to the outgoing headers.
This ID (16 random bytes, encoded in hexadecimal) will also be assigned to the
eartag property of the request. Since this function is synchronous, it can be also easily integrated with multiple vanilla HTTP server listeners as follows:
var http = require"http"var eartag = require"eartag""YOUR-SECRET-KEY"var server = httpcreateServerserveron"request" eartagserveron"request"reswriteHead200 "Content-Type": "text/plain"resend"Your ID is: " + reqeartagserverlisten8000
var express = require"express"var eartag = require"eartag""YOUR-SECRET-KEY"var app = expressappuseeartagappget"/id"ressend"Your ID is: " + reqeartagapplisten8000
Note that if cookies are disabled by the client, a new ID will be generated for every visit, preventing identification across requests.
(The MIT License)
Copyright (c) 2013 Jed Schmidt <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.