Skip to content

Commit e93b8de

Browse files
author
Leon Strauss
committed
removed semicolons. semicolons are evil
1 parent 21bd225 commit e93b8de

3 files changed

Lines changed: 71 additions & 71 deletions

File tree

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ The module will export a function, that you can call with an options object to
1818
get the middleware:
1919

2020
```js
21-
var app = require('express')();
22-
var basicAuth = require('express-basic-auth');
21+
var app = require('express')()
22+
var basicAuth = require('express-basic-auth')
2323

2424
app.use(basicAuth({
2525
users: { 'admin': 'supersecret' }
26-
}));
26+
}))
2727
```
2828

2929
The middleware will now check incoming requests to match the credentials
@@ -50,7 +50,7 @@ app.use(basicAuth({
5050
'adam': 'password1234',
5151
'eve': 'asdfghjkl'
5252
}
53-
}));
53+
}))
5454
```
5555

5656
The middleware will check incoming requests to have a basic auth header matching
@@ -63,10 +63,10 @@ however you want. It will be called with a username and password and is expected
6363
return `true` or `false` to indicate that the credentials were approved or not:
6464

6565
```js
66-
app.use(basicAuth( { authorizer: myAuthorizer } ));
66+
app.use(basicAuth( { authorizer: myAuthorizer } ))
6767

6868
function myAuthorizer(username, password) {
69-
return username.startsWith('A') && password.startsWith('secret');
69+
return username.startsWith('A') && password.startsWith('secret')
7070
}
7171
```
7272

@@ -87,11 +87,11 @@ Let's look at the same authorizer again, but this time asynchronous:
8787
app.use(basicAuth({
8888
authorizer: myAsyncAuthorizer,
8989
authorizeAsync: true
90-
}));
90+
}))
9191

9292
function myAsyncAuthorizer(username, password, cb) {
9393
if(username.startsWith('A') && password.startsWith('secret'))
94-
return cb(null, true);
94+
return cb(null, true)
9595
else
9696
return cb(null, false)
9797
}
@@ -109,12 +109,12 @@ be used as-is, otherwise it will be sent as JSON:
109109
app.use(basicAuth({
110110
users: { 'Foo': 'bar' },
111111
unauthorizedResponse: getUnauthorizedResponse
112-
}));
112+
}))
113113

114114
function getUnauthorizedResponse(req) {
115-
return req.auth
116-
? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected')
117-
: 'No credentials provided';
115+
return req.auth ?
116+
('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') :
117+
'No credentials provided'
118118
}
119119
```
120120

@@ -129,7 +129,7 @@ on unauthorized responses:
129129
app.use(basicAuth({
130130
users: { 'someuser': 'somepassword' },
131131
challenge: true
132-
}));
132+
}))
133133
```
134134

135135
## Try it

example.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var express = require('express');
1+
const express = require('express')
22

3-
var app = express();
3+
var app = express()
44

5-
var basicAuth = require('./index.js');
5+
const basicAuth = require('./index.js')
66

77
/**
88
* express-basic-auth
@@ -25,86 +25,86 @@ var staticUserAuth = basicAuth({
2525
'Admin': 'secret1234'
2626
},
2727
challenge: false
28-
});
28+
})
2929

3030
//Uses a custom (synchronous) authorizer function
3131
var customAuthorizerAuth = basicAuth({
3232
authorizer: myAuthorizer
33-
});
33+
})
3434

3535
//Same, but sends a basic auth challenge header when authorization fails
3636
var challengeAuth = basicAuth({
3737
authorizer: myAuthorizer,
3838
challenge: true
39-
});
39+
})
4040

4141
//Uses a custom asynchronous authorizer function
4242
var asyncAuth = basicAuth({
4343
authorizer: myAsyncAuthorizer,
4444
authorizeAsync: true
45-
});
45+
})
4646

4747
//Uses a custom response body function
4848
var customBodyAuth = basicAuth({
4949
users: { 'Foo': 'bar' },
5050
unauthorizedResponse: getUnauthorizedResponse
51-
});
51+
})
5252

5353
//Uses a static response body
5454
var staticBodyAuth = basicAuth({
5555
unauthorizedResponse: 'Haaaaaha'
56-
});
56+
})
5757

5858
//Uses a JSON response body
5959
var jsonBodyAuth = basicAuth({
6060
unauthorizedResponse: { foo: 'bar' }
61-
});
61+
})
6262

6363
app.get('/static', staticUserAuth, function(req, res) {
64-
res.status(200).send('You passed');
65-
});
64+
res.status(200).send('You passed')
65+
})
6666

6767
app.get('/custom', customAuthorizerAuth, function(req, res) {
68-
res.status(200).send('You passed');
69-
});
68+
res.status(200).send('You passed')
69+
})
7070

7171
app.get('/challenge', challengeAuth, function(req, res) {
72-
res.status(200).send('You passed');
73-
});
72+
res.status(200).send('You passed')
73+
})
7474

7575
app.get('/async', asyncAuth, function(req, res) {
76-
res.status(200).send('You passed');
77-
});
76+
res.status(200).send('You passed')
77+
})
7878

7979
app.get('/custombody', customBodyAuth, function(req, res) {
80-
res.status(200).send('You passed');
81-
});
80+
res.status(200).send('You passed')
81+
})
8282

8383
app.get('/staticbody', staticBodyAuth, function(req, res) {
84-
res.status(200).send('You passed');
85-
});
84+
res.status(200).send('You passed')
85+
})
8686

8787
app.get('/jsonbody', jsonBodyAuth, function(req, res) {
88-
res.status(200).send('You passed');
89-
});
88+
res.status(200).send('You passed')
89+
})
9090

9191
app.listen(8080, function() {
92-
console.log("Listening!");
93-
});
92+
console.log("Listening!")
93+
})
9494

9595
//Custom authorizer checking if the username starts with 'A' and the password with 'secret'
9696
function myAuthorizer(username, password) {
97-
return username.startsWith('A') && password.startsWith('secret');
97+
return username.startsWith('A') && password.startsWith('secret')
9898
}
9999

100100
//Same but asynchronous
101101
function myAsyncAuthorizer(username, password, cb) {
102102
if(username.startsWith('A') && password.startsWith('secret'))
103-
return cb(null, true);
103+
return cb(null, true)
104104
else
105105
return cb(null, false)
106106
}
107107

108108
function getUnauthorizedResponse(req) {
109-
return req.auth ? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') : 'No credentials provided';
109+
return req.auth ? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') : 'No credentials provided'
110110
}

index.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
var auth = require('basic-auth');
2-
var assert = require('assert');
1+
const auth = require('basic-auth')
2+
const assert = require('assert')
33

44
function buildMiddleware(options) {
5-
var challenge = options.challenge != undefined ? !!options.challenge : false;
6-
var users = options.users || {};
7-
var authorizer = options.authorizer || staticUsersAuthorizer;
8-
var isAsync = options.authorizeAsync != undefined ? !!options.authorizeAsync : false;
9-
var getResponseBody = options.unauthorizedResponse;
5+
var challenge = options.challenge != undefined ? !!options.challenge : false
6+
var users = options.users || {}
7+
var authorizer = options.authorizer || staticUsersAuthorizer
8+
var isAsync = options.authorizeAsync != undefined ? !!options.authorizeAsync : false
9+
var getResponseBody = options.unauthorizedResponse
1010

1111
if(!getResponseBody)
12-
getResponseBody = function() { return ''; };
12+
getResponseBody = function() { return '' }
1313
else if(typeof getResponseBody != 'function')
14-
getResponseBody = function() { return options.unauthorizedResponse };
14+
getResponseBody = function() { return options.unauthorizedResponse }
1515

16-
assert(typeof getResponseBody == 'function', 'Expected a string or function for the unauthorizedResponse option');
17-
assert(typeof users == 'object', 'Expected an object for the basic auth users, found ' + typeof users + ' instead');
18-
assert(typeof authorizer == 'function', 'Expected a function for the basic auth authorizer, found ' + typeof authorizer + ' instead');
16+
assert(typeof getResponseBody == 'function', 'Expected a string or function for the unauthorizedResponse option')
17+
assert(typeof users == 'object', 'Expected an object for the basic auth users, found ' + typeof users + ' instead')
18+
assert(typeof authorizer == 'function', 'Expected a function for the basic auth authorizer, found ' + typeof authorizer + ' instead')
1919

2020
function staticUsersAuthorizer(username, password) {
2121
for(var i in users)
2222
if(username == i && password == users[i])
23-
return true;
23+
return true
2424

25-
return false;
25+
return false
2626
}
2727

2828
return function authMiddleware(req, res, next) {
29-
var authentication = auth(req);
29+
var authentication = auth(req)
3030

3131
if(!authentication)
32-
return unauthorized();
32+
return unauthorized()
3333

3434
req.auth = {
3535
user: authentication.name,
3636
password: authentication.pass
37-
};
37+
}
3838

3939
if(isAsync)
40-
return authorizer(authentication.name, authentication.pass, authorizerCallback);
40+
return authorizer(authentication.name, authentication.pass, authorizerCallback)
4141
else if(!authorizer(authentication.name, authentication.pass))
42-
return unauthorized();
42+
return unauthorized()
4343

44-
return next();
44+
return next()
4545

4646
function unauthorized() {
4747
//TODO: Allow to set realm for the challenge
4848
if(challenge)
49-
res.set('WWW-Authenticate', 'Basic');
49+
res.set('WWW-Authenticate', 'Basic')
5050

5151
//TODO: Allow response body to be JSON (maybe autodetect?)
52-
const response = getResponseBody(req);
52+
const response = getResponseBody(req)
5353

5454
if(typeof response == 'string')
55-
return res.status(401).send(response);
55+
return res.status(401).send(response)
5656

57-
return res.status(401).json(response);
57+
return res.status(401).json(response)
5858
}
5959

6060
function authorizerCallback(err, approved) {
61-
assert.ifError(err);
61+
assert.ifError(err)
6262

6363
if(approved)
64-
return next();
64+
return next()
6565

66-
return unauthorized();
66+
return unauthorized()
6767
}
68-
};
68+
}
6969
}
7070

71-
module.exports = buildMiddleware;
71+
module.exports = buildMiddleware

0 commit comments

Comments
 (0)