Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / basic-auth / index.js
1 /*!
2  * basic-auth
3  * Copyright(c) 2013 TJ Holowaychuk
4  * Copyright(c) 2014 Jonathan Ong
5  * Copyright(c) 2015 Douglas Christopher Wilson
6  * MIT Licensed
7  */
8
9 'use strict'
10
11 /**
12  * Module exports.
13  * @public
14  */
15
16 module.exports = auth
17
18 /**
19  * RegExp for basic auth credentials
20  *
21  * credentials = auth-scheme 1*SP token68
22  * auth-scheme = "Basic" ; case insensitive
23  * token68     = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
24  * @private
25  */
26
27 var credentialsRegExp = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9\-\._~\+\/]+=*) *$/
28
29 /**
30  * RegExp for basic auth user/pass
31  *
32  * user-pass   = userid ":" password
33  * userid      = *<TEXT excluding ":">
34  * password    = *TEXT
35  * @private
36  */
37
38 var userPassRegExp = /^([^:]*):(.*)$/
39
40 /**
41  * Parse the Authorization header field of a request.
42  *
43  * @param {object} req
44  * @return {object} with .name and .pass
45  * @public
46  */
47
48 function auth(req) {
49   if (!req) {
50     throw new TypeError('argument req is required')
51   }
52
53   if (typeof req !== 'object') {
54     throw new TypeError('argument req is required to be an object')
55   }
56
57   // get header
58   var header = getAuthorization(req.req || req)
59
60   // parse header
61   var match = credentialsRegExp.exec(header || '')
62
63   if (!match) {
64     return
65   }
66
67   // decode user pass
68   var userPass = userPassRegExp.exec(decodeBase64(match[1]))
69
70   if (!userPass) {
71     return
72   }
73
74   // return credentials object
75   return new Credentials(userPass[1], userPass[2])
76 }
77
78 /**
79  * Decode base64 string.
80  * @private
81  */
82
83 function decodeBase64(str) {
84   return new Buffer(str, 'base64').toString()
85 }
86
87 /**
88  * Get the Authorization header from request object.
89  * @private
90  */
91
92 function getAuthorization(req) {
93   if (!req.headers || typeof req.headers !== 'object') {
94     throw new TypeError('argument req is required to have headers property')
95   }
96
97   return req.headers.authorization
98 }
99
100 /**
101  * Object to represent user credentials.
102  * @private
103  */
104
105 function Credentials(name, pass) {
106   this.name = name
107   this.pass = pass
108 }