-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
179 lines (154 loc) · 5.29 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const request = require('request');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
var passport = require('passport');
const passportConfig = require('./passport-config');
const config = require('./predix-config');
const RedisStore = require('connect-redis')(session);
const redis = require('redis');
const redisClient = redis.createClient(config.redisStore);
redisClient.on('error', function(err){
console.log('Error '+ err);
})
const appRoutes = require('./routes/app');
const dataviewRoutes = require('./routes/dataview');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'POST, PUT, GET, PATCH, DELETE, OPTIONS');
next();
});
passport = passportConfig.configurePassportStrategy();
app.use(session({
store: new RedisStore({
url: config.redisStore.url,
client: redisClient,
ttl: 43199
}),
secret: config.redisStore.password ? config.redisStore.password : 'password',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
// Also use passport.session() middleware, to support persistent login sessions (recommended).
app.use(passport.session());
app.use('/', appRoutes);
app.use('/dataview', dataviewRoutes);
app.get('/signin', passport.authenticate('predix', {'scope': ''}), function(req, res) {
// The request will be redirected to Predix for authentication, so this
// function will not be called.
});
app.get('/signin/callback', function(req, res, next) {
passport.authenticate('predix', function(err, user, info) {
if(err) { return next(err); }
if(!user) { return res.redirect('/#/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
const sid = req.cookies["connect.sid"] ? req.cookies["connect.sid"] : '';
if(existsInRedis(sid)) {
redisClient.hset(sid, 'token', user.ticket.access_token);
redisClient.expireat(sid, user.exp);
}
redisClient.hset(user.user_name, 'scope', user.scope.toString());
return res.redirect(303, '/#/materials?sid=' + sid);
});
})(req, res, next);
});
app.get('/signin/token', function(req, res, next) {
var sid = req.query.sid ? req.query.sid : '';
if(sid == '') {
return res.json({
token: ''
});
}
if(existsInRedis(sid)){
redisClient.hget(sid, 'token', function(err, value) {
if(err) {
res.json({
error: err
})
}
res.json({
token: value
})
})
} else {
res.redirect('/#/login');
}
return res;
});
app.get('/logout', function(req, res) {
var sid = req.query.sid ? req.query.sid : '';
if(req.session) {
req.session.destroy();
}
if(existsInRedis(sid)){
redisClient.del(sid, 'token');
}
req.logout();
passportConfig.reset();
res.redirect(config.uaaURL + '/logout?redirect=' + config.appURL);
})
app.get('/isauthenticated', function (req, res) {
var token = req.query.token ? req.query.token : '';
if(token == '') {
res.status = 401;
return res.json('Can not find token.')
}
if(!config.clientId || !config.clientSecret) {
res.status = 401;
return res.json('Can not find client credentials.')
}
const base64Credentials = Buffer.from(config.clientId + ':' + config.clientSecret).toString('base64');
const headers = {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Basic ' + base64Credentials
}
const options = {
url: config.uaaURL + '/check_token?token=' + token,
method: 'POST',
headers: headers
}
return request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
return res.json({
isAuthenticated : true,
body : JSON.parse(body)
})
}
if(!error && response.statusCode == 400) {
return res.json({
isAuthenticated: false,
body : JSON.parse(error)
});
}
return res.json(error);
})
})
var existsInRedis = function(key) {
return redisClient.exists(key, function(err, reply) {
if(reply == 0)
return false;
});
}
// catch 404 and forward to error handler
app.use(function (req, res, next) {
return res.render('index');
});
module.exports = app;