-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.js
More file actions
41 lines (34 loc) · 875 Bytes
/
auth.js
File metadata and controls
41 lines (34 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var basicAuth = require('basic-auth');
var config = require('config');
var auth = { };
auth.viewAuth = function() {
return function(req, res, next) {
if (req.session.viewAuthorised || isAuthValid(req, config.get('credentials.view'))) {
req.session.viewAuthorised = true;
next();
} else {
unauthorised(res);
}
}
};
auth.createAuth = function() {
return function(req, res, next) {
if (isAuthValid(req, config.get('credentials.create'))) {
next();
} else {
unauthorised(res);
}
}
};
var isAuthValid = function(req, credentials) {
if (!credentials) {
return true;
}
var user = basicAuth(req);
return !!user && user.name == credentials.username && user.pass == credentials.password;
};
var unauthorised = function(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
res.sendStatus(401);
}
module.exports = auth;