Skip to content

Commit 7a0b814

Browse files
committed
asdf
0 parents  commit 7a0b814

File tree

8 files changed

+417
-0
lines changed

8 files changed

+417
-0
lines changed

.gitignore

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Components #
55+
##############
56+
57+
/build
58+
/components
59+
60+
# ImageMagick #
61+
###############
62+
63+
*.cache
64+
*.mpc
65+
66+
# Other #
67+
#########
68+
test/*.2
69+
test/*/*.2
70+
test/*.mp4
71+
test/images/originalSideways.jpg.2

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.js

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_js:
2+
- "0.10"
3+
language: node_js

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BIN = ./node_modules/.bin/
2+
3+
test:
4+
@NODE_ENV=test $(BIN)mocha \
5+
--require should \
6+
--reporter spec
7+
8+
.PHONY: test

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# simgr - Simple Image Resizer [![Build Status](https://travis-ci.org/expressjs/basic-auth-connect.png)](https://travis-ci.org/expressjs/basic-auth-connect)
2+
3+
Connect's Basic Auth middleware in its own module. This module is considered deprecated. You should instead create your own middleware with [basic-auth](https://github.com/visionmedia/node-basic-auth).
4+
5+
## API
6+
7+
```js
8+
var basicAuth = require('basic-auth-connect');
9+
```
10+
11+
Sorry, couldn't think of a more clever name.
12+
13+
Simple username and password
14+
15+
```js
16+
connect()
17+
.use(basicAuth('username', 'password'));
18+
```
19+
20+
Callback verification
21+
22+
```js
23+
connect()
24+
.use(basicAuth(function(user, pass){
25+
return 'tj' == user && 'wahoo' == pass;
26+
}))
27+
```
28+
29+
Async callback verification, accepting `fn(err, user)`.
30+
31+
```
32+
connect()
33+
.use(basicAuth(function(user, pass, fn){
34+
User.authenticate({ user: user, pass: pass }, fn);
35+
}))
36+
```
37+
38+
## License
39+
40+
The MIT License (MIT)
41+
42+
Copyright (c) 2013 Jonathan Ong [email protected]
43+
44+
Permission is hereby granted, free of charge, to any person obtaining a copy
45+
of this software and associated documentation files (the "Software"), to deal
46+
in the Software without restriction, including without limitation the rights
47+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48+
copies of the Software, and to permit persons to whom the Software is
49+
furnished to do so, subject to the following conditions:
50+
51+
The above copyright notice and this permission notice shall be included in
52+
all copies or substantial portions of the Software.
53+
54+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
60+
THE SOFTWARE.

index.js

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
var http = require('http');
2+
3+
/*!
4+
* Connect - basicAuth
5+
* Copyright(c) 2010 Sencha Inc.
6+
* Copyright(c) 2011 TJ Holowaychuk
7+
* MIT Licensed
8+
*/
9+
10+
/**
11+
* Basic Auth:
12+
*
13+
* Status: Deprecated. No bug reports or pull requests are welcomed
14+
* for this middleware. However, this middleware will not be removed.
15+
* Instead, you should use [basic-auth](https://github.com/visionmedia/node-basic-auth).
16+
*
17+
* Enfore basic authentication by providing a `callback(user, pass)`,
18+
* which must return `true` in order to gain access. Alternatively an async
19+
* method is provided as well, invoking `callback(user, pass, callback)`. Populates
20+
* `req.user`. The final alternative is simply passing username / password
21+
* strings.
22+
*
23+
* Simple username and password
24+
*
25+
* connect(connect.basicAuth('username', 'password'));
26+
*
27+
* Callback verification
28+
*
29+
* connect()
30+
* .use(connect.basicAuth(function(user, pass){
31+
* return 'tj' == user && 'wahoo' == pass;
32+
* }))
33+
*
34+
* Async callback verification, accepting `fn(err, user)`.
35+
*
36+
* connect()
37+
* .use(connect.basicAuth(function(user, pass, fn){
38+
* User.authenticate({ user: user, pass: pass }, fn);
39+
* }))
40+
*
41+
* @param {Function|String} callback or username
42+
* @param {String} realm
43+
* @api public
44+
*/
45+
46+
module.exports = function basicAuth(callback, realm) {
47+
var username, password;
48+
49+
// user / pass strings
50+
if ('string' == typeof callback) {
51+
username = callback;
52+
password = realm;
53+
if ('string' != typeof password) throw new Error('password argument required');
54+
realm = arguments[2];
55+
callback = function(user, pass){
56+
return user == username && pass == password;
57+
}
58+
}
59+
60+
realm = realm || 'Authorization Required';
61+
62+
return function(req, res, next) {
63+
var authorization = req.headers.authorization;
64+
65+
if (req.user) return next();
66+
if (!authorization) return unauthorized(res, realm);
67+
68+
var parts = authorization.split(' ');
69+
70+
if (parts.length !== 2) return next(error(400));
71+
72+
var scheme = parts[0]
73+
, credentials = new Buffer(parts[1], 'base64').toString()
74+
, index = credentials.indexOf(':');
75+
76+
if ('Basic' != scheme || index < 0) return next(error(400));
77+
78+
var user = credentials.slice(0, index)
79+
, pass = credentials.slice(index + 1);
80+
81+
// async
82+
if (callback.length >= 3) {
83+
callback(user, pass, function(err, user){
84+
if (err || !user) return unauthorized(res, realm);
85+
req.user = req.remoteUser = user;
86+
next();
87+
});
88+
// sync
89+
} else {
90+
if (callback(user, pass)) {
91+
req.user = req.remoteUser = user;
92+
next();
93+
} else {
94+
unauthorized(res, realm);
95+
}
96+
}
97+
}
98+
};
99+
100+
/**
101+
* Respond with 401 "Unauthorized".
102+
*
103+
* @param {ServerResponse} res
104+
* @param {String} realm
105+
* @api private
106+
*/
107+
108+
function unauthorized(res, realm) {
109+
res.statusCode = 401;
110+
res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
111+
res.end('Unauthorized');
112+
};
113+
114+
/**
115+
* Generate an `Error` from the given status `code`
116+
* and optional `msg`.
117+
*
118+
* @param {Number} code
119+
* @param {String} msg
120+
* @return {Error}
121+
* @api private
122+
*/
123+
124+
function error(code, msg){
125+
var err = new Error(msg || http.STATUS_CODES[code]);
126+
err.status = code;
127+
return err;
128+
};

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "basic-auth-connect",
3+
"description": "Basic auth middleware for node and connect",
4+
"version": "1.0.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/expressjs/basic-auth-connect.git"
15+
},
16+
"bugs": {
17+
"mail": "[email protected]",
18+
"url": "https://github.com/expressjs/basic-auth-connect/issues"
19+
},
20+
"devDependencies": {
21+
"mocha": "*",
22+
"should": "*",
23+
"supertest": "*",
24+
"connect": "*"
25+
},
26+
"scripts": {
27+
"test": "make test"
28+
}
29+
}

0 commit comments

Comments
 (0)