-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
executable file
·104 lines (85 loc) · 3.74 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
/*
Copyright 2019 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const appID = require("ibmcloud-appid");
const WebAppStrategy = appID.WebAppStrategy;
const app = express();
const CALLBACK_URL = "/ibm/cloud/appid/callback";
const port = process.env.PORT || 3000;
// Setup express application to use express-session middleware
// Must be configured with proper session storage for production
// environments. See https://github.com/expressjs/session for
// additional documentation
app.use(session({
secret: "123456",
resave: true,
saveUninitialized: true,
proxy: true
}));
// Configure express application to use passportjs
app.use(passport.initialize());
app.use(passport.session());
let webAppStrategy = new WebAppStrategy(getAppIDConfig());
passport.use(webAppStrategy);
// Configure passportjs with user serialization/deserialization. This is required
// for authenticated session persistence accross HTTP requests. See passportjs docs
// for additional information http://passportjs.org/docs
passport.serializeUser((user, cb) => cb(null, user));
passport.deserializeUser((obj, cb) => cb(null, obj));
// Callback to finish the authorization process. Will retrieve access and identity tokens/
// from AppID service and redirect to either (in below order)
// 1. the original URL of the request that triggered authentication, as persisted in HTTP session under WebAppStrategy.ORIGINAL_URL key.
// 2. successRedirect as specified in passport.authenticate(name, {successRedirect: "...."}) invocation
// 3. application root ("/")
app.get(CALLBACK_URL, passport.authenticate(WebAppStrategy.STRATEGY_NAME, { failureRedirect: '/error', session: false }));
// Protect everything under /protected
app.use("/protected", passport.authenticate(WebAppStrategy.STRATEGY_NAME, { session: false }));
// This will statically serve pages:
app.use(express.static("public"));
// // This will statically serve the protected page (after authentication, since /protected is a protected area):
app.use('/protected', express.static("protected"));
app.get("/logout", (req, res) => {
//Note: if you enabled SSO for Cloud Directory be sure to use webAppStrategy.logoutSSO instead.
req._sessionManager = false;
WebAppStrategy.logout(req);
res.clearCookie("refreshToken");
res.redirect("/");
});
//Serves the identity token payload
app.get("/protected/api/idPayload", (req, res) => {
res.send(req.session[WebAppStrategy.AUTH_CONTEXT].identityTokenPayload);
});
app.get('/error', (req, res) => {
res.send('Authentication Error');
});
app.listen(port, () => {
console.log("Listening on http://localhost:" + port);
});
function getAppIDConfig() {
let config;
try {
// if running locally we'll have the local config file
config = require('./localdev-config.json');
} catch (e) {
if (process.env.APPID_SERVICE_BINDING) { // if running on Kubernetes this env variable would be defined
config = JSON.parse(process.env.APPID_SERVICE_BINDING);
config.redirectUri = process.env.redirectUri;
} else { // running on CF
let vcapApplication = JSON.parse(process.env["VCAP_APPLICATION"]);
return { "redirectUri": "https://" + vcapApplication["application_uris"][0] + CALLBACK_URL };
}
}
return config;
}