-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
106 lines (88 loc) · 2.61 KB
/
index.ts
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
import express from "express";
import http from "http";
import { EXPRESS_PORT } from "./config/constants";
import logging from "./config/logging";
import config from "./config/config";
import "./config/passport";
import session from "express-session";
import passport from "passport";
const main = () => {
const app = express();
const httpServer = http.createServer(app);
app.use((req, res, next) => {
logging.info(
`METHOD: [${req.method}] - URL: [${req.url}] - IP: [${req.socket.remoteAddress}]`
);
res.on("finish", () => {
logging.info(
`METHOD: [${req.method}] - URL: [${req.url}] - STATUS: [${res.statusCode}] - IP: [${req.socket.remoteAddress}]`
);
});
next();
});
/** API Rules (Options, CORS etc...) */
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", req.header("origin"));
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.header("Access-Control-Allow-Credentials", "true");
if (req.method == "OPTIONS") {
res.header(
"Access-Control-Allow-Methods",
"PUT, POST, PATCH, DELETE, GET"
);
return res.status(200).json({});
}
next();
});
/** Parse request body */
app.use(session(config.session));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
/** Endpoint to authenticate by Okta using Passport */
app.get(
"/login",
passport.authenticate("saml", config.saml.options),
(_req, res, _next) => {
return res.redirect("http://localhost:3000");
}
);
/** Endpoint called by Okta using Passport */
app.post(
"/login/callback",
passport.authenticate("saml", config.saml.options),
(_req, res, _next) => {
return res.redirect("http://localhost:3000");
}
);
/** Check for user authentication
* If user authenticated return user
*/
app.get("/me", (req, res, _next) => {
if (!req.isAuthenticated()) {
logging.info("User is not authenticated");
return res.status(401).json({
message: "Unauthorized",
});
} else {
logging.info("User is authenticated");
const { user } = req;
logging.info(user);
return res.status(200).json({ user });
}
});
app.use((_req, res, _next) => {
const error = new Error("404 Not found");
res.status(404).json({
message: error.message,
});
});
httpServer.listen(EXPRESS_PORT, () =>
logging.info(`Server is running on port ${EXPRESS_PORT}`)
);
};
main();