-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
61 lines (49 loc) · 1.94 KB
/
app.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
var express = require('express');
var passport = require('passport');
var db = require('./models');
var api = require('./api');
var BasicStrategy = require('passport-http').BasicStrategy;
// Use the BasicStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, a username and password), and invoke a callback
// with a user object.
passport.use(new BasicStrategy(
function (id, secret, callback) {
db.Account.find( { where: { id: id } } )
.error(function () {
// Callback with error on no user
return callback(new Error('Failed authentication.'));
})
.success(function (user) {
// Callback with error on wrong secret
if (!user) return callback(new Error('Failed authentication.'));
user.authenticate(secret, function (authd) {
if (!authd) return callback(new Error('Failed authentication.'));
// Success
return callback(null, user);
});
});
}
));
var app = express();
// configure Express
app.configure(function() {
app.use(express.json({strict: true}));
app.use(function(req, res, next){
console.log('request: %s %s', req.method, req.url, req.body);
next();
});
app.use(passport.initialize());
app.use(app.router);
});
// curl -v -I http://bob:secret@127.0.0.1:3000/
app.post('/account', api.createAccount);
app.get('/account', passport.authenticate('basic', { session: false }), api.viewAccount);
app.post('/wallets/:wallet/new_address', passport.authenticate('basic', { session: false }), api.newAddress);
app.get('/wallets/:wallet', passport.authenticate('basic', { session: false }), api.view);
app.post('/wallets/:wallet/send', passport.authenticate('basic', { session: false }), api.send);
app.post('/wallets/:wallet/move', passport.authenticate('basic', { session: false }), api.move);
app.listen(3000, function(){
console.log('Express server listening on port ' + 3000);
});