-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·179 lines (152 loc) · 5.27 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
'use strict';
require('dotenv').config();
import express from 'express';
import cookieParser from 'cookie-parser';
import expressSession from 'express-session';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import passport from 'passport';
import config from './config';
import connect from 'connect';
import path from 'path';
// GraphQL
import { GraphQLServer } from 'graphql-yoga';
import { default as resolvers } from './resolvers';
import { default as typeDefs } from './typeDefs';
// GraphQL Options
const opts = {
port: 30662,
endpoint: '/graphql',
subscriptions: '/subscriptions',
playground: '/playground',
cors: {
credentials: true,
origin: ['http://localhost:30662'] // your frontend url.
}
};
// GraphQL Context
const context = req => ({
req: req.request
});
const server = new GraphQLServer({ typeDefs, resolvers, context });
// Azure
var OIDCStrategy = require('passport-azure-ad').OIDCStrategy;
/******************************************************************************
* Set up passport in the app
******************************************************************************/
//-----------------------------------------------------------------------------
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
//-----------------------------------------------------------------------------
passport.serializeUser(function(user, done) {
done(null, user.oid);
});
passport.deserializeUser(function(oid, done) {
findByOid(oid, function(err, user) {
done(err, user);
});
});
// array to hold logged in users
var users = [];
var findByOid = function(oid, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = users[i];
// log.info("we are using user: ", user);
if (user.oid === oid) {
return fn(null, user);
}
}
return fn(null, null);
};
passport.use(
new OIDCStrategy(
{
identityMetadata: config.creds.identityMetadata,
clientID: config.creds.clientID,
responseType: config.creds.responseType,
responseMode: config.creds.responseMode,
redirectUrl: config.creds.redirectUrl,
allowHttpForRedirectUrl: config.creds.allowHttpForRedirectUrl,
clientSecret: config.creds.clientSecret,
validateIssuer: config.creds.validateIssuer,
isB2C: config.creds.isB2C,
issuer: config.creds.issuer,
passReqToCallback: config.creds.passReqToCallback,
scope: config.creds.scope,
loggingLevel: config.creds.loggingLevel,
nonceLifetime: config.creds.nonceLifetime,
nonceMaxAmount: config.creds.nonceMaxAmount,
useCookieInsteadOfSession: config.creds.useCookieInsteadOfSession,
cookieEncryptionKeys: config.creds.cookieEncryptionKeys,
clockSkew: config.creds.clockSkew
},
function(iss, sub, profile, accessToken, refreshToken, done) {
if (!profile.oid) {
return done(new Error('No oid found'), null);
}
// asynchronous verification, for effect...
process.nextTick(function() {
findByOid(profile.oid, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
// "Auto-registration"
users.push(profile);
return done(null, profile);
}
return done(null, user);
});
});
}
)
);
// Forward requests to /api/generate/resume to resumake to handle
var proxy = require('http-proxy-middleware');
server.express.use(
'/api/generate/resume',
proxy({ target: 'http://localhost:3001', changeOrigin: true })
);
server.express.use(require('body-parser').text());
server.express.use(methodOverride());
server.express.use(cookieParser());
server.express.use(connect());
server.express.use(
expressSession({
secret: process.env.EXPRESS_SESSION_SECRET,
resave: true,
saveUninitialized: false
})
);
// body parser stuff required to parse request bodies
server.express.use(bodyParser.urlencoded({ extended: true }));
server.express.use(bodyParser.json({ limit: '50mb' })); // to support JSON-encoded bodies
server.express.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true
})
);
server.express.use(passport.initialize());
server.express.use(passport.session());
server.express.use(express.static('public'));
// need to pass passport to routes to maintain state
const user = require('./routes/user')(passport);
server.express.use(user);
// Serve React build
server.express.use(express.static(path.join(__dirname + '/client/build')));
server.express.get('*', (req, res, next) => {
// Handle graphql-yoga specific routes
if (req.url == opts.playground || req.url == opts.subscriptions || req.url == opts.endpoint) {
// Return next() so that the GraphQLServer will handle it
return next();
}
// If the url matches the logo pattern we have
if (req.url.match(/\/logos\/([1-9A-HJ-NP-Za-km-z]{22}).[a-z]{3}/g)) {
res.sendFile(path.join(__dirname + req.url));
}
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
server.start(opts, () => console.log('Server is running on localhost:30662'));