Skip to content
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

Add optional basic http auth #22

Open
wants to merge 1 commit 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
40 changes: 40 additions & 0 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = function (app, user, password) {
app.use(function* (next) {
const challenge = `Basic realm="Node File Manager"`
let authorization = this.header['authorization']
if (authorization != null && authorization.slice(0, 6) === 'Basic ') {
authorization = new Buffer(authorization.slice(6), 'base64').toString('utf8')
const splitIndex = authorization.indexOf(':')
if (splitIndex > -1) {
const user = authorization.slice(0, splitIndex)
const password = authorization.slice(splitIndex + 1)
this.request.auth = {
user: user,
password: password
}
}
}

yield next

if (this.request.auth == null) {
this.status = 401
this.response.set('WWW-Authenticate', challenge)
}
})

app.use(function* (next) {
if (!this.request.auth) {
this.body = 'Please log in.'
return // 401 response
}

if (this.request.auth.user !== user || this.request.auth.password !== password) {
this.body = 'Invalid user.'
delete this.request.auth
return // 401 response
}

yield next
})
}
19 changes: 18 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var koaStatic = require('koa-static');
// Config
var argv = require('optimist')
.usage([
'USAGE: $0 [-p <port>] [-d <directory>]']
'USAGE: $0 [-p <port>] [-d <directory>] [--user <user>] [--password <password>]']
)
.option('port', {
alias: 'p',
Expand All @@ -29,6 +29,12 @@ var argv = require('optimist')
alias: 'h',
description: "Display This Help Message"
})
.option('user', {
description: "Username for basic http auth"
})
.option('password', {
description: "Password for basic http auth"
})
.argv;

if (argv.help) {
Expand All @@ -41,6 +47,11 @@ if (argv.version) {
process.exit(0);
}

if ((argv.user && !argv.password) || (argv.password && !argv.user)) {
console.log('Both username and password are required to enable http auth')
process.exit(0);
}

global.C = {
data: {
root: argv.directory || path.dirname('.')
Expand All @@ -58,6 +69,12 @@ var startServer = function (app, port) {
};

var app = koa();

if (argv.user && argv.password) {
var enableAuth = require('./auth')
enableAuth(app, argv.user, argv.password)
}

app.proxy = true;
app.use(Tools.handelError);
app.use(Tools.realIp);
Expand Down