How to use the cookie-signature.sign function in cookie-signature

To help you get started, we’ve selected a few cookie-signature examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ymichael / puton / node_modules / express / node_modules / connect / lib / middleware / session.js View on Github external
, tls = req.connection.encrypted || (trustProxy && 'https' == proto)
        , secured = cookie.secure && tls
        , isNew = unsignedCookie != req.sessionID;

      // only send secure cookies via https
      if (cookie.secure && !secured) return debug('not secured');

      // browser-session length cookie
      if (null == cookie.expires) {
        if (!isNew) return debug('already set browser-session cookie');
      // compare hashes and ids
      } else if (originalHash == hash(req.session) && originalId == req.session.id) {
        return debug('unmodified session');
      }

      var val = 's:' + signature.sign(req.sessionID, secret);
      val = cookie.serialize(key, val);
      debug('set-cookie %s', val);
      res.setHeader('Set-Cookie', val);
    });
github ifgyong / demo / React-native / Helloword / node_modules / csurf / index.js View on Github external
function setsecret(req, res, sessionKey, val, cookie) {
    if (cookie) {
        // set secret on cookie
        if (cookie.signed) {
            var secret = req.secret

            if (!secret) {
                throw new Error('cookieParser("secret") required for signed cookies')
            }

            val = 's:' + sign(val, secret)
        }

        setcookie(res, cookie.key, val, cookie);
    } else if (req[sessionKey]) {
        // set secret on session
        req[sessionKey].csrfSecret = val
    } else {
        /* istanbul ignore next: should never actually run */
        throw new Error('misconfigured csrf')
    }
}
github entermedia-community / entermedia-server / webapp / examples / annotations / workspace / node_modules / express / lib / response.js View on Github external
res.cookie = function(name, val, options){
  options = mixin({}, options);
  var secret = this.req.secret;
  var signed = options.signed;
  if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');
  if ('number' == typeof val) val = val.toString();
  if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
  if (signed) val = 's:' + sign(val, secret);
  if ('maxAge' in options) {
    options.expires = new Date(Date.now() + options.maxAge);
    options.maxAge /= 1000;
  }
  if (null == options.path) options.path = '/';
  var headerVal = cookie.serialize(name, String(val), options);

  // supports multiple 'res.cookie' calls by getting previous value
  var prev = this.get('Set-Cookie');
  if (prev) {
    if (Array.isArray(prev)) {
      headerVal = prev.concat(headerVal);
    } else {
      headerVal = [prev, headerVal];
    }
  }
github andreirtaylor / BoardGameTracking / node_modules / express / lib / response.js View on Github external
res.cookie = function(name, val, options){
  options = merge({}, options);
  var secret = this.req.secret;
  var signed = options.signed;
  if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');
  if ('number' == typeof val) val = val.toString();
  if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
  if (signed) val = 's:' + sign(val, secret);
  if ('maxAge' in options) {
    options.expires = new Date(Date.now() + options.maxAge);
    options.maxAge /= 1000;
  }
  if (null == options.path) options.path = '/';
  var headerVal = cookie.serialize(name, String(val), options);

  // supports multiple 'res.cookie' calls by getting previous value
  var prev = this.get('Set-Cookie');
  if (prev) {
    if (Array.isArray(prev)) {
      headerVal = prev.concat(headerVal);
    } else {
      headerVal = [prev, headerVal];
    }
  }
github bubkoo / grunt-restful-mock / tasks / lib / router.js View on Github external
options = merge({}, options);
  var secret = req.secret;
  var signed = options.signed;

  if (signed && !secret) {
    throw new Error('cookieParser("secret") required for signed cookies');
  }

  if ('number' === typeof val) {
    val = val.toString();
  }
  if ('object' === typeof val) {
    val = 'j:' + JSON.stringify(val);
  }
  if (signed) {
    val = 's:' + sign(val, secret);
  }
  if ('maxAge' in options) {
    options.expires = new Date(Date.now() + options.maxAge);
    options.maxAge /= 1000;
  }
  if (null == options.path) {
    options.path = '/';
  }


  var headerVal = cookie.serialize(name, String(val), options);

  // supports multiple 'res.cookie' calls by getting previous value
  var prev = res.getHeader('Set-Cookie');
  if (prev) {
    if (Array.isArray(prev)) {
github cdapio / cdap / server / sandbox / node_modules / connect / lib / middleware / session.js View on Github external
, tls = req.connection.encrypted || (trustProxy && 'https' == proto)
        , secured = cookie.secure && tls
        , isNew = unsignedCookie != req.sessionID;

      // only send secure cookies via https
      if (cookie.secure && !secured) return debug('not secured');

      // browser-session length cookie
      if (null == cookie.expires) {
        if (!isNew) return debug('already set browser-session cookie');
      // compare hashes and ids
      } else if (originalHash == hash(req.session) && originalId == req.session.id) {
        return debug('unmodified session');
      }

      var val = 's:' + signature.sign(req.sessionID, secret);
      val = cookie.serialize(key, val);
      debug('set-cookie %s', val);
      res.setHeader('Set-Cookie', val);
    });
github bfrgoncalves / Online-PhyloViZ / node_modules / express / lib / response.js View on Github external
res.cookie = function (name, value, options) {
  var opts = merge({}, options);
  var secret = this.req.secret;
  var signed = opts.signed;

  if (signed && !secret) {
    throw new Error('cookieParser("secret") required for signed cookies');
  }

  var val = typeof value === 'object'
    ? 'j:' + JSON.stringify(value)
    : String(value);

  if (signed) {
    val = 's:' + sign(val, secret);
  }

  if ('maxAge' in opts) {
    opts.expires = new Date(Date.now() + opts.maxAge);
    opts.maxAge /= 1000;
  }

  if (opts.path == null) {
    opts.path = '/';
  }

  this.append('Set-Cookie', cookie.serialize(name, String(val), opts));

  return this;
};
github howarddierking / RestBugs / RestBugs-Node / node_modules / express / lib / response.js View on Github external
res.cookie = function(name, val, options){
  options = utils.merge({}, options);
  var secret = this.req.secret;
  var signed = options.signed;
  if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');
  if ('number' == typeof val) val = val.toString();
  if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
  if (signed) val = 's:' + sign(val, secret);
  if ('maxAge' in options) {
    options.expires = new Date(Date.now() + options.maxAge);
    options.maxAge /= 1000;
  }
  if (null == options.path) options.path = '/';
  this.set('Set-Cookie', cookie.serialize(name, String(val), options));
  return this;
};
github adonisjs / adonis-framework / packages / cookie / src / Cookie / index.ts View on Github external
export function pack (value: any, secretKey?: string): null | string {
  if (value === undefined || value === null) {
    return null
  }

  if (value instanceof Date) {
    value = value.toJSON()
  } else if (typeof (value) !== 'string') {
    value = `j:${JSON.stringify(value)}`
  }

  /**
   * If secret is defined, then sign the cookie
   */
  if (secretKey) {
    return `s:${cookieSignature.sign(value, secretKey)}`
  }

  return value
}
github jawerty / brocast / signaling_server / node_modules / express / lib / response.js View on Github external
res.cookie = function(name, val, options){
  options = options || {};
  var secret = this.req.secret;
  var signed = options.signed;
  if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');
  if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
  if (signed) val = 's:' + sign(val, secret);
  if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);
  if (null == options.path) options.path = '/';
  this.set('Set-Cookie', cookie.serialize(name, String(val), options));
  return this;
};

cookie-signature

Sign and unsign cookies

MIT
Latest version published 1 year ago

Package Health Score

71 / 100
Full package analysis

Popular cookie-signature functions