forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamlProvider.ts
More file actions
158 lines (144 loc) · 6.74 KB
/
Copy pathSamlProvider.ts
File metadata and controls
158 lines (144 loc) · 6.74 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
import * as winston from "winston";
import * as express from "express";
// import * as passport from "passport";
import * as samlp from "samlp";
import { Config } from "./Config";
import { TokenUser } from "./TokenUser";
import { Audit } from "./Audit";
export class SamlProvider {
private static _logger: winston.Logger;
// private static app:express.Express;
public static profileMapper(pu: any): any {
return {
pu: pu,
getClaims: function (): any {
var claims: any = {};
var k: string[] = Object.keys(this.pu);
k.forEach(key => {
if (key.indexOf("http://") === 0) {
claims[key] = this.pu[key];
} else {
switch (key) {
case "id":
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"] = this.pu[key]; break;
case "displayName":
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"] = this.pu[key]; break;
case "name":
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"] = this.pu[key]; break;
case "username":
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"] = this.pu[key]; break;
case "emails":
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"] = this.pu[key][0];
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"] = this.pu[key][0]; break;
case "roles":
var roles: string[] = [];
this.pu[key].forEach(role => {
roles.push(role.name);
});
claims["http://schemas.xmlsoap.org/claims/Group"] = roles;
}
}
});
return claims;
},
getNameIdentifier: function (): any {
var claims: any = this.getClaims();
return {
nameIdentifier: claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"] ||
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"] ||
claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]
};
}
};
}
static configure(logger: winston.Logger, app: express.Express, baseurl: string): void {
this._logger = logger;
var cert: string = Buffer.from(Config.signing_crt, "base64").toString("ascii");
var key: string = Buffer.from(Config.singing_key, "base64").toString("ascii");
var samlpoptions: any = {
issuer: Config.saml_issuer,
cert: cert,
key: key,
getPostURL: (wtrealm: any, wreply: any, req: any, callback: any) => {
(async () => {
if (typeof wreply === "object") {
wreply = wreply.documentElement.getAttribute("AssertionConsumerServiceURL");
}
return callback(null, wreply);
})();
},
getUserFromRequest: (req: any) => {
var tuser: TokenUser = new TokenUser(req.user);
var remoteip = "";
if (req.connection) { remoteip = req.connection.remoteAddress; }
Audit.LoginSuccess(tuser, "tokenissued", "saml", remoteip);
return req.user;
},
profileMapper: SamlProvider.profileMapper
};
app.get("/issue/", (req: any, res: any, next: any): void => {
// passport.authenticate("session");
if (req.query.SAMLRequest !== undefined && req.query.SAMLRequest !== null) {
if ((req.user === undefined || req.user === null)) {
try {
// tslint:disable-next-line: max-line-length
samlp.parseRequest(req, samlpoptions, async (_err: any, samlRequestDom: any): Promise<void> => {
res.cookie("originalUrl", req.originalUrl, { maxAge: 900000, httpOnly: true });
res.redirect("/");
});
} catch (error) {
res.body(error.message);
res.end();
console.error(error);
}
} else {
// continue with issuing token using samlp
next();
}
} else {
res.send("go away!");
res.end();
}
});
app.get("/issue/", samlp.auth(samlpoptions));
app.get("/issue/FederationMetadata/2007-06/FederationMetadata.xml", samlp.metadata({
issuer: Config.saml_issuer,
cert: cert,
}));
// var SessionParticipants = require('samlp/lib/sessionParticipants');
// https://github.com/mcguinness/saml-idp/blob/master/app.js
// https://www.diycode.cc/projects/auth0/node-samlp
// https://github.com/auth0/node-samlp/blob/master/lib/sessionParticipants/index.js
// app.get('/logout', samlp.logout({
// deflate: true,
// issuer: 'the-issuer',
// protocolBinding: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
// cert: cert,
// key: key
// }));
// TODO: FIX !!!!
app.get('/logout', (req: any, res: any, next: any): void => {
var referer: string = req.headers.referer;
req.logout();
if (referer !== null && referer !== undefined && referer !== "") {
res.redirect(referer);
} else {
res.redirect("/");
}
// samlp.logout({
// issuer: Config.saml_issuer,
// protocolBinding: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
// cert: cert,
// key: key
// })(req, res, next);
});
app.post('/logout', (req: any, res: any, next: any): void => {
samlp.logout({
issuer: Config.saml_issuer,
protocolBinding: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
cert: cert,
key: key
})(req, res, next);
});
}
}