-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (137 loc) · 3.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var express = require('express');
var app = express();
var session = require('express-session')
var basicAuth = require('basic-auth-connect');
var bodyParser = require('body-parser');
var db = require('./server/db');
var store = require('./server/util/store');
var permission = require('./server/permission');
var passphraseStore = require('./server/passphrase');
var token_generator = require('./server/token');
var ApiKeyService = require('./server/auth/apikey');
var config = require('./config');
var providers = {
facebook : require('./server/auth/facebook')
}
var apikey = new ApiKeyService();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json())
app.use(session({secret: 'authapisecret'}));
app.use(function(req, res, next) {
var accept = req.headers['accept'] || "application/json";
var accept2 = accept.split(';').reduce(function(acc, a) {
return a.split(',').concat(acc);
}, []);
req.accept = accept2;
next(null);
})
/*
管理画面
login
*/
app.get('/auth/apikey/token/:key/:secret', function (req, res) {
var key = req.param('key');
var secret = req.param('secret');
apikey.auth(key, secret, function(err, data) {
get_token(req, res, err, data.name)
});
});
function get_token(req, res, err, sub) {
if(err) {
response(err);
return;
}
token_generator.generate(sub, response);
function response(err, token) {
if(req.accept.indexOf('application/json') >= 0) {
if(err) {
res.json({err:err});
return;
}
res.json({err:null, content : {
token:token
}});
}else{
if(err) {
res.status(500).send(err);
return;
}
res.send(token);
}
}
}
app.get('/auth/:provider/dialog', function (req, res) {
var provider = req.param('provider');
providers[provider].dialog(req, res);
});
app.get('/auth/:provider/callback', function (req, res) {
var provider = req.param('provider');
providers[provider].callback(req, function(err, sub) {
if(err) {
send({
err : err.message || err
});
return;
}
token_generator.generate(sub, function(err, token) {
if(err) {
send({
err : err.message || err
});
return;
}
send({
err : null,
content : token
});
});
});
function send(data) {
var html = '<script>window.opener.postMessage('+JSON.stringify(data)+', "'+config.origin+'");window.close();</script>';
res.send(html);
}
});
app.get('/', function (req, res) {
res.redirect('/dev');
});
app.all('/dev/*', basicAuth('username', 'password'));
app.get('/dev/passphrase', function (req, res) {
passphraseStore.get(result(res));
});
app.post('/dev/passphrase', function (req, res) {
var passphrase = req.param('passphrase');
passphraseStore.set(passphrase, result(res));
});
app.post('/dev/apikey', function (req, res) {
var name = req.body.name;
apikey.add(name, result(res));
});
app.delete('/dev/apikey/:key', function (req, res) {
var key = req.param('key');
apikey.remove(key, result(res));
});
app.get('/dev/apikey', function (req, res) {
var skip = req.body.skip || 0;
var limit = req.body.limit || 20;
apikey.list(skip, limit, result(res));
});
app.use('/dev', express.static('public'));
function result(res) {
return function(err, content) {
if(err) {
res.json({err:err.message || err});
return;
}
res.json({err:null, content:content});
}
}
db.open(process.env.MONGO_URI || process.env.MONGOLAB_URI || 'mongodb://localhost:27017/mlkccaauth', function(err, _db) {
if(err) throw err;
store.setDb(db);
apikey.setDb(db);
token_generator.setPassphrase(config.passphrase);
app.listen(process.env.PORT || 3000);
});
module.exports = app;