This repository was archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from apoorvakorde/myAssignment4
[Assignment 4] Basic Authentication
- Loading branch information
Showing
13 changed files
with
238 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ config.json | |
*/._* | ||
*/*/._* | ||
coverage.* | ||
.settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 0.10 | ||
- 0.12 | ||
- iojs | ||
- 4.0 | ||
- 4 | ||
- 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
// Load modules | ||
|
||
const Basic = require('hapi-auth-basic'); | ||
const Users = require('./users.json'); | ||
|
||
|
||
// Declare internals | ||
|
||
const internals = {}; | ||
|
||
|
||
internals.validateFunc = function (request, username, password, callback) { | ||
|
||
const user = Users[username]; | ||
if (!user || user.password !== password) { | ||
return callback(null, false); | ||
} | ||
|
||
user.username = username; | ||
|
||
return callback(null, true, user); | ||
}; | ||
|
||
|
||
exports.register = function (server, options, next) { | ||
|
||
server.register(Basic, (err) => { | ||
|
||
if (err) { | ||
return next(err); | ||
} | ||
|
||
server.auth.strategy('basic', 'basic', { validateFunc: internals.validateFunc }); | ||
server.route({ | ||
method: 'GET', | ||
path: '/private', | ||
config: { | ||
auth: 'basic', | ||
description: 'Returns a greeting message to the authenticated user', | ||
handler: function (request, reply) { | ||
|
||
const html = '<div>Hello ' + request.auth.credentials.username + '</div>'; | ||
return reply(html); | ||
} | ||
} | ||
}); | ||
|
||
return next(); | ||
}); | ||
}; | ||
|
||
|
||
exports.register.attributes = { | ||
name: 'Private' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
'use strict'; | ||
|
||
// Load modules | ||
|
||
var Hoek = require('hoek'); | ||
var Server = require('./index'); | ||
const Hoek = require('hoek'); | ||
const Server = require('./index'); | ||
|
||
|
||
// Declare internals | ||
|
||
var internals = {}; | ||
const internals = {}; | ||
|
||
|
||
Server.init(8000, function (err, server) { | ||
Server.init(8000, (err, server) => { | ||
|
||
Hoek.assert(!err, err); | ||
console.log('Server started at: ' + server.info.uri); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"foo": { | ||
"password": "foo" | ||
}, | ||
"bar": { | ||
"password": "bar" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
{ | ||
"name": "hueniversity", | ||
"version": "0.0.3", | ||
"name": "hapi-university", | ||
"version": "0.0.4", | ||
"description": "Community learning experiment", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/hueniverse/hueniversity.git" | ||
"url": "https://github.com/hapijs/university.git" | ||
}, | ||
"keywords": [ | ||
"hapi", | ||
"learn", | ||
"community" | ||
], | ||
"license": "BSD", | ||
"license": "BSD-3-Clause", | ||
"bugs": { | ||
"url": "https://github.com/hueniverse/hueniversity/issues" | ||
"url": "https://github.com/hapijs/university/issues" | ||
}, | ||
"homepage": "https://github.com/hueniverse/hueniversity", | ||
"homepage": "https://github.com/hapijs/university", | ||
"dependencies": { | ||
"hapi": "8.x.x", | ||
"hoek": "2.x.x" | ||
"hapi": "12.x.x", | ||
"hapi-auth-basic": "4.x.x", | ||
"hoek": "3.x.x" | ||
}, | ||
"scripts": { | ||
"test": "node node_modules/lab/bin/lab -a code -t 100 -L", | ||
"test": "lab -a code -t 100 -L -v", | ||
"test-cov-html": "lab -a code -L -r html -o coverage.html", | ||
"start": "node lib/start.js" | ||
}, | ||
"devDependencies": { | ||
"code": "1.x.x", | ||
"lab": "5.x.x" | ||
"code": "2.x.x", | ||
"lab": "8.x.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict'; | ||
|
||
// Load modules | ||
|
||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const University = require('../lib'); | ||
const Users = require('../lib/users.json'); | ||
const Basic = require('hapi-auth-basic'); | ||
|
||
|
||
// Declare internals | ||
|
||
const internals = {}; | ||
|
||
|
||
// Test shortcuts | ||
|
||
const lab = exports.lab = Lab.script(); | ||
const describe = lab.experiment; | ||
const expect = Code.expect; | ||
const it = lab.test; | ||
|
||
|
||
describe('/private', () => { | ||
|
||
it('returns a greeting for the authenticated user', (done) => { | ||
|
||
University.init(0, (err, server) => { | ||
|
||
expect(err).to.not.exist(); | ||
|
||
const request = { method: 'GET', url: '/private', headers: { authorization: internals.header('foo', Users.foo.password) } }; | ||
server.inject(request, (res) => { | ||
|
||
expect(res.statusCode, 'Status Code').to.equal(200); | ||
expect(res.result, 'result').to.equal('<div>Hello foo</div>'); | ||
|
||
server.stop(done); | ||
}); | ||
}); | ||
}); | ||
|
||
it('errors on wrong password', (done) => { | ||
|
||
University.init(0, (err, server) => { | ||
|
||
expect(err).to.not.exist(); | ||
|
||
const request = { method: 'GET', url: '/private', headers: { authorization: internals.header('foo', '') } }; | ||
server.inject(request, (res) => { | ||
|
||
expect(res.statusCode, 'Status code').to.equal(401); | ||
|
||
server.stop(done); | ||
}); | ||
}); | ||
}); | ||
|
||
it('errors on failed auth', (done) => { | ||
|
||
University.init(0, (err, server) => { | ||
|
||
expect(err).to.not.exist(); | ||
|
||
const request = { method: 'GET', url: '/private', headers: { authorization: internals.header('I do not exist', '') } }; | ||
server.inject(request, (res) => { | ||
|
||
expect(res.statusCode, 'Status code').to.equal(401); | ||
|
||
server.stop(done); | ||
}); | ||
}); | ||
}); | ||
|
||
it('errors on failed registering of auth', { parallel: false }, (done) => { | ||
|
||
const orig = Basic.register; | ||
|
||
Basic.register = function (plugin, options, next) { | ||
|
||
Basic.register = orig; | ||
return next(new Error('fail')); | ||
}; | ||
|
||
Basic.register.attributes = { | ||
name: 'fake hapi-auth-basic' | ||
}; | ||
|
||
University.init(0, (err) => { | ||
|
||
expect(err).to.exist(); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
internals.header = function (username, password) { | ||
|
||
return 'Basic ' + (new Buffer(username + ':' + password, 'utf8')).toString('base64'); | ||
}; |
Oops, something went wrong.