Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Authentication helper option added for digest authentication.

  • Loading branch information...
commit ed38d7c52b882dd5c78eadad731cd6a8bdd8ffd6 1 parent 219a780
Gevorg Harutyunyan authored
4 LICENSE
View
@@ -1,9 +1,9 @@
(The MIT License)
-Copyright (c) 2012 Gevorg Harutyunyan <gevorg.ha@gmail.com>
+Copyright (c) 2013 Gevorg Harutyunyan <gevorg.ha@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3  README.md
View
@@ -107,6 +107,7 @@ http.createServer(function(req, res) {
## Configurations
- `authRealm` - Authentication realm.
+ - `authHelper` - Function that allows to override standard authentication method by providing custom user loading mechanism. Works only with digest authentication.
- `authFile` - File where user details are stored in format **{user:pass}** or **{user:passHash}** for basic access and **{user:realm:passHash}** for digest access.
- `authList` - List where user details are stored in format **{user:pass}** or **{user:passHash}** for basic access and **{user:realm:passHash}** for digest access, ignored if `authFile` is specified.
- `authType` - Type of authentication, may be **basic** or **digest**, optional, default is **basic**.
@@ -144,7 +145,7 @@ You can find list of issues using **[this link](http://github.com/gevorg/http-au
(The MIT License)
-Copyright (c) 2012 Gevorg Harutyunyan <gevorg.ha@gmail.com>
+Copyright (c) 2013 Gevorg Harutyunyan <gevorg.ha@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the **Software**), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
BIN  examples/.example_helper.js.swp
View
Binary file not shown
38 examples/example_helper.js
View
@@ -0,0 +1,38 @@
+/**
+ * HTTP authentication module.
+ */
+var auth = require('../lib/http-auth');
+
+/**
+ * HTTP module.
+ */
+var http = require('http');
+
+/**
+ * Requesting new authentication instance.
+ */
+var digest = auth({
+ authRealm : "Private area.",
+ // username is mia, password is supergirl.
+ authHelper : function(user, callback) {
+ if ( user === 'mia' ) {
+ callback('mia:Private area.:3a556dc7260e8e7f032d247fb668b06b');
+ } else {
+ callback();
+ }
+ },
+ authType : 'digest'
+});
+
+/**
+ * Creating new HTTP server.
+ */
+http.createServer(function(req, res) {
+ // Apply authentication to server.
+ digest.apply(req, res, function(username) {
+ res.end("Welcome to private area - " + username + "!");
+ });
+}).listen(1337);
+
+// Log url.
+console.log("Server running at http://127.0.0.1:1337/");
BIN  lib/auth/.digest.js.swp
View
Binary file not shown
37 lib/auth/digest.js
View
@@ -31,8 +31,9 @@ function State(name){
* @param {String} authRealm authentication realm.
* @param {Array} authUsers array of users.
* @param {String} algorithm algorithm for authentication.
+ * @param {Function} authentication helper allows to override standard authentication.
*/
-function Digest(authRealm, authUsers, algorithm) {
+function Digest(authRealm, authUsers, algorithm, authHelper) {
// Realm.
this.realm = authRealm;
// Users.
@@ -41,6 +42,8 @@ function Digest(authRealm, authUsers, algorithm) {
this.nonces = new Array();
// Algorithm.
this.algorithm = algorithm;
+ // Authentication helper.
+ this.authHelper = authHelper;
// Used for async callback.
var self = this;
@@ -87,12 +90,22 @@ Digest.prototype.isAuthenticated = function(request, callback) {
// Calculating second hash.
var ha2 = utils.md5(request.method + ":" + request.url);
- // Checking response for username.
- var userHash = this.users[co.username];
-
- // Validates client options.
- callback(this.validateClientOptions(ha2, co, userHash));
- } else {
+ // @todo need to have some binding mechanism to clean this.
+ var validateClientOptions = this.validateClientOptions;
+ var staleOption = this.STALE;
+ var nonces = this.nonces;
+
+ if ( this.authHelper ) {
+ this.authHelper(co.username, function(userHash) {
+ callback(validateClientOptions(ha2, co, userHash, nonces, staleOption));
+ });
+ } else {
+ // Checking response for username.
+ var userHash = this.users[co.username];
+ // Validates client options.
+ callback(this.validateClientOptions(ha2, co, userHash, nonces, staleOption));
+ }
+ } else {
callback(this.STALE);
}
} else {
@@ -106,9 +119,11 @@ Digest.prototype.isAuthenticated = function(request, callback) {
* @param {String} second hash in digest authentication mechanism.
* @param {Object} object with client options.
* @param {String} user hash from server.
+ * @param {Array} array of nonces.
+ * @param {Object} identifies stale option.
* @return {String} the authenticated user ID, if authentication is successful, else undefined.
*/
-Digest.prototype.validateClientOptions = function(ha2, co, userHash) {
+Digest.prototype.validateClientOptions = function(ha2, co, userHash, nonces, staleOption) {
var authenticated = undefined;
// Username is correct.
@@ -122,16 +137,16 @@ Digest.prototype.validateClientOptions = function(ha2, co, userHash) {
// If qop is specified.
if(co.qop) {
- if(co.nc > this.nonces[co.nonce]) {
+ if(co.nc > nonces[co.nonce]) {
// Updating nonce count.
- this.nonces[co.nonce] = co.nc;
+ nonces[co.nonce] = co.nc;
// Evaluating final authentication response.
var authRes = utils.md5(ha1 + ":" + co.nonce + ":" + co.nc + ":" +
co.cnonce + ":" + co.qop + ":" + ha2);
authenticated = (authRes == co.response) ? co.username : undefined;
} else {
- authenticated = this.STALE;
+ authenticated = staleOption;
}
} else {
// Evaluating final authentication response.
4 lib/http-auth.js
View
@@ -14,6 +14,8 @@ var opt = require('./options');
* @param {Array} options options that may be used for authentication.
*
* - authRealm authentication realm.
+ * - authHelper function that allows to override standard authentication method
+ * by providing custom user loading mechanism. Only for digest.
* - authFile file where user details are stored in format {user:pass}.
* - authList list where user details are stored in format {user:pass},
* ignored if authFile is specified.
@@ -26,4 +28,4 @@ module.exports = function(options) {
var parsedOptions = opt(options);
// Requesting new authentication instance.
return provider.newInstance(parsedOptions);
-};
+};
50 lib/options.js
View
@@ -39,34 +39,36 @@ module.exports = function(options) {
throw new Error("Authentication realm is mandatory!");
}
- // Authentication users.
- var authUsers = new Array();
- var authList = options['authList'];
+ if ( !options['authHelper'] ) {
+ // Authentication users.
+ var authUsers = new Array();
+ var authList = options['authList'];
- // If authFile is provided.
- var authFile = options['authFile'];
- if(authFile) {
- authList = fs.readFileSync(authFile, 'UTF-8').toString().split('\n');
- }
+ // If authFile is provided.
+ var authFile = options['authFile'];
+ if(authFile) {
+ authList = fs.readFileSync(authFile, 'UTF-8').toString().split('\n');
+ }
- // Checking authentication list.
- if(!authList || authList.length == 0) {
- throw new Error("Authentication list cannot be empty!");
- }
+ // Checking authentication list.
+ if(!authList || authList.length == 0) {
+ throw new Error("Authentication list cannot be empty!");
+ }
- for(var i = 0; i < authList.length; ++i) {
- var authLine = authList[i];
+ for(var i = 0; i < authList.length; ++i) {
+ var authLine = authList[i];
- if(authType == 'digest') {
- var authTokens = authLine.split(":");
- authUsers[authTokens[0]] = authLine;
- } else if(authType == 'basic') {
- authUsers.push(authLine);
- }
- }
+ if(authType == 'digest') {
+ var authTokens = authLine.split(":");
+ authUsers[authTokens[0]] = authLine;
+ } else if(authType == 'basic') {
+ authUsers.push(authLine);
+ }
+ }
- // Setting authUsers.
- options['authUsers'] = authUsers;
+ // Setting authUsers.
+ options['authUsers'] = authUsers;
+ }
return options;
-};
+};
4 lib/provider.js
View
@@ -15,11 +15,11 @@ module.exports = {
*/
'newInstance' : function(options) {
if(options && options.authType == 'digest') {
- return new Digest(options.authRealm, options.authUsers, options.algorithm);
+ return new Digest(options.authRealm, options.authUsers, options.algorithm, options.authHelper);
} else if(options && options.authType == 'basic') {
return new Basic(options.authRealm, options.authUsers);
} else {
throw new Error("Invalid type, may be digest | basic!");
}
}
-};
+};
BIN  tests/auth/.test-digest.js.swp
View
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.