This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
executable file
·117 lines (99 loc) · 3.21 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
var express = require("express");
var https = require("https");
var fs = require("fs");
var app = express();
var bodyParser = require("body-parser");
var methodOverride = require("method-override");
var basicAuth = require("express-basic-auth");
const session = require("express-session");
var passport = require("passport");
var GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
var morgan = require("morgan");
require("dotenv").config();
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: "application/vnd.api+json" })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded
if (process.env.ENVIRONMENT == "production") {
app.use(morgan("combined"));
} else {
app.use(morgan("tiny"));
}
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: process.env.ENVIRONMENT == "production" }
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user.profile.id);
});
passport.deserializeUser(function(id, done) {
done(null, { profile: { id: id } });
});
if (process.env.GOOGLE_CLIENT_ID) {
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_REDIRECT_URL
},
function(token, refreshToken, profile, done) {
return done(null, {
profile: profile,
token: token
});
}
)
);
app.get("/auth/google", passport.authenticate("google", { scope: ["https://www.googleapis.com/auth/plus.login"] }));
// Support Google OAuth logon
app.get("/auth/google/callback", passport.authenticate("google", { failureRedirect: "/" }), function(req, res) {
console.log("New login: " + req.user.profile.displayName + " " + req.user.profile.id);
req.session.authorized = true;
res.redirect("/");
});
}
// Support password-based logon if APP_PASSWORD is set
app.get("/login", (req, res, next) => {
const b64auth = (req.headers.authorization || "").split(" ")[1] || "";
const [login, password] = new Buffer(b64auth, "base64").toString().split(":");
if (process.env.APP_PASSWORD && password === process.env.APP_PASSWORD) {
req.session.authorized = true;
res.redirect("/");
return;
}
res.set("WWW-Authenticate", 'Basic realm="example"');
res.status(401).send();
});
app.all("*", passport.session(), (req, res, next) => {
if (!req.session.authorized) {
res.redirect("/auth/google");
return;
}
next();
});
app.use(express.static(__dirname + "/client"));
require("./server/routes")(app); // pass application into routes
if (process.env.ENVIRONMENT == "production") {
const privateKey = fs.readFileSync("privkey.pem", "utf8");
const certificate = fs.readFileSync("cert.pem", "utf8");
const ca = fs.readFileSync("chain.pem", "utf8");
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
https.createServer(credentials, app).listen(443, () => {
console.log("Express server listening on port %d.", 443);
});
} else {
app.listen(3000, () => {
console.log("Express server listening on port %d.", 3000);
});
}
module.exports = app;