forked from mcguinness/saml-idp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
1211 lines (1116 loc) · 41.6 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Module dependencies.
*/
const express = require('express'),
_ = require('underscore'),
os = require('os'),
fs = require('fs'),
http = require('http'),
https = require('https'),
path = require('path'),
extend = require('extend'),
hbs = require('hbs'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
yargs = require('yargs/yargs'),
xmlFormat = require('xml-formatter'),
samlp = require('samlp'),
SamlStrategy = require('passport-wsfed-saml2').Strategy,
passport = require('passport'),
PassportSaml = require('passport-wsfed-saml2').SAML,
PassportSamlp = require('passport-wsfed-saml2').samlp,
Parser = require('xmldom').DOMParser,
SessionParticipants = require('samlp/lib/sessionParticipants'),
SimpleProfileMapper = require('./lib/simpleProfileMapper.js'),
IdPMetadata = require('./idp-metadata');
/**
* Globals
*/
const IDP_PATHS = {
SSO: '/samlproxy/idp/saml/sso',
SLO: '/samlproxy/idp/saml/slo',
METADATA: '/samlproxy/idp/metadata',
SIGN_IN: '/samlproxy/idp/signin',
SIGN_OUT: '/samlproxy/idp/signout',
SETTINGS: '/samlproxy/idp/settings'
}
const AUTHN_REQUEST_TEMPLATE = _.template(
fs.readFileSync(path.join(__dirname, '/templates/authnrequest.tpl'), 'utf8')
);
const METADATA_TEMPLATE = _.template(
fs.readFileSync(path.join(__dirname, '/templates/metadata.tpl'), 'utf8')
);
const SP_SLO_URL = '/samlproxy/sp/saml/slo';
const SP_PROFILE_URL = '/samlproxy/sp/profile';
const SP_LOGIN_URL ='/samlproxy/sp/login';
const SP_LOGOUT_URL = '/samlproxy/sp/logout';
const SP_METADATA_URL = '/samlproxy/sp/metadata';
const SP_SETTINGS_URL = '/samlproxy/sp/settings';
const SP_ERROR_URL = '/samlproxy/sp/error';
const BINDINGS = {
REDIRECT: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
POST: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST'
}
const NAMEID_FORMAT_PREFERENCE = [
'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified',
'urn:oasis:names:tc:SAML:2.0:nameid-format:transient',
'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
'urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos',
'urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName'
]
const cryptTypes = {
certificate: /-----BEGIN CERTIFICATE-----[^-]*-----END CERTIFICATE-----/,
'RSA private key': /-----BEGIN RSA PRIVATE KEY-----\n[^-]*\n-----END RSA PRIVATE KEY-----/,
'public key': /-----BEGIN PUBLIC KEY-----\n[^-]*\n-----END PUBLIC KEY-----/,
},
KEY_CERT_HELP_TEXT = "Please generate a key-pair for the IdP using the following openssl command:\n" +
"\topenssl req -x509 -new -newkey rsa:2048 -nodes -subj '/C=US/ST=California/L=San Francisco/O=JankyCo/CN=Test Identity Provider' -keyout idp-private-key.pem -out idp-public-cert.pem -days 7300";
function matchesCertType(value, type) {
// console.info(`Testing ${cryptTypes[type].toString()} against "${value}"`);
// console.info(`result: ${cryptTypes[type] && cryptTypes[type].test(value)}`);
return cryptTypes[type] && cryptTypes[type].test(value);
}
function bufferFromString(value) {
if (Buffer.hasOwnProperty('from')) {
// node 6+
return Buffer.from(value);
} else {
return new Buffer(value);
}
}
function resolveFilePath(filePath) {
var possiblePath;
if (fs.existsSync(filePath)) {
return filePath;
}
if (filePath.slice(0, 2) === '~/') {
possiblePath = path.resolve(process.env.HOME, filePath.slice(2));
if (fs.existsSync(possiblePath)) {
return possiblePath;
} else {
// for ~/ paths, don't try to resolve further
return filePath;
}
}
['.', __dirname].forEach(function (base) {
possiblePath = path.resolve(base, filePath);
if (fs.existsSync(possiblePath)) {
return possiblePath;
}
});
return null;
}
function makeCertFileCoercer(type, description, helpText) {
return function certFileCoercer(value) {
if (matchesCertType(value, type)) {
return value;
}
const filePath = resolveFilePath(value);
if (filePath) {
return fs.readFileSync(filePath)
}
throw new Error(
'Invalid ' + description + ', not a valid crypt cert/key or file path' +
(helpText ? '\n' + helpText : '')
)
};
}
function certToPEM(cert) {
if (/-----BEGIN CERTIFICATE-----/.test(cert)) {
return cert;
}
cert = cert.match(/.{1,64}/g).join('\n');
cert = "-----BEGIN CERTIFICATE-----\n" + cert;
cert = cert + "\n-----END CERTIFICATE-----\n";
return cert;
}
function getHashCode(str) {
var hash = 0;
if (str.length == 0) return hash;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
function getPath(path) {
if (path) {
return path.startsWith('/') ? path : '/' + path;
}
}
function getReqUrl(req, path) {
if (req) {
return (req.get('x-forwarded-proto') || req.protocol) + '://' + (req.get('x-forwarded-host') || req.get('host')) + getPath(path || req.originalUrl);
}
};
function removeHeaders(cert) {
const pem = /-----BEGIN (\w*)-----([^-]*)-----END (\w*)-----/g.exec(cert);
if (pem && pem.length > 0) {
return pem[2].replace(/[\n|\r\n]/g, '');
}
return cert;
};
/**
* Arguments
*/
function processArgs(args, options) {
var baseArgv;
console.log();
console.log('loading configuration...');
if (options) {
baseArgv = yargs(args).config(options);
} else {
baseArgv = yargs(args).config('settings', function(settingsPathArg) {
const settingsPath = resolveFilePath(settingsPathArg);
return JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));
});
}
return baseArgv
.usage('\nSimple IdP for SAML 2.0 WebSSO & SLO Profile\n\n' +
'Launches an IdP web server that mints SAML assertions or logout responses for a Service Provider (SP)\n\n' +
'Usage:\n\t$0 -acs {url} -aud {uri}')
.options({
idpPort: {
description: 'IdP Web Server Listener Port',
required: true,
default: 7000
},
idpCert: {
description: 'IdP Signature PublicKey Certificate',
required: true,
default: './idp-public-cert.pem',
coerce: makeCertFileCoercer('certificate', 'IdP Signature PublicKey Certificate', KEY_CERT_HELP_TEXT)
},
idpKey: {
description: 'IdP Signature PrivateKey Certificate',
required: true,
default: './idp-private-key.pem',
coerce: makeCertFileCoercer('RSA private key', 'IdP Signature PrivateKey Certificate', KEY_CERT_HELP_TEXT)
},
idpIssuer: {
description: 'IdP Issuer URI',
required: true,
default: 'urn:example:idp'
},
idpAcsUrl: {
description: 'SP Assertion Consumer URL',
required: true,
},
idpSloUrl: {
description: 'SP Single Logout URL',
required: false,
},
idpAudience: {
description: 'SP Audience URI',
required: true,
},
idpServiceProviderId: {
description: 'SP Issuer/Entity URI',
required: false,
string: true
},
idpRelayState: {
description: 'Default SAML RelayState for SAMLResponse',
required: false,
},
idpDisableRequestAcsUrl: {
description: 'Disables ability for SP AuthnRequest to specify Assertion Consumer URL',
required: false,
boolean: true,
default: false
},
idpEncryptAssertion: {
description: 'Encrypts assertion with SP Public Key',
required: false,
boolean: true,
default: false
},
idpEncryptionCert: {
description: 'SP Certificate (pem) for Assertion Encryption',
required: false,
string: true,
coerce: makeCertFileCoercer('certificate', 'Encryption cert')
},
idpEncryptionPublicKey: {
description: 'SP RSA Public Key (pem) for Assertion Encryption ' +
'(e.g. openssl x509 -pubkey -noout -in sp-cert.pem)',
required: false,
string: true,
coerce: makeCertFileCoercer('public key', 'Encryption public key')
},
idpHttpsPrivateKey: {
description: 'Web Server TLS/SSL Private Key (pem)',
required: false,
string: true,
coerce: makeCertFileCoercer('RSA private key')
},
idpHttpsCert: {
description: 'Web Server TLS/SSL Certificate (pem)',
required: false,
string: true,
coerce: makeCertFileCoercer('certificate')
},
idpHttps: {
description: 'Enables HTTPS Listener (requires httpsPrivateKey and httpsCert)',
required: true,
boolean: true,
default: false
},
idpSignResponse: {
description: 'Enables signing of responses',
required: false,
boolean: true,
default: true,
},
idpConfigFile: {
description: 'Path to a SAML attribute config file',
required: true,
default: require.resolve('./config.js'),
},
idpRollSession: {
description: 'Create a new session for every authn request instead of reusing an existing session',
required: false,
boolean: true,
default: false
},
idpAuthnContextClassRef: {
description: 'Authentication Context Class Reference',
required: false,
string: true,
default: 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport',
},
idpAuthnContextDecl: {
description: 'Authentication Context Declaration (XML FilePath)',
required: false,
string: true,
coerce: function (value) {
const filePath = resolveFilePath(value);
if (filePath) {
return fs.readFileSync(filePath, 'utf8')
}
}
},
idpBaseUrl: {
description: 'IdP Base URL',
required: false,
string: true,
},
spPort: {
description: 'Web Server listener port',
required: true,
number: true,
default: 7070
},
spProtocol: {
description: 'Federation Protocol',
required: true,
string: true,
default: 'samlp'
},
spIdpIssuer: {
description: 'IdP Issuer URI',
required: false,
string: true,
default: 'urn:example:idp'
},
spIdpSsoUrl: {
description: 'IdP Single Sign-On Service URL (SSO URL)',
required: false,
string: true
},
spIdpSsoBinding: {
description: 'IdP Single Sign-On AuthnRequest Binding',
required: true,
string: true,
default: BINDINGS.REDIRECT
},
spIdpSloUrl: {
description: 'IdP Single Logout Service URL (SLO URL) (SAMLP)',
required: false,
string: true
},
spIdpSloBinding: {
description: 'IdP Single Logout Request Binding (SAMLP)',
required: true,
string: true,
default: BINDINGS.REDIRECT
},
spIdpCert: {
description: 'IdP Public Key Signing Certificate (PEM)',
required: false,
string: true,
coerce: (value) => {
return certToPEM(makeCertFileCoercer('certificate', 'IdP Public Key Signing Certificate (PEM)', KEY_CERT_HELP_TEXT));
}
},
spIdpThumbprint: {
description: 'IdP Public Key Signing Certificate SHA1 Thumbprint',
required: false,
string: true,
coerce: (value) => {
return value ? value.replace(/:/g, '') : value
}
},
spIdpMetaUrl: {
description: 'IdP SAML Metadata URL',
required: false,
string: true
},
spAudience: {
description: 'SP Audience URI / RP Realm',
required: false,
string: true,
default: 'urn:example:sp'
},
spProviderName: {
description: 'SP Provider Name',
required: false,
string: true,
default: 'Simple SAML Service Provider'
},
spAcsUrls: {
description: 'SP Assertion Consumer Service (ACS) URLs (Relative URL)',
required: true,
array: true,
default: ['/saml/sso']
},
spSignAuthnRequests: {
description: 'Sign AuthnRequest Messages (SAMLP)',
required: true,
boolean: true,
default: true,
},
spSignatureAlgorithm: {
description: 'Signature Algorithm',
required: false,
string: true,
default: 'rsa-sha256'
},
spDigestAlgorithm: {
description: 'Digest Algorithm',
required: false,
string: true,
default: 'sha256'
},
spRequestNameIDFormat : {
description: 'Request Subject NameID Format (SAMLP)',
required: false,
boolean: true,
default: true
},
spValidateNameIDFormat : {
description: 'Validate format of Assertion Subject NameID',
required: false,
boolean: true,
default: true
},
spNameIDFormat : {
description: 'Assertion Subject NameID Format',
required: false,
string: true,
default: 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'
},
spRequestAuthnContext : {
description: 'Request Authentication Context (SAMLP)',
required: false,
boolean: true,
default: true
},
spAuthnContextClassRef : {
description: 'Authentication Context Class Reference',
required: false,
string: true,
default: 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport'
},
spCert: {
description: 'SP/RP Public Key Signature & Encryption Certificate (PEM)',
string: true,
required: false,
default: path.resolve(__dirname, './sp-cert.pem'),
coerce: makeCertFileCoercer('certificate', 'SP Signing Public Key Certificate (PEM)', KEY_CERT_HELP_TEXT)
},
spKey: {
description: 'SP/RP Private Key Signature & Decryption Certificate(PEM)',
string: true,
required: false,
default: path.resolve(__dirname, './sp-key.pem'),
coerce: makeCertFileCoercer('privateKey', 'SP Signing Private Key (PEM)', KEY_CERT_HELP_TEXT)
},
spHttpsPrivateKey: {
description: 'Web Server TLS/SSL Private Key (PEM)',
required: false,
string: true,
coerce: makeCertFileCoercer('privateKey', 'Web Server TLS/SSL Private Key (PEM)', KEY_CERT_HELP_TEXT)
},
spHttpsCert: {
description: 'Web Server TLS/SSL Certificate (PEM)',
required: false,
string: true,
coerce: makeCertFileCoercer('certificate', 'Web Server TLS/SSL Public Key Certificate (PEM)', KEY_CERT_HELP_TEXT)
},
spHttps: {
description: 'Enables HTTPS Listener (requires httpsPrivateKey and httpsCert)',
required: false,
boolean: true,
default: false
},
spRelayState: {
description: 'Default Relay State',
required: false,
string: true
}
})
.example('\t$0 --acs http://acme.okta.com/auth/saml20/exampleidp --aud https://www.okta.com/saml2/service-provider/spf5aFRRXFGIMAYXQPNV', '')
.check(function(argv, aliases) {
if (argv.idpEncryptAssertion) {
if (argv.idpEncryptionPublicKey === undefined) {
return 'encryptionPublicKey argument is also required for assertion encryption';
}
if (argv.idpEncryptionCert === undefined) {
return 'encryptionCert argument is also required for assertion encryption';
}
}
return true;
})
.check(function(argv, aliases) {
if (argv.idpConfig) {
return true;
}
const configFilePath = resolveFilePath(argv.idpConfigFile);
if (!configFilePath) {
return 'SAML attribute config file path "' + argv.idpConfigFile + '" is not a valid path.\n';
}
try {
argv.idpConfig = require(configFilePath);
} catch (error) {
return 'Encountered an exception while loading SAML attribute config file "' + configFilePath + '".\n' + error;
}
return true;
})
.check((argv, aliases) => {
if (!_.isString(argv.spIdpMetaUrl)) {
if (!_.isString(argv.spIdpSsoUrl) || argv.spIdpSsoUrl === '') {
return 'IdP SSO Assertion Consumer URL (spIdpSsoUrl) is required when IdP metadata is not specified';
}
if (!_.isString(argv.spIdpCert) && !_.isString(argv.spIdpThumbprint)) {
return ' IdP Signing Certificate (spIdpCert) or IdP Signing Key Thumbprint (spIdpThumbprint) is required when IdP metadata is not specified';
}
// convert cert to PEM
argv.spIdpCertPEM = certToPEM(argv.spIdpCert)
}
return true;
})
.wrap(baseArgv.terminalWidth());
}
function _runServer(argv) {
IdPMetadata.fetch(argv.spIdpMetaUrl)
.then((metadata) => {
if (metadata.protocol) {
argv.protocol = metadata.protocol;
if (metadata.signingKeys[0]) {
argv.spIdpCert = certToPEM(metadata.signingKeys[0]);
}
switch (metadata.protocol) {
case 'samlp':
if (metadata.sso.redirectUrl) {
argv.spIdpSsoUrl = metadata.sso.redirectUrl;
} else if (metadata.sso.postUrl) {
argv.spIdpSsoUrl = metadata.sso.postUrl;
}
if (metadata.slo.redirectUrl) {
argv.spIdpSloUrl = metadata.slo.redirectUrl;
} else if (metadata.slo.postUrl) {
argv.spIdpSloUrl = metadata.slo.postUrl;
}
if (metadata.signRequest) {
argv.spSignAuthnRequests = metadata.signRequest;
}
break;
case 'wsfed':
if (metadata.sso.redirectUrl) {
argv.spIdpSsoUrl = metadata.sso.redirectUrl;
}
break;
}
}
})
.then(() => {
const app = express();
const httpServer = argv.idpHttps ?
https.createServer({ key: argv.idpHttpsPrivateKey, cert: argv.idpHttpsCert }, app) :
http.createServer(app);
const blocks = {};
console.log();
console.log('Listener Port:\n\t' + argv.idpPort);
console.log('HTTPS Enabled:\n\t' + argv.idpHttps);
console.log();
console.log('[IdP]');
console.log();
console.log('Issuer URI:\n\t' + argv.idpIssuer);
console.log('Sign Response Message:\n\t' + argv.idpSignResponse);
console.log('Encrypt Assertion:\n\t' + argv.idpEncryptAssertion);
console.log('Authentication Context Class Reference:\n\t' + argv.idpAuthnContextClassRef);
console.log('Authentication Context Declaration:\n\n' + argv.idpAuthnContextDecl);
console.log('Default RelayState:\n\t' + argv.idpRelayState);
console.log();
console.log('[IdP SP]');
console.log();
console.log('Issuer URI:\n\t' + argv.idpServiceProviderId);
console.log('Audience URI:\n\t' + argv.idpAudience);
console.log('ACS URL:\n\t' + argv.idpAcsUrl);
console.log('SLO URL:\n\t' + argv.idpSloUrl);
console.log('Trust ACS URL in Request:\n\t' + !argv.idpDisableRequestAcsUrl);
console.log();
console.log();
console.log('[SP]');
console.log();
console.log('Protocol: ' + "SAMLP");
console.log();
console.log('IdP Issuer URI:\n\t' + argv.spIdpIssuer);
console.log('IdP SSO ACS URL:\n\t' + argv.spIdpSsoUrl);
console.log('IdP SLO URL:\n\t' + argv.spIdpSloUrl);
console.log();
console.log('SP Issuer URI:\n\t' + argv.spAudience);
console.log('SP Audience URI:\n\t' + argv.spAudience);
console.log('SP NameID Format:\n\t' + argv.spNameIDFormat);
console.log('SP ACS Binding:\n\turn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST');
console.log('SP ACS URL:');
argv.spAcsUrls.forEach(function(acsUrl) {
console.log('\t' + acsUrl);
});
console.log('SP Default Relay State:\n\t' + argv.spRelayState);
console.log();
/**
* IdP Configuration
*/
SimpleProfileMapper.prototype.metadata = argv.idpConfig.metadata;
const idpOptions = {
idpBaseUrl: argv.idpBaseUrl,
issuer: argv.idpIssuer,
serviceProviderId: argv.idpServiceProviderId || argv.idpAudience,
cert: argv.idpCert,
key: argv.idpKey,
audience: argv.idpAudience,
recipient: argv.idpAcsUrl,
destination: argv.idpAcsUrl,
acsUrl: argv.idpAcsUrl,
sloUrl: argv.idpSloUrl,
RelayState: argv.idpRelayState,
allowRequestAcsUrl: !argv.idpDisableRequestAcsUrl,
digestAlgorithm: 'sha256',
signatureAlgorithm: 'rsa-sha256',
signResponse: argv.idpSignResponse,
encryptAssertion: argv.idpEncryptAssertion,
encryptionCert: argv.idpEncryptionCert,
encryptionPublicKey: argv.idpEncryptionPublicKey,
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
lifetimeInSeconds: 3600,
authnContextClassRef: argv.idpAuthnContextClassRef,
authnContextDecl: argv.idpAuthnContextDecl,
includeAttributeNameFormat: true,
profileMapper: SimpleProfileMapper,
postEndpointPath: IDP_PATHS.SSO,
redirectEndpointPath: IDP_PATHS.SSO,
logoutEndpointPaths: argv.idpSloUrl ?
{
redirect: IDP_PATHS.SLO,
post: IDP_PATHS.SLO
} : {},
getUserFromRequest: function(req) { return req.user; },
getPostURL: function (audience, authnRequestDom, req, callback) {
return callback(null, (req.authnRequest && req.authnRequest.acsUrl) ?
req.authnRequest.acsUrl :
argv.idpAcsUrl);
},
transformAssertion: function(assertionDom) {
if (argv.idpAuthnContextDecl) {
var declDoc;
try {
declDoc = new Parser().parseFromString(argv.idpAuthnContextDecl);
} catch(err){
console.log('Unable to parse Authentication Context Declaration XML', err);
}
if (declDoc) {
const authnContextDeclEl = assertionDom.createElementNS('urn:oasis:names:tc:SAML:2.0:assertion', 'saml:AuthnContextDecl');
authnContextDeclEl.appendChild(declDoc.documentElement);
const authnContextEl = assertionDom.getElementsByTagName('saml:AuthnContext')[0];
authnContextEl.appendChild(authnContextDeclEl);
}
}
},
responseHandler: function(response, opts, req, res, next) {
console.log();
console.log(`req.session.ssoResponse = ${JSON.stringify(req.session.ssoResponse)}\n`);
console.log(`Sending SAMLResponse to ${opts.postUrl} with RelayState ${opts.RelayState} =>\n`);
console.log(xmlFormat(response.toString(), {indentation: ' '}));
console.log();
res.render('samlresponse', {
AcsUrl: opts.postUrl,
SAMLResponse: response.toString('base64'),
RelayState: opts.RelayState
});
}
}
const spConfig = {
port: argv.spPort,
protocol: argv.spProtocol,
idpIssuer: argv.spIdpIssuer,
idpSsoUrl: argv.spIdpSsoUrl,
idpSsoBinding: argv.spIdpSsoBinding,
idpSloUrl: argv.spIdpSloUrl,
idpSloBinding: argv.spIdpSloBinding,
idpCert: argv.spIdpCert,
idpThumbprint: argv.spIdpThumbprint,
idpMetaUrl: argv.spIdpMetaUrl,
audience: argv.spAudience,
providerName: argv.spProviderName,
acsUrls: argv.spAcsUrls,
signAuthnRequests: argv.spSignAuthnRequests,
signatureAlgorithm: argv.spSignatureAlgorithm,
digestAlgorithm: argv.spDigestAlgorithm,
requestNameIDFormat: argv.spRequestNameIDFormat,
validateNameIDFormat: argv.spValidateNameIDFormat,
nameIDFormat: argv.spNameIDFormat,
requestAuthnContext: argv.spRequestAuthnContext,
authnContextClassRef: argv.spAuthnContextClassRef,
spCert: argv.spCert,
spKey: argv.spKey,
httpsPrivateKey: argv.spHttpsPrivateKey,
httpsCert: argv.spHttpsCert,
https: argv.spHttps,
relayState: argv.spRelayState,
requestAcsUrl: argv.spAcsUrls[0],
failureRedirect: SP_ERROR_URL,
failureFlash: true,
// can't use arrow functions due to lexical scoping
getMetadataParams: function(req) {
return {
protocol: this.protocol,
entityID: this.audience,
realm: this.audience,
cert: removeHeaders(this.spCert),
acsUrls: this.acsUrls.map(url => getReqUrl(req, url)),
sloUrl: getReqUrl(req, SP_SLO_URL),
nameIDFormat: this.nameIDFormat
}
},
getRequestSecurityTokenParams: function(wreply, wctx) {
return {
wreply: wreply,
wctx: wctx || this.relayState,
}
},
getAuthnRequestParams: function(acsUrl, forceAuthn, relayState) {
const params = {
protocol: this.protocol,
realm: this.audience,
callback: acsUrl,
protocolBinding: this.idpSsoBinding,
identityProviderUrl: this.idpSsoUrl,
providerName: this.providerName,
forceAuthn: forceAuthn,
authnContext: this.authnContextClassRef,
requestContext: {
NameIDFormat: this.nameIDFormat
},
requestTemplate: AUTHN_REQUEST_TEMPLATE({
ForceAuthn: forceAuthn,
NameIDFormat: this.requestNameIDFormat,
AuthnContext: this.requestAuthnContext,
}),
signatureAlgorithm: this.signatureAlgorithm,
digestAlgorithm: this.digestAlgorithm,
deflate: this.deflate,
RelayState: relayState || this.relayState,
failureRedirect: this.failureRedirect,
failureFlash: this.failureFlash
}
if (this.signAuthnRequests) {
params.signingKey = {
cert: this.spCert,
key: this.spKey
}
}
return params;
},
getResponseParams: function(destinationUrl) {
return {
protocol: this.protocol,
thumbprint: this.idpThumbprint,
cert: removeHeaders(this.idpCert),
realm: this.audience,
identityProviderUrl: this.idpSsoUrl, //wsfed
recipientUrl: destinationUrl,
destinationUrl: destinationUrl,
decryptionKey: this.spKey,
checkResponseID: true,
checkDestination: true,
checkInResponseTo: true,
checkExpiration: true,
checkAudience: true,
checkNameQualifier: true,
checkSPNameQualifier: true,
failureRedirect: this.failureRedirect,
failureFlash: this.failureFlash
}
},
getLogoutParams: function() {
return {
issuer: this.audience,
protocolBinding: this.idpSloBinding,
deflate: this.deflate,
identityProviderUrl: this.idpSloUrl,
identityProviderSigningCert: this.idpCert,
key: this.spKey,
cert: this.spCert
}
}
};
responseParams = spConfig.getResponseParams();
const strategy = new SamlStrategy(responseParams,
(profile, done) => {
console.log();
console.log('Assertion => ' + JSON.stringify(profile, null, '\t'));
console.log();
return done(null, {
issuer: profile.issuer,
userName: profile.nameIdAttributes.value,
nameIdFormat: profile.nameIdAttributes.Format,
authnContext: {
sessionIndex: profile.sessionIndex,
authnMethod: profile['http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod']
},
claims: _.chain(profile)
.omit('issuer', 'sessionIndex', 'nameIdAttributes',
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier',
'http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod')
.value()
});
}
);
passport.use(strategy);
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
/**
* App Environment
*/
app.set('port', process.env.PORT || argv.idpPort);
app.set('views', path.join(__dirname, 'views'));
/**
* View Engine
*/
app.set('view engine', 'hbs');
app.set('view options', { layout: 'layout' })
app.engine('handlebars', hbs.__express);
app.use(express.static(path.join(__dirname, 'public')));
app.use('/samlproxy/idp/bower_components', express.static(__dirname + '/public/bower_components'))
app.use(passport.initialize());
// Register Helpers
hbs.registerHelper('extend', function(name, context) {
var block = blocks[name];
if (!block) {
block = blocks[name] = [];
}
block.push(context.fn(this));
});
hbs.registerHelper('block', function(name) {
const val = (blocks[name] || []).join('\n');
// clear the block
blocks[name] = [];
return val;
});
hbs.registerHelper('select', function(selected, options) {
return options.fn(this).replace(
new RegExp(' value=\"' + selected + '\"'), '$& selected="selected"');
});
hbs.registerHelper('getProperty', function(attribute, context) {
return context[attribute];
});
hbs.registerHelper('serialize', function(context) {
return new Buffer(JSON.stringify(context)).toString('base64');
});
/**
* Middleware
*/
app.use(logger(':date> :method :url - {:referrer} => :status (:response-time ms)', {
skip: function (req, res)
{
return req.path.startsWith('/samlproxy/idp/bower_components') || req.path.startsWith('/samlproxy/idp/css');
}
}));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
secret: 'The universe works on a math equation that never even ever really ends in the end',
resave: false,
saveUninitialized: true,
name: 'idp_sid',
cookie: { maxAge: 60000 }
}));
/**
* View Handlers
*/
const showUser = function (req, res, next) {
const acsUrl = req.query.acsUrl ?
getReqUrl(req, req.query.acsUrl) :
getReqUrl(req, spConfig.requestAcsUrl);
params = spConfig.getAuthnRequestParams(
acsUrl,
req.query.forceauthn === '' || req.query.forceAuthn === '' || req.query.forceauthn || req.query.forceAuthn,
req.authnRequest.relayState
);
console.log('Generating SSO Request with Params ', params);
passport.authenticate('wsfed-saml2', params)(req, res, next);
};
/**
* Shared Handlers
*/
const parseSamlRequest = function(req, res, next) {
samlp.parseRequest(req, function(err, data) {
if (err) {
return res.render('error', {
message: 'SAML AuthnRequest Parse Error: ' + err.message,
error: err
});
};
if (data) {
req.authnRequest = {
relayState: req.query.RelayState || req.body.RelayState,
id: data.id,
issuer: data.issuer,
destination: data.destination,
acsUrl: data.assertionConsumerServiceURL,
forceAuthn: data.forceAuthn === 'true'
};
req.session.authnRequest = req.authnRequest;
console.log('Received AuthnRequest => \n', req.authnRequest);
}
return showUser(req, res, next);
});
};
const getSessionIndex = function(req) {
if (req && req.session) {
return Math.abs(getHashCode(req.session.id)).toString();
}
};
const getParticipant = function(req) {
return {
serviceProviderId: req.idp.options.serviceProviderId,
sessionIndex: getSessionIndex(req),
nameId: req.user.userName,
nameIdFormat: req.user.nameIdFormat,
serviceProviderLogoutURL: req.idp.options.sloUrl
};
};
const parseLogoutRequest = function(req, res, next) {
if (!req.idp.options.sloUrl) {
return res.render('error', {
message: 'SAML Single Logout Service URL not defined for Service Provider'
});
};
console.log('Processing SAML SLO request for participant => \n', req.participant);
return samlp.logout({
cert: req.idp.options.cert,
key: req.idp.options.key,
digestAlgorithm: req.idp.options.digestAlgorithm,
signatureAlgorithm: req.idp.options.signatureAlgorithm,
sessionParticipants: new SessionParticipants(
[
req.participant
]),
clearIdPSession: function(callback) {