123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- const should = require('should')
- const express = require('express')
- const supertest = require('supertest')
-
- const basicAuth = require('./index.js')
-
- var app = express()
-
- //Requires basic auth with username 'Admin' and password 'secret1234'
- var staticUserAuth = basicAuth({
- users: {
- 'Admin': 'secret1234'
- },
- challenge: false
- })
-
- //Uses a custom (synchronous) authorizer function
- var customAuthorizerAuth = basicAuth({
- authorizer: myAuthorizer
- })
-
- //Uses a custom (synchronous) authorizer function
- var customCompareAuth = basicAuth({
- authorizer: myComparingAuthorizer
- })
-
- //Same, but sends a basic auth challenge header when authorization fails
- var challengeAuth = basicAuth({
- authorizer: myAuthorizer,
- challenge: true
- })
-
- //Uses a custom asynchronous authorizer function
- var asyncAuth = basicAuth({
- authorizer: myAsyncAuthorizer,
- authorizeAsync: true
- })
-
- //Uses a custom response body function
- var customBodyAuth = basicAuth({
- users: { 'Foo': 'bar' },
- unauthorizedResponse: getUnauthorizedResponse
- })
-
- //Uses a static response body
- var staticBodyAuth = basicAuth({
- unauthorizedResponse: 'Haaaaaha'
- })
-
- //Uses a JSON response body
- var jsonBodyAuth = basicAuth({
- unauthorizedResponse: { foo: 'bar' }
- })
-
- //Uses a custom realm
- var realmAuth = basicAuth({
- challenge: true,
- realm: 'test'
- })
-
- //Uses a custom realm function
- var realmFunctionAuth = basicAuth({
- challenge: true,
- realm: function (req) {
- return 'bla'
- }
- })
-
- app.get('/static', staticUserAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/custom', customAuthorizerAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/custom-compare', customCompareAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/challenge', challengeAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/async', asyncAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/custombody', customBodyAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/staticbody', staticBodyAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/jsonbody', jsonBodyAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/realm', realmAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- app.get('/realmfunction', realmFunctionAuth, function(req, res) {
- res.status(200).send('You passed')
- })
-
- //Custom authorizer checking if the username starts with 'A' and the password with 'secret'
- function myAuthorizer(username, password) {
- return username.startsWith('A') && password.startsWith('secret')
- }
-
- //Same but asynchronous
- function myAsyncAuthorizer(username, password, cb) {
- if(username.startsWith('A') && password.startsWith('secret'))
- return cb(null, true)
- else
- return cb(null, false)
- }
-
- function myComparingAuthorizer(username, password) {
- return basicAuth.safeCompare(username, 'Testeroni') & basicAuth.safeCompare(password, 'testsecret')
- }
-
- function getUnauthorizedResponse(req) {
- return req.auth ? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') : 'No credentials provided'
- }
-
- describe('express-basic-auth', function() {
- describe('safe compare', function() {
- const safeCompare = basicAuth.safeCompare
-
- it('should return false on different inputs', function() {
- (!!safeCompare('asdf', 'rftghe')).should.be.false()
- })
-
- it('should return false on prefix inputs', function() {
- (!!safeCompare('some', 'something')).should.be.false()
- })
-
- it('should return false on different inputs', function() {
- (!!safeCompare('anothersecret', 'anothersecret')).should.be.true()
- })
- })
-
- describe('static users', function() {
- const endpoint = '/static'
-
- it('should reject on missing header', function(done) {
- supertest(app)
- .get(endpoint)
- .expect(401, done)
- })
-
- it('should reject on wrong credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('dude', 'stuff')
- .expect(401, done)
- })
-
- it('should reject on shorter prefix', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Admin', 'secret')
- .expect(401, done)
- })
-
- it('should reject without challenge', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('dude', 'stuff')
- .expect(function (res) {
- if(res.headers['WWW-Authenticate'])
- throw new Error('Response should not have a challenge')
- })
- .expect(401, done)
- })
-
- it('should accept correct credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Admin', 'secret1234')
- .expect(200, 'You passed', done)
- })
- })
-
- describe('custom authorizer', function() {
- const endpoint = '/custom'
-
- it('should reject on missing header', function(done) {
- supertest(app)
- .get(endpoint)
- .expect(401, done)
- })
-
- it('should reject on wrong credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('dude', 'stuff')
- .expect(401, done)
- })
-
- it('should accept fitting credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Aloha', 'secretverymuch')
- .expect(200, 'You passed', done)
- })
-
- describe('with safe compare', function() {
- const endpoint = '/custom-compare'
-
- it('should reject wrong credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('bla', 'blub')
- .expect(401, done)
- })
-
- it('should reject prefix credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Test', 'test')
- .expect(401, done)
- })
-
- it('should accept fitting credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Testeroni', 'testsecret')
- .expect(200, 'You passed', done)
- })
- })
- })
-
- describe('async authorizer', function() {
- const endpoint = '/async'
-
- it('should reject on missing header', function(done) {
- supertest(app)
- .get(endpoint)
- .expect(401, done)
- })
-
- it('should reject on wrong credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('dude', 'stuff')
- .expect(401, done)
- })
-
- it('should accept fitting credentials', function(done) {
- supertest(app)
- .get(endpoint)
- .auth('Aererer', 'secretiveStuff')
- .expect(200, 'You passed', done)
- })
- })
-
- describe('custom response body', function() {
- it('should reject on missing header and generate resposne message', function(done) {
- supertest(app)
- .get('/custombody')
- .expect(401, 'No credentials provided', done)
- })
-
- it('should reject on wrong credentials and generate response message', function(done) {
- supertest(app)
- .get('/custombody')
- .auth('dude', 'stuff')
- .expect(401, 'Credentials dude:stuff rejected', done)
- })
-
- it('should accept fitting credentials', function(done) {
- supertest(app)
- .get('/custombody')
- .auth('Foo', 'bar')
- .expect(200, 'You passed', done)
- })
-
- it('should reject and send static custom resposne message', function(done) {
- supertest(app)
- .get('/staticbody')
- .expect(401, 'Haaaaaha', done)
- })
-
- it('should reject and send static custom json resposne message', function(done) {
- supertest(app)
- .get('/jsonbody')
- .expect(401, { foo: 'bar' }, done)
- })
- })
-
- describe('challenge', function() {
- it('should reject with blank challenge', function(done) {
- supertest(app)
- .get('/challenge')
- .expect('WWW-Authenticate', 'Basic')
- .expect(401, done)
- })
-
- it('should reject with custom realm challenge', function(done) {
- supertest(app)
- .get('/realm')
- .expect('WWW-Authenticate', 'Basic realm="test"')
- .expect(401, done)
- })
-
- it('should reject with custom generated realm challenge', function(done) {
- supertest(app)
- .get('/realmfunction')
- .expect('WWW-Authenticate', 'Basic realm="bla"')
- .expect(401, done)
- })
- })
- })
|