Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / csrf / index.js
1 /*!
2  * csrf
3  * Copyright(c) 2014 Jonathan Ong
4  * Copyright(c) 2015 Douglas Christopher Wilson
5  * MIT Licensed
6  */
7
8 'use strict'
9
10 /**
11  * Module dependencies.
12  * @private
13  */
14
15 var rndm = require('rndm')
16 var uid = require('uid-safe')
17 var compare = require('tsscmp')
18 var crypto = require('crypto')
19 var escape = require('base64-url').escape
20
21 /**
22  * Module exports.
23  * @public
24  */
25
26 module.exports = Tokens
27
28 /**
29  * Token generation/verification class.
30  *
31  * @param {object} [options]
32  * @param {number} [options.saltLength=8] The string length of the salt
33  * @param {number} [options.secretLength=18] The byte length of the secret key
34  * @public
35  */
36
37 function Tokens (options) {
38   if (!(this instanceof Tokens)) {
39     return new Tokens(options)
40   }
41
42   var opts = options || {}
43
44   var saltLength = opts.saltLength !== undefined
45     ? opts.saltLength
46     : 8
47
48   if (typeof saltLength !== 'number' || !isFinite(saltLength) || saltLength < 1) {
49     throw new TypeError('option saltLength must be finite number > 1')
50   }
51
52   var secretLength = opts.secretLength !== undefined
53     ? opts.secretLength
54     : 18
55
56   if (typeof secretLength !== 'number' || !isFinite(secretLength) || secretLength < 1) {
57     throw new TypeError('option secretLength must be finite number > 1')
58   }
59
60   this.saltLength = saltLength
61   this.secretLength = secretLength
62 }
63
64 /**
65  * Create a new CSRF token.
66  *
67  * @param {string} secret The secret for the token.
68  * @public
69  */
70
71 Tokens.prototype.create = function create (secret) {
72   if (!secret || typeof secret !== 'string') {
73     throw new TypeError('argument secret is required')
74   }
75
76   return this._tokenize(secret, rndm(this.saltLength))
77 }
78
79 /**
80  * Create a new secret key.
81  *
82  * @param {function} [callback]
83  * @public
84  */
85
86 Tokens.prototype.secret = function secret (callback) {
87   return uid(this.secretLength, callback)
88 }
89
90 /**
91  * Create a new secret key synchronously.
92  * @public
93  */
94
95 Tokens.prototype.secretSync = function secretSync () {
96   return uid.sync(this.secretLength)
97 }
98
99 /**
100  * Tokenize a secret and salt.
101  * @private
102  */
103
104 Tokens.prototype._tokenize = function tokenize (secret, salt) {
105   var hash = crypto
106     .createHash('sha1')
107     .update(salt + '-' + secret, 'ascii')
108     .digest('base64')
109   return escape(salt + '-' + hash)
110 }
111
112 /**
113  * Verify if a given token is valid for a given secret.
114  *
115  * @param {string} secret
116  * @param {string} token
117  * @public
118  */
119
120 Tokens.prototype.verify = function verify (secret, token) {
121   if (!secret || typeof secret !== 'string') {
122     return false
123   }
124
125   if (!token || typeof token !== 'string') {
126     return false
127   }
128
129   var index = token.indexOf('-')
130
131   if (index === -1) {
132     return false
133   }
134
135   var salt = token.substr(0, index)
136   var expected = this._tokenize(secret, salt)
137
138   return compare(token, expected)
139 }