(options, checker) => { return require('./auth/basic')(options, checker); }
...
$ npm install http-auth
```
## Basic example
```javascript
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd"
});
// Creating new HTTP server.
http.createServer(basic, (req, res) => {
res.end(`Welcome to private area - ${req.user}!`);
...
(auth) => { return require('./server/connect')(auth); }
...
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd"
});
// Application setup.
var app = express();
app.use(auth.connect(basic));
// Setup route.
app.get('/', (req, res) => {
res.send(`Hello from express - ${req.user}!`);
});
```
...
(options, checker) => { return require('./auth/digest')(options, checker); }
...
const utils = {};
// Generates md5 hash of input.
utils.md5 = (input) => {
let hash = crypto.createHash('MD5');
hash.update(input);
return hash.digest('hex');
};
// Generates sha1 hash of input.
utils.sha1 = (input) => {
let hash = crypto.createHash('sha1');
hash.update(input);
...
() => { return require('./server/hapi'); }
...
});
// Create server.
const server = new Hapi.Server();
server.connection({ port: 1337 });
// Register auth plugin.
server.register(auth.hapi());
// Setup strategy.
server.auth.strategy('http-auth', 'http', basic);
// Setup route.
server.route({
method: 'GET',
...
(auth) => { return require('./server/koa')(auth); }
...
// Final handler.
app.use(function *(next) {
yield next;
this.body = `Hello from koa - ${this.req.user}!`;
});
// Enable auth.
app.use(auth.koa(basic));
```
## For [koa@next](https://github.com/koajs/koa/tree/v2.x) you can use [http-auth-koa](https://github.com/http-auth/http-auth-koa
)
```javascript
// Authentication module.
import auth from 'http-auth'
import koaAuth from 'http-auth-koa'
...
(auth) => { return require('./server/passport')(auth); }
...
});
// Application setup.
var app = express();
// Setup strategy.
var passport = require('passport');
passport.use(auth.passport(basic));
// Setup route.
app.get('/', passport.authenticate('http', {session: false}),
(req, res) => {
res.end(`Welcome to private area - ${req.user}!`);
}
);
...
(plugin, options, next) => { plugin.auth.scheme('http', httpScheme); next(); }
...
});
// Create server.
const server = new Hapi.Server();
server.connection({ port: 1337 });
// Register auth plugin.
server.register(auth.hapi());
// Setup strategy.
server.auth.strategy('http-auth', 'http', basic);
// Setup route.
server.route({
method: 'GET',
...
(options, checker) => { return require('./auth/basic')(options, checker); }
...
$ npm install http-auth
```
## Basic example
```javascript
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd"
});
// Creating new HTTP server.
http.createServer(basic, (req, res) => {
res.end(`Welcome to private area - ${req.user}!`);
...
(auth) => { return require('./server/connect')(auth); }
...
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd"
});
// Application setup.
var app = express();
app.use(auth.connect(basic));
// Setup route.
app.get('/', (req, res) => {
res.send(`Hello from express - ${req.user}!`);
});
```
...
(options, checker) => { return require('./auth/digest')(options, checker); }
...
const utils = {};
// Generates md5 hash of input.
utils.md5 = (input) => {
let hash = crypto.createHash('MD5');
hash.update(input);
return hash.digest('hex');
};
// Generates sha1 hash of input.
utils.sha1 = (input) => {
let hash = crypto.createHash('sha1');
hash.update(input);
...
() => { return require('./server/hapi'); }
...
});
// Create server.
const server = new Hapi.Server();
server.connection({ port: 1337 });
// Register auth plugin.
server.register(auth.hapi());
// Setup strategy.
server.auth.strategy('http-auth', 'http', basic);
// Setup route.
server.route({
method: 'GET',
...
(auth) => { return require('./server/koa')(auth); }
...
// Final handler.
app.use(function *(next) {
yield next;
this.body = `Hello from koa - ${this.req.user}!`;
});
// Enable auth.
app.use(auth.koa(basic));
```
## For [koa@next](https://github.com/koajs/koa/tree/v2.x) you can use [http-auth-koa](https://github.com/http-auth/http-auth-koa
)
```javascript
// Authentication module.
import auth from 'http-auth'
import koaAuth from 'http-auth-koa'
...
(auth) => { return require('./server/passport')(auth); }
...
});
// Application setup.
var app = express();
// Setup strategy.
var passport = require('passport');
passport.use(auth.passport(basic));
// Setup route.
app.get('/', passport.authenticate('http', {session: false}),
(req, res) => {
res.end(`Welcome to private area - ${req.user}!`);
}
);
...
(input) => { return new Buffer(input, 'utf8').toString('base64'); }
n/a
(input) => { return new Buffer(input, 'base64').toString('utf8'); }
...
return tokens[1];
}
}
// Searching for user.
findUser(req, hash, callback) {
// Decode base64.
let splitHash = utils.decodeBase64(hash).split(":");
let username = splitHash.shift();
let password = splitHash.join(":");
let self = this;
if (this.checker) {
// Custom auth.
...
(path) => { try { return !!require.resolve(path); } catch (err) { return false; } }
...
// http integration.
require('./server/http');
// https integration.
require('./server/https');
// http-proxy integration.
if (utils.isAvailable('http-proxy')) {
require('./server/proxy');
}
// Exports.
module.exports = {
// Basic authentication.
basic: (options, checker) => {
...
(input) => { let hash = crypto.createHash('MD5'); hash.update(input); return hash.digest('hex'); }
...
// Validating hash.
validate(ha2, co, hash) {
let ha1 = hash;
// Algorithm.
if (co.algorithm === 'MD5-sess') {
ha1 = utils.md5(`${ha1}:${co.nonce}:${co.cnonce}`);
}
let response = undefined;
// Quality of protection.
if (co.qop) {
response = utils.md5(`${ha1}:${co.nonce}:${co.nc}:${co.cnonce}:${co.qop}:${ha2}`);
...
(input) => { let hash = crypto.createHash('sha1'); hash.update(input); return hash.digest('base64'); }
...
super(options, checker);
}
// Verifies if password is correct.
validate (hash, password) {
if (hash.substr(0, 5) === '{SHA}') {
hash = hash.substr(5);
return hash === utils.sha1(password);
} else if (hash.substr(0, 6) === '$apr1$' || hash.substr(0, 3) === '$1$') {
return hash === md5(password, hash);
} else if (hash.substr(0, 4) === '$2y$' || hash.substr(0, 4) === '$2a$') {
return bcrypt.compareSync(password, hash);
} else if (hash === crypt(password, hash)) {
return true;
} else if (hash.length === password.length) {
...