-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (142 loc) · 5.71 KB
/
index.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
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
const passportSaml = require('passport-saml');
const fs = require('fs');
const _ = require('lodash');
const psm = require('passport-saml-metadata');
const path = require('path');
const URL = require('url').URL;
module.exports = {
extend: 'apostrophe-module',
// Build up our options dynamically before the parent class sees them.
afterConstruct: function (self, callback) {
self.enablePassportStrategy();
self.generateMetadata();
self.addRoutes();
return callback(null);
},
construct: async function (self, options) {
const salesforceExperience = self.apos.modules['apostrophe-salesforce-experience'];
const sfExpOptions = salesforceExperience.options;
let portalUrl;
if (!sfExpOptions.adminLoginUrl ||
!sfExpOptions.adminPasswordAndToken ||
!sfExpOptions.adminUsername) {
self.apos.utils.error('⚠️ Salesforce admin credentials incomplete.');
sfExpOptions.missingLogin = true;
}
self.enablePassportStrategy = function () {
// This is the Salesforce identity provider's metadata, not ours.
const confFolder = _.last(self.__meta.chain).dirname;
const reader = new psm.MetadataReader(fs.readFileSync(`${confFolder}/salesforce-metadata.xml`, 'utf8'));
let config = psm.toPassportConfig(reader);
config.decryptionCert = fs.readFileSync(`${confFolder}/connect.cer`, 'utf8');
config.decryptionPvk = fs.readFileSync(`${confFolder}/connect.key`, 'utf8');
// Match signatureAlgorithm to how certs were made (see README).
config.signatureAlgorithm = 'sha256';
// Issuer must be unique to this site. It's common practice to use the
// URL of our metadata (which doesn't have to be published like this, but
// it's standard practice and doesn't hurt anything).
config.issuer = self.getIssuer();
// Without this it looks for emailAddress, which is not available
config.identifierFormat = null;
// passport-saml uses entryPoint, not identityProviderUrl
config.entryPoint = config.identityProviderUrl;
config.callbackUrl = sfExpOptions.callbackUrl || (`${options.apos.options.baseUrl}/auth/saml/login/callback`);
// Add our extra passportSamlOptions into our config object
config = self.addPassportSamlOptions(config);
const strategy = new passportSaml.Strategy(config, self.profileCallback);
self.strategy = strategy;
self.apos.login.passport.use(strategy);
};
self.generateMetadata = function () {
const confFolder = _.last(self.__meta.chain).dirname;
const metadata = self.strategy.generateServiceProviderMetadata(fs.readFileSync(`${confFolder}/connect.cer`, 'utf8'));
fs.writeFileSync(`${self.apos.rootDir}/public/${path.basename(self.getIssuer())}`, metadata);
};
self.getIssuer = function () {
return sfExpOptions.issuer || (`${options.apos.options.baseUrl}/saml-metadata.xml`);
};
self.getLoginPath = function () {
if (sfExpOptions.loginUrl) {
const loginUrl = new URL(sfExpOptions.loginUrl);
return loginUrl.pathname;
} else {
return '/auth/saml/login';
}
};
self.getPortalUrl = function () {
return portalUrl;
};
self.getCallbackPath = function () {
if (sfExpOptions.callbackUrl) {
const callbackUrl = new URL(sfExpOptions.callbackUrl);
return callbackUrl.pathname;
} else {
return '/auth/saml/login/callback';
}
};
self.addPassportSamlOptions = function (config) {
// Merge the base configuration options into the
// passportSamlOptionsObject.
// Note: If you have the same attribute in both objects, the base
// configuration option will overwrite the passportSamlOptions
// attribute.
return Object.assign({}, sfExpOptions.passportSamlOptions, config);
};
self.addRoutes = function () {
self.apos.app.get(
self.getLoginPath(),
self.apos.login.passport.authenticate('saml'),
function (req, res) {
// TODO: Is this needed?
res.redirect('/');
}
);
self.apos.app.post(
self.getCallbackPath(),
function (req, res, next) {
return self.apos.login.passport.authenticate(
'saml',
async function (err, user, info, status) {
if (err) {
return next(err);
}
if (!user) {
return next('No user authenticated.');
}
const sfe = self.apos.modules['apostrophe-salesforce-experience'];
await sfe.completeProfile(user);
req.session.sfUser = user;
req.session.sfExperiences = await sfe.getExperiences(user.AccountId);
if (req.session.sfUser.issuer) {
portalUrl = req.session.sfUser.issuer._;
}
if (sfExpOptions.redirectionUrl) {
return res.redirect(sfExpOptions.redirectionUrl);
}
return res.redirect('/');
}
)(req, res, next);
}
);
self.apos.app.get(
'/auth/saml/logout',
function (req, res) {
req.session.sfUser = undefined;
req.session.sfExperiences = undefined;
const portalBaseUrl = self.getPortalUrl();
if (portalBaseUrl) {
return res.redirect(`${portalBaseUrl}/secur/logout.jsp`);
}
return res.redirect('/');
}
);
self.apos.on('csrfExceptions', function (list) {
list.push(self.getCallbackPath());
});
};
self.profileCallback = function (profile, callback) {
// Returning (no error, user) since we're not logging in a user.
return callback(null, profile);
};
}
};