forked from bitfinexcom/nodebb-plugin-write-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (46 loc) · 1.6 KB
/
index.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
'use strict';
/* globals module, require */
var passport = module.parent.require('passport'),
BearerStrategy = require('passport-http-bearer').Strategy,
meta = require.main.require('./src/meta'),
auth = require('./lib/auth'),
sockets = require('./lib/sockets'),
API = {};
API.init = function(data, callback) {
// API Versions
var routes = require('./routes')(data.middleware);
data.router.use('/api/v1', routes.v1);
data.router.use('/api/v2', routes.v2);
// Set up HTTP bearer authentication via Passport
passport.use(new BearerStrategy({}, function(token, done) {
// Find the user by token. If there is no user with the given token, set
// the user to `false` to indicate failure. Otherwise, return the
// authenticated `user`. Note that in a production-ready application, one
// would want to validate the token for authenticity.
auth.verifyToken(token, done);
}));
require('./routes/admin')(data.router, data.middleware); // ACP
sockets.init(); // WebSocket listeners
API.reloadSettings();
callback();
};
API.addMenuItem = function(custom_header, callback) {
custom_header.plugins.push({
route: '/plugins/write-api',
icon: 'fa-cogs',
name: 'Write API'
});
callback(null, custom_header);
};
API.authenticate = function(data) {
require('./routes/v2/middleware').requireUser(data.req, data.res, data.next);
};
API.associateUser = require('./routes/v2/middleware').associateUser;
API.reloadSettings = function(hash) {
if (!hash || hash === 'settings:writeapi') {
meta.settings.get('writeapi', function(err, settings) {
API.settings = settings;
});
}
};
module.exports = API;