3ef1ff17133c194308be00fe203c779d8bd0ea69
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / morgan / node_modules / basic-auth / index.js
1
2 /**
3  * Parse the Authorization header field of `req`.
4  *
5  * @param {Request} req
6  * @return {Object} with .name and .pass
7  * @api public
8  */
9
10 module.exports = function(req){
11   req = req.req || req;
12
13   var auth = req.headers.authorization;
14   if (!auth) return;
15
16   // malformed
17   var parts = auth.split(' ');
18   if ('basic' != parts[0].toLowerCase()) return;
19   if (!parts[1]) return;
20   auth = parts[1];
21
22   // credentials
23   auth = new Buffer(auth, 'base64').toString();
24   auth = auth.match(/^([^:]*):(.*)$/);
25   if (!auth) return;
26
27   return { name: auth[1], pass: auth[2] };
28 };