Skip to content

Add in authentication #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if (argv.directory) options.directory = argv.directory;
if (argv.nocolors) options.colors = false;
if (argv.nologs) options.nologs = true;
if (argv.help) options.help = true;
if (argv.auth || argv.a) options.auth = argv.auth || argv.a;

// Run the Server!
var url = require('../lib/server').run(options);
Expand Down
39 changes: 39 additions & 0 deletions lib/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Simple HTTP Server Login</title>
<style type="text/css">
body {
font-family: tahoma; }

h1 { text-align: center }
fieldset { border: none; }

.container { width: 50%; margin: 0 auto; text-align: center; }
.container #password {
width: 100%;
height: 30px;
font-size: 1.25em;
border-radius: 5px;
border: 1px solid #CCC; }

.container #login {
margin-top: 10px;
width: 50px;
height: 30px;
border-radius: 5px;
border: 1px solid #CCC; }
</style>
</head>
<body>
<div class="container">
<form method="post" action="auth">
<h1>Login</h1>
<fieldset>
<input type="password" name="password" placeholder="Password" id="password"/>
<input type="submit" name="submit" value="Login" id="login" />
</fieldset>
</form>
</div>
</body>
</html>
48 changes: 43 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
http: require('http'),
url: require('url'),
fs: require('fs'),
path: require('path')
path: require('path'),
qs: require('querystring')
};

config = exports.config = {
Expand Down Expand Up @@ -64,7 +65,7 @@
};

return mimeTypes[ext.toLowerCase()];
}
};

BANNER = 'Usage: nserver [options]\n\nIf called without options, `nserver` will listen to the current directory on port ' + config.port + '.';

Expand All @@ -74,7 +75,8 @@
['', '--launch', 'launch the server in your default browser after starting'],
['', '--nocolors', 'disable colors, default: false'],
['', '--nologs', 'disable request logs, default: false'],
['', '--help', 'show this help screen']
['', '--help', 'show this help screen'],
['-a', '--auth', 'allow a login screen']
];

/**
Expand All @@ -86,15 +88,16 @@
*
* @author Andrew Thorp
*/
var isLoggedIn = false;
exports.run = function(options){

// Options
if (options !== undefined){
if (options.help !== undefined) return usage();
if (options.port !== undefined) exports.config.port = config.port = options.port;
if (options.directory !== undefined) exports.config.directory = config.directory = options.directory;
if (options.colors !== undefined) exports.config.colors = config.colors = options.colors;
if (options.nologs !== undefined) exports.config.nologs = config.nologs = options.nologs;
if ( options.auth ) exports.config.auth = config.auth = options.auth;
}

// Fire off the server!
Expand All @@ -110,6 +113,41 @@

var fullPath = libs.path.join(config.directory, path);

if ( config.auth && /auth\/?/.test( request.url ) && request.method == 'POST' ) {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end',function(){
var POST = libs.qs.parse(body);

isLoggedIn = options.auth === POST.password;

response.writeHead(302, {
'Location': '/'
});
response.end();
});
return;
}

if ( config.auth && !isLoggedIn ) {
fullPath = libs.path.join(__dirname, "login.html");

libs.fs.readFile(fullPath, function(error, data) {
if ( !error ){
response.writeHead(200, {
'content-type': getMimeType( fullPath )
});
response.write(data);
response.end();
result += " - 200 AUTHENTICATE".green;
logString(result);
}
});
return;
}

libs.path.exists(fullPath, function(exists){

if (exists) {
Expand Down Expand Up @@ -214,7 +252,7 @@
directory: config.directory.green,
shortDir: "./".green,
url: "http://localhost:".green + config.port.toString().green + "/".green
}
};

if (__dirname.indexOf(consoleStrings.directory.stripColors) !== -1){
currDir = consoleStrings.shortDir;
Expand Down