|
13 | 13 | * permissions and limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
16 | | -const { runServer } = require('saml-idp'); |
17 | | - |
| 16 | +const express = require('express'); |
| 17 | +const bodyParser = require('body-parser'); |
| 18 | +const minimist = require('minimist'); |
18 | 19 | const { generate } = require('selfsigned'); |
| 20 | +const { SAML } = require('@node-saml/node-saml'); |
19 | 21 |
|
20 | | -const minimist = require('minimist'); |
| 22 | +const argv = minimist(process.argv.slice(2), { |
| 23 | + default: { basePath: '' }, |
| 24 | +}); |
21 | 25 |
|
22 | | -const pems = generate(null, { |
| 26 | +const pems = generate([{ name: 'commonName', value: 'Test Identity Provider' }], { |
23 | 27 | keySize: 2048, |
24 | 28 | clientCertificateCN: '/C=US/ST=California/L=San Francisco/O=JankyCo/CN=Test Identity Provider', |
25 | 29 | days: 7300, |
26 | 30 | }); |
27 | 31 |
|
28 | | -const argv = minimist(process.argv.slice(2), { |
29 | | - default: { basePath: '' }, |
| 32 | +// configure the SAML client (Service Provider) |
| 33 | +const saml = new SAML({ |
| 34 | + // Dashboards SAML ACS URL |
| 35 | + callbackUrl: `http://localhost:5601${argv.basePath}/_opendistro/_security/saml/acs`, |
| 36 | + // the IdP redirect endpoint (you’ll register this SP with them ahead of time) |
| 37 | + entryPoint: 'https://your-idp.example.com/saml2/idp/SSOService.php', |
| 38 | + // the SP entityID (Audience URI) |
| 39 | + issuer: 'https://localhost:9200', |
| 40 | + // SP signing key & cert |
| 41 | + privateKey: pems.private.toString(), |
| 42 | + publicCert: pems.cert, |
| 43 | + // validate the IdP’s signature on the Response |
| 44 | + idpCert: pems.cert, |
| 45 | + audience: 'https://localhost:9200', |
| 46 | + skipRequestCompression: true, |
30 | 47 | }); |
31 | 48 |
|
32 | | -// Create certificate pair on the fly and pass it to runServer |
33 | | -runServer({ |
34 | | - acsUrl: `http://localhost:5601${argv.basePath}/_opendistro/_security/saml/acs`, |
35 | | - audience: 'https://localhost:9200', |
36 | | - cert: pems.cert, |
37 | | - key: pems.private.toString().replace(/\r\n/, '\n'), |
| 49 | +const app = express(); |
| 50 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 51 | + |
| 52 | +// Initiate SSO by redirecting the browser to the IdP |
| 53 | +app.get('/login', (req, res, next) => { |
| 54 | + saml.getAuthorizeUrl(req, (err, url) => { |
| 55 | + if (err) return next(err); |
| 56 | + res.redirect(url); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +// Consume the SAMLResponse at ACS endpoint |
| 61 | +app.post(`/_opendistro/_security/saml/acs`, (req, res, next) => { |
| 62 | + const { SAMLResponse, RelayState } = req.body; |
| 63 | + saml.validatePostResponse({ SAMLResponse, RelayState }, (err, profile, logout) => { |
| 64 | + if (err) return next(err); |
| 65 | + res.send(`SAML login successful for ${profile.nameID}`); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +const port = 5601; |
| 70 | +app.listen(port, () => { |
| 71 | + console.log(`SAML SP listening on http://localhost:${port}`); |
38 | 72 | }); |
| 73 | + |
0 commit comments