otto-authentication
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,5 +5,15 @@ | ||
// Modules | ||
var basic_auth = require('basic-auth'); | ||
var basic_auth = require('basic-auth'); | ||
var ErrorUnauthorized = require('otto-errors').ErrorUnauthorized; | ||
// TODO: Function that returns true/false | ||
// fn returns true/false to callback | ||
function custom (fn) { | ||
return function (req, res, next) { | ||
fn(req, function (result) { | ||
if (result === true) { return next(); } | ||
next(new ErrorUnauthorized('Authentication Failed')); | ||
}); | ||
}; | ||
} | ||
// Also allow multiple users / methods | ||
@@ -21,3 +31,3 @@ function http_basic (username, password) { | ||
res.set('WWW-Authenticate', 'Basic'); | ||
res.send(401, { error : { message : 'Authentication Failed' } }); | ||
next(new ErrorUnauthorized('Authentication Failed')); | ||
}; | ||
@@ -28,3 +38,4 @@ } | ||
module.exports = { | ||
custom : custom, | ||
http_basic : http_basic | ||
}; |
{ | ||
"name" : "otto-authentication", | ||
"version" : "0.0.1", | ||
"version" : "0.0.2", | ||
"repository" : "https://github.com/ottojs/otto-authentication.git", | ||
"main" : "./lib/index.js", | ||
"dependencies" : { | ||
"basic-auth" : "1.0.0" | ||
"otto-errors" : "0.0.2", | ||
"basic-auth" : "1.0.0" | ||
}, | ||
"devDependencies" : { | ||
"jshint" : "2.5.2", | ||
"mocha" : "1.20.1", | ||
"mocha" : "1.21.4", | ||
"should" : "4.0.4", | ||
@@ -16,3 +17,3 @@ "supertest" : "0.13.0", | ||
"istanbul" : "0.3.0", | ||
"express" : "4.6.1" | ||
"otto" : "0.0.4" | ||
}, | ||
@@ -19,0 +20,0 @@ "scripts" : { |
@@ -7,3 +7,3 @@ | ||
var supertest = require('supertest'); | ||
var express = require('express'); | ||
var otto = require('otto'); | ||
@@ -13,8 +13,8 @@ // Subject | ||
// New Express App | ||
var app = express(); | ||
// New Otto/Express App | ||
var app = otto.app(); | ||
// Public Route | ||
app.get('/public', function (req, res) { | ||
res.send(200, { public_page : true }); | ||
res.status(200).send({ public_page : true }); | ||
}); | ||
@@ -26,6 +26,22 @@ | ||
function (req, res) { | ||
res.send(200, { protected_page : true }); | ||
res.status(200).send({ protected_page : true }); | ||
} | ||
]); | ||
// Protected Route custom | ||
app.get('/custom', [ | ||
otto_authentication.custom(function (req, allow) { | ||
if (req.query.letmein && req.query.letmein === 'now') { | ||
return allow(true); | ||
} | ||
allow(false); | ||
}), | ||
function (req, res) { | ||
res.status(200).send({ custom_authentication : true }); | ||
} | ||
]); | ||
// Handle Errors | ||
otto.error_handler(app); | ||
// Bind SuperTest | ||
@@ -74,6 +90,4 @@ var request = supertest(app); | ||
describe('Protected Route', function () { | ||
describe('HTTP Basic Protected Route', function () { | ||
it('should deny a request without credentials', function (done) { | ||
@@ -84,3 +98,9 @@ request.get('/protected') | ||
.expect(401) | ||
.expect({ error : { message : 'Authentication Failed' } }) | ||
.expect({ | ||
error : { | ||
type : 'client', | ||
name : 'ErrorUnauthorized', | ||
message : 'Authentication Failed' | ||
} | ||
}) | ||
.end(done); | ||
@@ -95,3 +115,9 @@ }); | ||
.expect(401) | ||
.expect({ error : { message : 'Authentication Failed' } }) | ||
.expect({ | ||
error : { | ||
type : 'client', | ||
name : 'ErrorUnauthorized', | ||
message : 'Authentication Failed' | ||
} | ||
}) | ||
.end(done); | ||
@@ -106,3 +132,9 @@ }); | ||
.expect(401) | ||
.expect({ error : { message : 'Authentication Failed' } }) | ||
.expect({ | ||
error : { | ||
type : 'client', | ||
name : 'ErrorUnauthorized', | ||
message : 'Authentication Failed' | ||
} | ||
}) | ||
.end(done); | ||
@@ -117,3 +149,9 @@ }); | ||
.expect(401) | ||
.expect({ error : { message : 'Authentication Failed' } }) | ||
.expect({ | ||
error : { | ||
type : 'client', | ||
name : 'ErrorUnauthorized', | ||
message : 'Authentication Failed' | ||
} | ||
}) | ||
.end(done); | ||
@@ -134,2 +172,30 @@ }); | ||
describe('Custom Authentication', function () { | ||
it('should deny a request without query "letmein"', function (done) { | ||
request.get('/custom') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(401) | ||
.expect({ | ||
error : { | ||
type : 'client', | ||
name : 'ErrorUnauthorized', | ||
message : 'Authentication Failed' | ||
} | ||
}) | ||
.end(done); | ||
}); | ||
it('should allow a request when query "letmein" is set to "now"', function (done) { | ||
request.get('/custom?letmein=now') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200) | ||
.expect({ custom_authentication : true }) | ||
.end(done); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8938
197
2
+ Addedotto-errors@0.0.2
+ Addedotto-errors@0.0.2(transitive)