-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
176 lines (147 loc) · 3.9 KB
/
server.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
/* eslint-disable no-console */
require('es6-promise').polyfill()
require('isomorphic-fetch')
const express = require('express')
const app = express()
const session = require('express-session')
const RedisStore = require('connect-redis')(session)
const path = require('path')
const passport = require('passport')
const OAuth2Strategy = require('passport-oauth2').Strategy
const bodyParser = require('body-parser')
const fs = require('fs')
const config = require('./config')
/*
AUTH
*/
function getSessionConfig() {
let sessionInfo = {
secret: config.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}
if (config.SESSION_STORE === config.SessionStoreType.REDIS) {
sessionInfo.store = new RedisStore({
socket: config.REDIS_SOCKET,
})
}
if (app.get('env') === 'production') {
app.set('trust proxy', 1)
sessionInfo.cookie = {
secure: true
}
}
return sessionInfo
}
app.use(bodyParser.json())
app.use(session(getSessionConfig()))
app.use(passport.initialize())
app.use(passport.session())
passport.serializeUser(function(user, done) {
done(null, user)
})
passport.deserializeUser(function(user, done) {
done(null, user)
})
const oauth2Strategy = new OAuth2Strategy(
{
authorizationURL: config.OAUTH2_AUTHORIZATION_URL,
tokenURL: config.OAUTH2_TOKEN_URL,
clientID: config.OAUTH2_CLIENT_ID,
clientSecret: config.OAUTH2_CLIENT_SECRET,
callbackURL: config.OAUTH2_CALLBACK_URL,
},
(accessToken, refreshToken, profile, done) => {
const user = {
accessToken,
refreshToken,
}
done(null, user)
}
)
passport.use(oauth2Strategy)
app.get('/auth/log-in', passport.authenticate('oauth2'))
app.get('/auth/callback',
passport.authenticate('oauth2', {failureRedirect: '/'}),
(req, res) => {
res.redirect('/')
}
)
app.get('/auth/log-out', function(req, res) {
if (isUserLoggedIn(req)) {
req.session.destroy()
}
res.redirect('/')
})
app.get('/auth/refresh-token', function(req, res) {
if (!isUserLoggedIn(req)) {
res.redirect('/auth/log-in')
return
}
oauth2Strategy._oauth2.getOAuthAccessToken(
req.session.passport.user.refreshToken,
{ grant_type: 'refresh_token' },
function(error, accessToken, refreshToken) {
if (error) {
res.redirect('/auth/log-out')
return
}
req.session.passport.user.accessToken = accessToken
req.session.passport.user.refreshToken = refreshToken
res.redirect('/')
}
)
})
function isUserLoggedIn(req) {
const passportSession = req.session.passport
return passportSession && passportSession.user
}
/*
PROXY
*/
app.post('/proxy/graphql', function (req, res) {
if (!isUserLoggedIn(req)) {
res.status(401).send('You need to be logged in!')
return
}
const token = `Bearer ${req.session.passport.user.accessToken}`
const query = JSON.stringify(req.body)
fetch(config.GRAPHQL_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': token,
},
body: query,
}).then(function (response) {
const isUnauthorized = response.status === 401
if (isUnauthorized && isUserLoggedIn(req)) {
res.redirect('/auth/refresh-token')
return
}
response.json()
.then(jsonBody => res.send(jsonBody))
.catch(error => res.send(error))
})
})
/*
INDEX
*/
app.get('/', function (req, res) {
fs.readFile(path.join(__dirname, '/index.html'), function (err, content) {
if (err)
return res.send(err)
content = content.toString()
.replace('{{signedOutClass}}', isUserLoggedIn(req) ? 'signed-in' : 'signed-out')
return res.send(content)
})
})
app.use(function(req, res) {
res.status(404).send('Page not found!')
})
app.listen(config.APP_PORT, function () {
console.log(`Example app listening on port ${config.APP_PORT}!`)
})
// required to use the server on the tests
module.exports = app