xprezzo-basic-auth
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -0,1 +1,6 @@ | ||
1.0.2 / 2029-09-27 | ||
================== | ||
* instroduce Credentials class | ||
1.0.1 / 2029-09-25 | ||
@@ -2,0 +7,0 @@ ================== |
133
index.js
@@ -21,114 +21,53 @@ /*! | ||
module.exports = auth | ||
module.exports.parse = parse | ||
module.exports.buffer = require("xprezzo-buffer") | ||
module.exports.mixim = require("xprezzo-buffer").mixim | ||
/** | ||
* RegExp for basic auth credentials | ||
* | ||
* credentials = auth-scheme 1*SP token68 | ||
* auth-scheme = "Basic" ; case insensitive | ||
* token68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"=" | ||
* @private | ||
*/ | ||
var CREDENTIALS_REGEXP = /^\s*(?:[Bb][Aa][Ss][Ii][Cc])\s+([A-Za-z0-9._~+/-]+=*)\s*$/ | ||
/** | ||
* RegExp for basic auth user/pass | ||
* | ||
* user-pass = userid ":" password | ||
* userid = *<TEXT excluding ":"> | ||
* password = *TEXT | ||
* @private | ||
*/ | ||
var USER_PASS_REGEXP = /^([^:]*):(.*)$/ | ||
/** | ||
* Parse the Authorization header field of a request. | ||
* | ||
* @param {object} req | ||
* @return {object} with .name and .pass | ||
* @public | ||
*/ | ||
function auth (req) { | ||
module.exports = function(req) { | ||
if (!req) { | ||
throw new TypeError('argument req is required') | ||
} | ||
if (typeof req !== 'object') { | ||
} else if (typeof req !== 'object') { | ||
throw new TypeError('argument req is required to be an object') | ||
} | ||
return (new Credentials(req)).verify(); | ||
}; | ||
module.exports.parse = function(opts) { | ||
return (new Credentials(opts)).verify(); | ||
}; | ||
module.exports.Credentials = Credentials; | ||
module.exports.buffer = require("xprezzo-buffer") | ||
module.exports.mixim = require("xprezzo-buffer").mixim | ||
// get header | ||
var header = getAuthorization(req) | ||
// parse header | ||
return parse(header) | ||
} | ||
/** | ||
* Decode base64 string. | ||
* Class to represent user credentials. | ||
* @private | ||
*/ | ||
function decodeBase64 (str) { | ||
return Buffer.from(str, 'base64').toString() | ||
function Credentials (opts) { | ||
this.name = this.pass = ""; | ||
this.verified = false; | ||
if (typeof opts === 'object'){ | ||
if (!opts.headers || typeof opts.headers !== 'object') { | ||
throw new TypeError('argument req is required to have headers property') | ||
} | ||
var header=opts.headers.authorization | ||
this.parseHeader(header); | ||
} else if (typeof opts === 'string'){ | ||
this.parseHeader(opts); | ||
} | ||
} | ||
/** | ||
* Get the Authorization header from request object. | ||
* @private | ||
*/ | ||
function getAuthorization (req) { | ||
if (!req.headers || typeof req.headers !== 'object') { | ||
throw new TypeError('argument req is required to have headers property') | ||
} | ||
return req.headers.authorization | ||
Credentials.prototype.verify = function(){ | ||
return (this.verified)? this: undefined; | ||
} | ||
/** | ||
* Parse basic auth to object. | ||
* | ||
* @param {string} string | ||
* @return {object} | ||
* @public | ||
*/ | ||
function parse (string) { | ||
if (typeof string !== 'string') { | ||
return undefined | ||
} | ||
Credentials.prototype.parseHeader = function(opts){ | ||
// parse header | ||
var match = CREDENTIALS_REGEXP.exec(string) | ||
if (!match) { | ||
return undefined | ||
var match, userPass; | ||
if (typeof opts !== 'string' || | ||
!(match=/^\s*(?:basic)\s+([A-Z\d\._~\+/-]+=*)\s*$/i.exec(opts)) || | ||
!(userPass = /^([^:]*):(.*)$/.exec(Buffer.from(match[1], 'base64').toString())) | ||
) { | ||
return this; | ||
} | ||
// decode user pass | ||
var userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1])) | ||
if (!userPass) { | ||
return undefined | ||
} | ||
// return credentials object | ||
return new Credentials(userPass[1], userPass[2]) | ||
this.name = userPass[1]; | ||
this.pass = userPass[2]; | ||
this.verified = true; | ||
return this; | ||
} | ||
/** | ||
* Object to represent user credentials. | ||
* @private | ||
*/ | ||
function Credentials (name, pass) { | ||
this.name = name | ||
this.pass = pass | ||
} |
{ | ||
"name": "xprezzo-basic-auth", | ||
"description": "xprezzo basic auth parser", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -26,3 +26,3 @@ # xprezzo-basic-auth | ||
``` | ||
$ npm install basic-auth | ||
$ npm install xprezzo-basic-auth | ||
``` | ||
@@ -113,2 +113,3 @@ | ||
* exposed [xprezzo-mixin](https://github.com/xprezzo/xprezzo-mixin) | ||
* exposed class Credntials | ||
@@ -115,0 +116,0 @@ # License |
Sorry, the diff of this file is not supported yet
117
6916
63