Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Assignment6 #152

Merged
merged 18 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ config.json
*/._*
*/*/._*
coverage.*
*/*~
.emacs*
Binary file added assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions assets/scripts/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.onreadystatechange = function () {

if (document.readyState === 'complete') {

document.getElementById('btnLogin').addEventListener('click', function (event) {

event.preventDefault();
console.log('Login Button Clicked');
});
}
};
4 changes: 4 additions & 0 deletions assets/styles/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h3 {
font-size: 24px;
font-weight: bold;
}
4 changes: 2 additions & 2 deletions guides/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ There are two main ways to get back to sync and one way to avoid this problem in

This is the simplest. You just go to your fork on GitHub, go to **settings** and at the bottom
click on **Delete this repository**. Once deleted, go back to
[the official repository](https://github.com/hueniverse/hueniversity) and fork it again. All set.
[the official repository](https://github.com/hapijs/university) and fork it again. All set.
Remember to also delete your local directory and then to clone your new fork all over again.

### Replace your master branch with the remote branch.

```
git remote add upstream https://github.com/hueniverse/hueniversity
git remote add upstream https://github.com/hapijs/university
git fetch upstream
git branch backup
git checkout upstream/master -B master
Expand Down
15 changes: 15 additions & 0 deletions lib/certs/cert.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN CERTIFICATE-----
MIICRzCCAbACCQDLKLdg5Wbn9TANBgkqhkiG9w0BAQsFADBoMQswCQYDVQQGEwJB
VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMSEwHwYJKoZIhvcNAQkBFhJydXRhaWh3YUBnbWFpbC5jb20wHhcN
MTUwNjA4MTE1MjU1WhcNMTYwNjA3MTE1MjU1WjBoMQswCQYDVQQGEwJBVTETMBEG
A1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkg
THRkMSEwHwYJKoZIhvcNAQkBFhJydXRhaWh3YUBnbWFpbC5jb20wgZ8wDQYJKoZI
hvcNAQEBBQADgY0AMIGJAoGBANIDuMQini8ttfexVQdmph6hjKXxVc1Zk8bX6z0O
b9uKw4SrHXDKYelgmvG9Gv8awl8hElbUamVNWe5U7sN3LxiTzTMu+rhScABzrt6X
VZEJxZsdFYgIzvzC6PUy1rBMb7MZGmdm5Cd5eaWAGQ/lPfI+dW604FKVo/xI5gPd
8lWbAgMBAAEwDQYJKoZIhvcNAQELBQADgYEArZPt5dJXnGQxWTt0FsGF9Mk6IsfJ
4sTS2vt9KND2i18RT0qyn3YUsqfnjieRKId5Ru2PYsqJEFz2/e0OwmAxjmrpiz2q
9yfTHFtWB3Rjt/O+IXN9D4otwb6ADrzkXDy1FWwPMDK9vJ3A/quBhN8iCpcr+FdH
MMbrPytUAKCaSLs=
-----END CERTIFICATE-----
15 changes: 15 additions & 0 deletions lib/certs/key.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDSA7jEIp4vLbX3sVUHZqYeoYyl8VXNWZPG1+s9Dm/bisOEqx1w
ymHpYJrxvRr/GsJfIRJW1GplTVnuVO7Ddy8Yk80zLvq4UnAAc67el1WRCcWbHRWI
CM78wuj1MtawTG+zGRpnZuQneXmlgBkP5T3yPnVutOBSlaP8SOYD3fJVmwIDAQAB
AoGARPLL2jizTjHnp+YkUEW3DB9lmaVd0gkypvmEVYtD1gP8pLW4+Kq48nGw7OR0
1IPohGrLJo6eT5vRhpmDudCLOSIYPVqi/14kW+ahMJuMc2FGohRL89dIGEWvGPks
2c4YCtYhZL4sfFJwE2TVnOv8jmQM8l0Y/K/vXcLPrNFzfmECQQDvmo5mEYsVrV9/
UFCEi7b38e2tLdo40U6eRm9yd6zK7XpYqQVMGJFB14EU4FSyUMk9lpvAiHJjFCG2
alkR6mtnAkEA4GLQWGXkjNBn/5aUlbPsqGKzWfDQqMaGnsPCDEJsONAemPxqDsmO
5qBP3CWH093azT2NTWWDefM07KdciqKXrQJBANjzfE6lQeet5VN1GIL7sv+M2qW9
Z+OcefBMXKsr+5R/NRHEcVx2vkf85w9NFMOk7KLMlVje85wM5X2zi9TyCq8CQQC7
K1HVWvEhULpfZbD9iv8Omn42uPykvC0RBAO6jDGP1nKfdHXXbePolrLm1NuPLBFW
gFB8zf+02t07xoMYFXZlAkA5YaolWVD/IMb1H8UYh2enseR5IFVrrkbOAnTSHXzX
w1w+wU4rNow/hftjSuW7nnOozWYwI+HEmgvsf9P6Wnke
-----END RSA PRIVATE KEY-----
15 changes: 15 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Fs = require('fs');

var config = module.exports = {};

// Configuring TLS
config.tls = {
key: Fs.readFileSync('./lib/certs/key.key'),
cert: Fs.readFileSync('./lib/certs/cert.crt'),

// Only necessary if using the client certificate authentication.
requestCert: true,

// Only necessary only if client is using the self-signed certificate.
ca: []
};
52 changes: 43 additions & 9 deletions lib/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,63 @@ var internals = {

exports.register = function (server, options, next) {

// Future, put global view config into static plugin
server.views({
engines: {
html: require('handlebars')
},
path: '../views',
partialsPath: '../views/partials',
relativeTo: __dirname
});

// Routing for static files
server.route({
method: 'GET',
path: '/home',
config: {
description: 'Returns the home page',
handler: {
view: {
template: 'home',
context: {
path: Path.relative(internals.rootPath, Path.resolve(internals.viewsPath, 'home.html'))
path: '/{assetpath*}',
handler: {
directory: {
path: './assets/'
}
}
});

// Home related routing
server.route([

// Index route

// Login route

{
method: 'GET',
path: '/login',
config: {
description: 'Returns a login form',
handler: {
view: {
template: 'login'
}
}
}
},

// Home route

{
method: 'GET',
path: '/home',
config: {
description: 'Returns the home page',
handler: {
view: {
template: 'home'
}
}
}
}
});

]);

return next();
};
Expand Down
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ exports.init = function (manifest, composeOptions, next) {
return next(err);
}

// TLS everything

server.select('web').ext('onRequest', function (request, reply) {

return reply.redirect('https://localhost:8001' + request.url.path).permanent();
});

// Start the server

server.start(function (err) {

return next(err, server);
Expand Down
32 changes: 27 additions & 5 deletions lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Hoek = require('hoek');
var Server = require('./index');
var Config = require('./config');

// Declare internals

Expand All @@ -10,13 +11,27 @@ var internals = {};
internals.manifest = {
connections: [
{
port: 8000
host: 'localhost',
port: 8000,
labels: ['web']
},
{
host: 'localhost',
port: 8001,
labels: ['web-tls'],
tls: Config.tls
}
],
plugins: {
'./version': {},
'./private': {},
'./home': {},
'./version': [{
'select': ['web', 'web-tls']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why register the plugins also on web when all routes are redirected to web-tls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdriVanHoudt, I think you are right 👍 , since everything gets caught and redirected to https before being routed, it may be not necessary to register web.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems unnecessary at least

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, will be addressed in the next commit.

}],
'./private': [{
'select': ['web', 'web-tls']
}],
'./home': [{
'select': ['web', 'web-tls']
}],
'./auth': {},
'hapi-auth-basic': {}
}
Expand All @@ -29,5 +44,12 @@ internals.composeOptions = {
Server.init(internals.manifest, internals.composeOptions, function (err, server) {

Hoek.assert(!err, err);
console.log('Server started at: ' + server.info.uri);

// Server connections
var web = server.select('web');
var webTls = server.select('web-tls');

// Logging started server
console.log('Web server started at: ' + web.info.uri);
console.log('WebTLS server started at: ' + webTls.info.uri);
});
33 changes: 29 additions & 4 deletions test/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Code = require('code');
var Lab = require('lab');
var University = require('../lib');
var Path = require('path');
var Config = require('../lib/config');

// Declare internals

Expand All @@ -19,17 +20,33 @@ var it = lab.test;

describe('/home', function () {

it('returns home page containing relative path from root to home template', function (done) {
it('ensures that /home is always redirected to https', function (done) {

University.init(internals.manifest, internals.composeOptions, function (err, server) {

expect(err).to.not.exist();

var request = { method: 'GET', url: '/home' };
server.inject(request, function (res) {
server.select('web').inject(request, function (res) {

expect(res.statusCode, 'Status code').to.equal(301);
expect(res.headers.location).to.equal('https://localhost:8001/home');

server.stop(done);
});
});
});

it('returns an home page via https', function (done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the actual test for testing if it indeed is served via https?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdriVanHoudt What you mean to this test below or the above one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdriVanHoudt

expect(res.headers.location).to.equal('https://localhost:8001/home');

Headers location shows it was redirected to https, proving it was redirected to https.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

below, i see you did in other tests although if you change the port your test fails even when routing to https which seems weird but also will probably never happen ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdriVanHoudt How would you test it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rutaihwa turns out it's not really available, because of the redirect the protocol in the connection info is http (somewhere deep in the res object ;)) so the better thing would be to do res.headers.location.substring(0,5) === 'https' I think


University.init(internals.manifest, internals.composeOptions, function (err, server) {

expect(err).to.not.exist();

var request = { method: 'GET', url: '/home' };
server.select('web-tls').inject(request, function (res) {

expect(res.statusCode, 'Status code').to.equal(200);
expect(res.result, 'result').to.equal(Path.relative(Path.resolve('__dirname', '../'), Path.resolve('__dirname', '../views/home.html')));

server.stop(done);
});
Expand All @@ -40,7 +57,15 @@ describe('/home', function () {
internals.manifest = {
connections: [
{
port: 0
host: 'localhost',
port: 0,
labels: ['web']
},
{
host: 'localhost',
port: 0,
labels: ['web-tls'],
tls: Config.tls
}
],
plugins: {
Expand Down
Loading