forked from go-hse/node-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
193 lines (148 loc) · 5.06 KB
/
app.js
File metadata and controls
193 lines (148 loc) · 5.06 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
20.10.2020 (43) 16:16:29
Example-App for ID-Management with
Keycloak: ID Provider
Node.js-App: Service Provider
Using the SAML-Protocol
derived from
https://github.com/austincunningham/keycloak-express
which uses openid-connect
*/
'use strict';
const fs = require('fs');
const path = require('path');
const express = require('express');
const session = require('express-session');
const expressHbs = require('express-handlebars');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res, next) {
console.log('Time: %d', Date.now(), req.method, req.url); // , req.body, req.params
next()
});
// Register 'handelbars' extension with The Mustache Express
app.engine('hbs', expressHbs({
extname: 'hbs',
defaultLayout: 'layout.hbs',
relativeTo: __dirname
}));
app.set('view engine', 'hbs');
let memoryStore = new session.MemoryStore();
// session
app.use(session({
secret: 'thisShouldBeLongAndSecret',
resave: false,
saveUninitialized: true,
store: memoryStore
}));
// unprotected route
app.get('/', function (req, res) {
res.render('index');
});
app.use( express.static( path.resolve( __dirname, "public" ) ) );
const NODE_PORT = 8100
let http = require('http').createServer(app);
http.listen(NODE_PORT, () => {
console.log('listening on *:', NODE_PORT);
});
/////////////////////////////////////////////////////////////////////////////80
// Socket.IO Connection
let socket_io = require( 'socket.io' )( http );
function IoConnectServer(io_api, callbacks) {
let uniqueID = 0;
let no_of_clients = 0;
io_api.on( 'connection', ( socket ) => {
let currentClientID = ++uniqueID;
++no_of_clients;
socket.emit( 'init', {id: currentClientID, no_of_clients});
socket.on( 'disconnect', function() {
--no_of_clients;
} );
for ( let cbName in callbacks ) {
let cb = callbacks[ cbName ];
socket.on( cbName, ( o ) => {
cb( socket, currentClientID, o );
} );
}
});
}
let callbacks = {
open: function( socket, id, o ) {
console.log( "open connection to client", id, o );
},
update: function( socket, id, o ) {
socket.broadcast.emit("update", o); // send to all others
}
};
IoConnectServer(socket_io, callbacks);
/////////////////////////////////////////////////////////////////////////////80
const saml = require('passport-saml');
const passport = require('passport');
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.use(passport.initialize());
app.use(passport.session());
const CALLBACK_URL = `http://localhost:${NODE_PORT}/login/callback`
const ISSUER = "node"
// passed as option to docker
const ENTRY_POINT = "http://localhost:8080/auth/realms/hse/protocol/saml"
const publicKey = fs.readFileSync(__dirname + '/certs/server.crt', 'utf8');
const privateKey = fs.readFileSync(__dirname + '/certs/key.pem', 'utf8');
const saml_options = {
callbackUrl: CALLBACK_URL,
issuer: ISSUER,
entryPoint: ENTRY_POINT,
identifierFormat: null,
// The decryptionPvK and privateCert both refer to the local private key
// downloaded from keycloak - client - SAML keys - export (format: pks12)
privateCert: privateKey,
decryptionPvk: privateKey,
// IDP public key from the servers meta data
cert: fs.readFileSync(__dirname + '/certs/idp_cert.pem', 'utf8'),
validateInResponseTo: false,
disableRequestedAuthnContext: true,
}
function saml_callback(profile, done) {
console.log('Parsing SAML', profile);
const user = {
email: profile.email,
group: profile.eduPersonAffiliation
};
return done(null, user);
}
const samlStrategy = new saml.Strategy(saml_options, saml_callback);
passport.use('samlStrategy', samlStrategy);
//
app.get('/login', passport.authenticate('samlStrategy', { successRedirect: '/saml', failureRedirect: '/auth/fail' }));
app.use('/saml', (req, res, next) => {
if (req.isAuthenticated()) res.render('saml', { title: 'Logged in' });
else res.redirect('/');
});
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.post('/login/callback', passport.authenticate('samlStrategy', { failureRedirect: '/auth/fail' }), (req, res, next) => {
console.log('SSO Login ################', req.user);
res.redirect('/saml');
/*
for (let k in req) {
if (typeof req[k] !== "function") {
console.log(k, "::", req[k]);
}
}
*/
});
const metadata = samlStrategy.generateServiceProviderMetadata(publicKey, publicKey);
app.get('/metadata',
function (req, res) {
res.type('application/xml');
res.status(200).send(metadata);
}
);