-
Notifications
You must be signed in to change notification settings - Fork 390
/
LogoutRequest.java
1018 lines (911 loc) · 33.8 KB
/
LogoutRequest.java
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
package com.onelogin.saml2.logout;
import java.io.IOException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.onelogin.saml2.exception.ValidationError;
import com.onelogin.saml2.exception.SettingsException;
import com.onelogin.saml2.http.HttpRequest;
import com.onelogin.saml2.settings.Saml2Settings;
import com.onelogin.saml2.util.Util;
import com.onelogin.saml2.util.Constants;
import com.onelogin.saml2.util.SchemaFactory;
/**
* LogoutRequest class of Java Toolkit.
*
* A class that implements SAML 2 Logout Request builder/parser/validator
*/
public class LogoutRequest {
/**
* Private property to construct a logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(LogoutRequest.class);
/**
* SAML LogoutRequest string
*/
private final String logoutRequestString;
/**
* SAML LogoutRequest ID.
*/
public String id;
/**
* Settings data.
*/
private final Saml2Settings settings;
/**
* HttpRequest object to be processed (Contains GET and POST parameters, request URL, ...).
*/
private final HttpRequest request;
/**
* URL of the current host + current view
*/
private String currentUrl;
/**
* Time when the Logout Request was created
*/
private Calendar issueInstant;
/**
* After validation, if it fails this property has the cause of the problem
*/
private Exception validationException;
/**
* Constructs the LogoutRequest object.
*
* @param settings
* OneLogin_Saml2_Settings
* @param request
* the HttpRequest object to be processed (Contains GET and POST
* parameters, request URL, ...).
* @param nameId
* The NameID that will be set in the LogoutRequest.
* @param sessionIndex
* The SessionIndex (taken from the SAML Response in the SSO
* process).
* @param nameIdFormat
* The nameIdFormat that will be set in the LogoutRequest.
* @param nameIdNameQualifier
* The NameID NameQualifier that will be set in the LogoutRequest.
* @param nameIdSPNameQualifier
* The SP Name Qualifier that will be set in the LogoutRequest.
*
* @deprecated use {@link #LogoutRequest(Saml2Settings, HttpRequest)} to build a
* received request from the HTTP request, or
* {@link #LogoutRequest(Saml2Settings, LogoutRequestParams)} with
* {@link LogoutRequestParams#LogoutRequestParams(String, String, String, String, String)}
* to build a new request to be sent
*/
@Deprecated
public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId, String sessionIndex, String nameIdFormat, String nameIdNameQualifier, String nameIdSPNameQualifier) {
this.settings = settings;
this.request = request;
String samlLogoutRequest = null;
if (request != null) {
samlLogoutRequest = request.getParameter("SAMLRequest");
currentUrl = request.getRequestURL();
}
if (samlLogoutRequest == null) {
LogoutRequestParams params = new LogoutRequestParams(sessionIndex, nameId, nameIdFormat, nameIdNameQualifier, nameIdSPNameQualifier);
id = Util.generateUniqueID(settings.getUniqueIDPrefix());
issueInstant = Calendar.getInstance();
StrSubstitutor substitutor = generateSubstitutor(params, settings);
logoutRequestString = postProcessXml(substitutor.replace(getLogoutRequestTemplate()), params, settings);
} else {
logoutRequestString = Util.base64decodedInflated(samlLogoutRequest);
Document doc = Util.loadXML(logoutRequestString);
id = getId(doc);
issueInstant = getIssueInstant(doc);
}
}
/**
* Constructs the LogoutRequest object.
*
* @param settings
* OneLogin_Saml2_Settings
* @param request
* the HttpRequest object to be processed (Contains GET and POST
* parameters, request URL, ...).
* @param nameId
* The NameID that will be set in the LogoutRequest.
* @param sessionIndex
* The SessionIndex (taken from the SAML Response in the SSO
* process).
* @param nameIdFormat
* The nameIdFormat that will be set in the LogoutRequest.
* @param nameIdNameQualifier
* The NameID NameQualifier will be set in the LogoutRequest.
*
* @deprecated use {@link #LogoutRequest(Saml2Settings, HttpRequest)} to build a
* received request from the HTTP request, or
* {@link #LogoutRequest(Saml2Settings, LogoutRequestParams)} with
* {@link LogoutRequestParams#LogoutRequestParams(String, String, String, String)}
* to build a new request to be sent
*/
@Deprecated
public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId, String sessionIndex, String nameIdFormat, String nameIdNameQualifier) {
this(settings, request, nameId, sessionIndex, nameIdFormat, nameIdNameQualifier, null);
}
/**
* Constructs the LogoutRequest object.
*
* @param settings
* OneLogin_Saml2_Settings
* @param request
* the HttpRequest object to be processed (Contains GET and POST
* parameters, request URL, ...).
* @param nameId
* The NameID that will be set in the LogoutRequest.
* @param sessionIndex
* The SessionIndex (taken from the SAML Response in the SSO
* process).
* @param nameIdFormat
* The nameIdFormat that will be set in the LogoutRequest.
*
* @deprecated use {@link #LogoutRequest(Saml2Settings, HttpRequest)} to build a
* received request from the HTTP request, or
* {@link #LogoutRequest(Saml2Settings, LogoutRequestParams)} with
* {@link LogoutRequestParams#LogoutRequestParams(String, String, String)}
* to build a new request to be sent
*/
@Deprecated
public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId, String sessionIndex, String nameIdFormat) {
this(settings, request, nameId, sessionIndex, nameIdFormat, null);
}
/**
* Constructs the LogoutRequest object.
*
* @param settings
* OneLogin_Saml2_Settings
* @param request
* the HttpRequest object to be processed (Contains GET and POST
* parameters, request URL, ...).
* @param nameId
* The NameID that will be set in the LogoutRequest.
* @param sessionIndex
* The SessionIndex (taken from the SAML Response in the SSO
* process).
*
* @deprecated use {@link #LogoutRequest(Saml2Settings, HttpRequest)} to build a
* received request from the HTTP request, or
* {@link #LogoutRequest(Saml2Settings, LogoutRequestParams)} with
* {@link LogoutRequestParams#LogoutRequestParams(String, String)}
* to build a new request to be sent
*/
@Deprecated
public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId, String sessionIndex) {
this(settings, request, nameId, sessionIndex, null);
}
/**
* Constructs a LogoutRequest object when a new request should be generated and
* sent.
*
* @param settings
* OneLogin_Saml2_Settings
*
* @see #LogoutRequest(Saml2Settings, LogoutRequestParams)
*/
public LogoutRequest(Saml2Settings settings) {
this(settings, new LogoutRequestParams());
}
/**
* Constructs the LogoutRequest object when a received request should be
* extracted from the HTTP request and parsed.
*
* @param settings
* OneLogin_Saml2_Settings
* @param request
* the HttpRequest object to be processed (Contains GET and POST
* parameters, request URL, ...).
*/
public LogoutRequest(Saml2Settings settings, HttpRequest request) {
this(settings, request, null, null);
}
/**
* Constructs the LogoutRequest object when a new request should be generated
* and sent.
*
* @param settings
* OneLogin_Saml2_Settings
* @param params
* a set of logout request input parameters that shape the
* request to create
*/
public LogoutRequest(Saml2Settings settings, LogoutRequestParams params) {
this.settings = settings;
this.request = null;
id = Util.generateUniqueID(settings.getUniqueIDPrefix());
issueInstant = Calendar.getInstance();
StrSubstitutor substitutor = generateSubstitutor(params, settings);
logoutRequestString = postProcessXml(substitutor.replace(getLogoutRequestTemplate()), params, settings);
}
/**
* Allows for an extension class to post-process the LogoutRequest XML generated
* for this request, in order to customize the result.
* <p>
* This method is invoked at construction time when no existing LogoutRequest
* message is found in the HTTP request (and hence in the logout request sending
* scenario only), after all the other fields of this class have already been
* initialised. Its default implementation simply returns the input XML as-is,
* with no change.
*
* @param logoutRequestXml
* the XML produced for this LogoutRequest by the standard
* implementation provided by {@link LogoutRequest}
* @param params
* the logout request input parameters
* @param settings
* the settings
* @return the post-processed XML for this LogoutRequest, which will then be
* returned by any call to {@link #getLogoutRequestXml()}
*/
protected String postProcessXml(final String logoutRequestXml, final LogoutRequestParams params, final Saml2Settings settings) {
return logoutRequestXml;
}
/**
* @return the base64 encoded unsigned Logout Request (deflated or not)
*
* @param deflated
* If deflated or not the encoded Logout Request
*
* @throws IOException
*/
public String getEncodedLogoutRequest(Boolean deflated) throws IOException {
String encodedLogoutRequest;
if (deflated == null) {
deflated = settings.isCompressRequestEnabled();
}
if (deflated) {
encodedLogoutRequest = Util.deflatedBase64encoded(getLogoutRequestXml());
} else {
encodedLogoutRequest = Util.base64encoder(getLogoutRequestXml());
}
return encodedLogoutRequest;
}
/**
* @return the base64 encoded unsigned Logout Request (deflated or not)
*
* @throws IOException
*/
public String getEncodedLogoutRequest() throws IOException {
return getEncodedLogoutRequest(null);
}
/**
* @return the plain XML Logout Request
*/
public String getLogoutRequestXml() {
return logoutRequestString;
}
/**
* Substitutes LogoutRequest variables within a string by values.
*
* @param params
* the logout request input parameters
* @param settings
* Saml2Settings object. Setting data
*
* @return the StrSubstitutor object of the LogoutRequest
*/
private StrSubstitutor generateSubstitutor(LogoutRequestParams params, Saml2Settings settings) {
Map<String, String> valueMap = new HashMap<String, String>();
valueMap.put("id", Util.toXml(id));
String issueInstantString = Util.formatDateTime(issueInstant.getTimeInMillis());
valueMap.put("issueInstant", issueInstantString);
String destinationStr = "";
URL slo = settings.getIdpSingleLogoutServiceUrl();
if (slo != null) {
destinationStr = " Destination=\"" + Util.toXml(slo.toString()) + "\"";
}
valueMap.put("destinationStr", destinationStr);
valueMap.put("issuer", Util.toXml(settings.getSpEntityId()));
String nameId = params.getNameId();
String requestedNameIdFormat = params.getNameIdFormat();
String nameIdFormat = null;
String spNameQualifier = params.getNameIdSPNameQualifier();
String nameQualifier = params.getNameIdNameQualifier();
if (nameId != null) {
if (requestedNameIdFormat == null && !settings.getSpNameIDFormat().equals(Constants.NAMEID_UNSPECIFIED)) {
nameIdFormat = settings.getSpNameIDFormat();
} else {
nameIdFormat = requestedNameIdFormat;
}
} else {
nameId = settings.getIdpEntityId();
nameIdFormat = Constants.NAMEID_ENTITY;
}
// From saml-core-2.0-os 8.3.6, when the entity Format is used: "The NameQualifier, SPNameQualifier, and
// SPProvidedID attributes MUST be omitted.
if (nameIdFormat != null && nameIdFormat.equals(Constants.NAMEID_ENTITY)) {
nameQualifier = null;
spNameQualifier = null;
}
// NameID Format UNSPECIFIED omitted
if (nameIdFormat != null && nameIdFormat.equals(Constants.NAMEID_UNSPECIFIED)) {
nameIdFormat = null;
}
X509Certificate cert = null;
if (settings.getNameIdEncrypted()) {
cert = settings.getIdpx509cert();
if (cert == null) {
List<X509Certificate> multipleCertList = settings.getIdpx509certMulti();
if (multipleCertList != null && !multipleCertList.isEmpty())
cert = multipleCertList.get(0);
}
}
String nameIdStr = Util.generateNameId(nameId, spNameQualifier, nameIdFormat, nameQualifier, cert);
valueMap.put("nameIdStr", nameIdStr);
String sessionIndexStr = "";
String sessionIndex = params.getSessionIndex();
if (sessionIndex != null) {
sessionIndexStr = " <samlp:SessionIndex>" + Util.toXml(sessionIndex) + "</samlp:SessionIndex>";
}
valueMap.put("sessionIndexStr", sessionIndexStr);
return new StrSubstitutor(valueMap);
}
/**
* @return the LogoutRequest's template
*/
private static StringBuilder getLogoutRequestTemplate() {
StringBuilder template = new StringBuilder();
template.append("<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" ");
template.append("ID=\"${id}\" ");
template.append("Version=\"2.0\" ");
template.append("IssueInstant=\"${issueInstant}\"${destinationStr} >");
template.append("<saml:Issuer>${issuer}</saml:Issuer>");
template.append("${nameIdStr}${sessionIndexStr}</samlp:LogoutRequest>");
return template;
}
/**
* Determines if the SAML LogoutRequest is valid or not
*
* @return true if the SAML LogoutRequest is valid
*/
public Boolean isValid() {
validationException = null;
try {
if (this.logoutRequestString == null || logoutRequestString.isEmpty()) {
throw new ValidationError("SAML Logout Request is not loaded", ValidationError.INVALID_XML_FORMAT);
}
if (this.request == null) {
throw new Exception("The HttpRequest of the current host was not established");
}
if (this.currentUrl == null || this.currentUrl.isEmpty()) {
throw new Exception("The URL of the current host was not established");
}
String signature = request.getParameter("Signature");
Document logoutRequestDocument = Util.loadXML(logoutRequestString);
if (settings.isStrict()) {
Element rootElement = logoutRequestDocument.getDocumentElement();
rootElement.normalize();
if (settings.getWantXMLValidation()) {
if (!Util.validateXML(logoutRequestDocument, SchemaFactory.SAML_SCHEMA_PROTOCOL_2_0)) {
throw new ValidationError("Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd", ValidationError.INVALID_XML_FORMAT);
}
}
// Check NotOnOrAfter
if (rootElement.hasAttribute("NotOnOrAfter")) {
String notOnOrAfter = rootElement.getAttribute("NotOnOrAfter");
Instant notOnOrAfterDate = Util.parseDateTime(notOnOrAfter);
if (Util.isEqualNow(notOnOrAfterDate) || Util.isBeforeNow(notOnOrAfterDate)) {
throw new ValidationError("Could not validate timestamp: expired. Check system clock.", ValidationError.RESPONSE_EXPIRED);
}
}
// Check destination
if (rootElement.hasAttribute("Destination")) {
String destinationUrl = rootElement.getAttribute("Destination");
if (destinationUrl != null) {
if (!destinationUrl.isEmpty() && !destinationUrl.equals(currentUrl)) {
throw new ValidationError("The LogoutRequest was received at " + currentUrl + " instead of "
+ destinationUrl, ValidationError.WRONG_DESTINATION);
}
}
}
// Try get the nameID
String nameID = getNameId(logoutRequestDocument, settings.getSPkey(), settings.isTrimNameIds());
// Check the issuer
String issuer = getIssuer(logoutRequestDocument, settings.isTrimNameIds());
if (issuer != null && (issuer.isEmpty() || !issuer.equals(settings.getIdpEntityId()))) {
throw new ValidationError(
String.format("Invalid issuer in the Logout Request. Was '%s', but expected '%s'", issuer, settings.getIdpEntityId()),
ValidationError.WRONG_ISSUER
);
}
if (settings.getWantMessagesSigned() && (signature == null || signature.isEmpty())) {
throw new ValidationError("The Message of the Logout Request is not signed and the SP requires it", ValidationError.NO_SIGNED_MESSAGE);
}
}
if (signature != null && !signature.isEmpty()) {
X509Certificate cert = settings.getIdpx509cert();
List<X509Certificate> certList = new ArrayList<X509Certificate>();
List<X509Certificate> multipleCertList = settings.getIdpx509certMulti();
if (multipleCertList != null && multipleCertList.size() != 0) {
certList.addAll(multipleCertList);
}
if (cert != null) {
if (certList.isEmpty() || !certList.contains(cert)) {
certList.add(0, cert);
}
}
if (certList.isEmpty()) {
throw new SettingsException("In order to validate the sign on the Logout Request, the x509cert of the IdP is required", SettingsException.CERT_NOT_FOUND);
}
String signAlg = request.getParameter("SigAlg");
if (signAlg == null || signAlg.isEmpty()) {
signAlg = Constants.RSA_SHA1;
}
Boolean rejectDeprecatedAlg = settings.getRejectDeprecatedAlg();
if (Util.mustRejectDeprecatedSignatureAlgo(signAlg, rejectDeprecatedAlg)) {
return false;
}
String relayState = request.getEncodedParameter("RelayState");
String signedQuery = "SAMLRequest=" + request.getEncodedParameter("SAMLRequest");
if (relayState != null && !relayState.isEmpty()) {
signedQuery += "&RelayState=" + relayState;
}
signedQuery += "&SigAlg=" + request.getEncodedParameter("SigAlg", signAlg);
if (!Util.validateBinarySignature(signedQuery, Util.base64decoder(signature), certList, signAlg)) {
throw new ValidationError("Signature validation failed. Logout Request rejected", ValidationError.INVALID_SIGNATURE);
}
}
LOGGER.debug("LogoutRequest validated --> " + logoutRequestString);
return true;
} catch (Exception e) {
validationException = e;
LOGGER.debug("LogoutRequest invalid --> " + logoutRequestString);
LOGGER.error(validationException.getMessage());
return false;
}
}
/**
* Returns the ID of the Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @return the ID of the Logout Request.
*/
public static String getId(Document samlLogoutRequestDocument) {
String id = null;
try {
Element rootElement = samlLogoutRequestDocument.getDocumentElement();
rootElement.normalize();
id = rootElement.getAttribute("ID");
} catch (Exception e) {}
return id;
}
/**
* Returns the issue instant of the Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @return the issue instant of the Logout Request.
*/
public static Calendar getIssueInstant(Document samlLogoutRequestDocument) {
Calendar issueInstant = null;
try {
Element rootElement = samlLogoutRequestDocument.getDocumentElement();
rootElement.normalize();
String issueInstantString = rootElement.hasAttribute(
"IssueInstant")? rootElement.getAttribute("IssueInstant"): null;
if(issueInstantString == null)
return null;
issueInstant = Calendar.getInstance();
issueInstant.setTimeInMillis(Util.parseDateTime(issueInstantString).toEpochMilli());
} catch (Exception e) {}
return issueInstant;
}
/**
* Returns the ID of the Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
*
* @return the ID of the Logout Request.
*
*/
public static String getId(String samlLogoutRequestString) {
Document doc = Util.loadXML(samlLogoutRequestString);
return getId(doc);
}
/**
* Returns the issue instant of the Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @return the issue instant of the Logout Request.
*/
public static Calendar getIssueInstant(String samlLogoutRequestString) {
Document doc = Util.loadXML(samlLogoutRequestString);
return getIssueInstant(doc);
}
/**
* Gets the NameID Data from the the Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
* @param key
* The SP key to decrypt the NameID if encrypted
*
* @return the Name ID Data (Value, Format, NameQualifier, SPNameQualifier)
*
* @throws Exception
*/
public static Map<String, String> getNameIdData(Document samlLogoutRequestDocument, PrivateKey key) throws Exception {
return getNameIdData(samlLogoutRequestDocument, key, false);
}
/**
* Gets the NameID Data from the the Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
* @param key
* The SP key to decrypt the NameID if encrypted
* @param trimValue
* whether the extracted Name ID value should be trimmed
*
* @return the Name ID Data (Value, Format, NameQualifier, SPNameQualifier)
*
* @throws Exception
*/
public static Map<String, String> getNameIdData(Document samlLogoutRequestDocument, PrivateKey key, boolean trimValue) throws Exception {
NodeList encryptedIDNodes = Util.query(samlLogoutRequestDocument, "/samlp:LogoutRequest/saml:EncryptedID");
NodeList nameIdNodes;
Element nameIdElem;
if (encryptedIDNodes.getLength() == 1) {
if (key == null) {
throw new SettingsException("Key is required in order to decrypt the NameID", SettingsException.PRIVATE_KEY_NOT_FOUND);
}
Element encryptedData = (Element) encryptedIDNodes.item(0);
Util.decryptElement(encryptedData, key);
nameIdNodes = Util.query(samlLogoutRequestDocument, "/samlp:LogoutRequest/saml:NameID");
if (nameIdNodes == null || nameIdNodes.getLength() != 1) {
throw new Exception("Not able to decrypt the EncryptedID and get a NameID");
}
}
else {
nameIdNodes = Util.query(samlLogoutRequestDocument, "/samlp:LogoutRequest/saml:NameID");
}
if (nameIdNodes != null && nameIdNodes.getLength() == 1) {
nameIdElem = (Element) nameIdNodes.item(0);
} else {
throw new ValidationError("No name id found in Logout Request.", ValidationError.NO_NAMEID);
}
Map<String, String> nameIdData = new HashMap<String, String>();
if (nameIdElem != null) {
String value = nameIdElem.getTextContent();
if(value != null && trimValue) {
value = value.trim();
}
nameIdData.put("Value", value);
if (nameIdElem.hasAttribute("Format")) {
nameIdData.put("Format", nameIdElem.getAttribute("Format"));
}
if (nameIdElem.hasAttribute("SPNameQualifier")) {
nameIdData.put("SPNameQualifier", nameIdElem.getAttribute("SPNameQualifier"));
}
if (nameIdElem.hasAttribute("NameQualifier")) {
nameIdData.put("NameQualifier", nameIdElem.getAttribute("NameQualifier"));
}
}
return nameIdData;
}
/**
* Gets the NameID Data from the the Logout Request String.
*
* @param samlLogoutRequestString
* A DOMDocument object loaded from the SAML Logout Request.
* @param key
* The SP key to decrypt the NameID if encrypted
*
* @return the Name ID Data (Value, Format, NameQualifier, SPNameQualifier)
*
* @throws Exception
*/
public static Map<String, String> getNameIdData(String samlLogoutRequestString, PrivateKey key) throws Exception {
return getNameIdData(samlLogoutRequestString, key, false);
}
/**
* Gets the NameID Data from the the Logout Request String.
*
* @param samlLogoutRequestString
* A DOMDocument object loaded from the SAML Logout Request.
* @param key
* The SP key to decrypt the NameID if encrypted
* @param trimValue
* whether the extracted Name ID value should be trimmed
*
* @return the Name ID Data (Value, Format, NameQualifier, SPNameQualifier)
*
* @throws Exception
*/
public static Map<String, String> getNameIdData(String samlLogoutRequestString, PrivateKey key, boolean trimValue) throws Exception {
Document doc = Util.loadXML(samlLogoutRequestString);
return getNameIdData(doc, key, trimValue);
}
/**
* Gets the NameID value provided from the SAML Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @param key
* The SP key to decrypt the NameID if encrypted
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(Document samlLogoutRequestDocument, PrivateKey key) throws Exception {
return getNameId(samlLogoutRequestDocument, key, false);
}
/**
* Gets the NameID value provided from the SAML Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @param key
* The SP key to decrypt the NameID if encrypted
*
* @param trimValue
* whether the extracted Name ID value should be trimmed
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(Document samlLogoutRequestDocument, PrivateKey key, boolean trimValue)
throws Exception {
Map<String, String> nameIdData = getNameIdData(samlLogoutRequestDocument, key, trimValue);
LOGGER.debug("LogoutRequest has NameID --> " + nameIdData.get("Value"));
return nameIdData.get("Value");
}
/**
* Gets the NameID value provided from the SAML Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(Document samlLogoutRequestDocument) throws Exception {
return getNameId(samlLogoutRequestDocument, null);
}
/**
* Gets the NameID value provided from the SAML Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
* @param key
* The SP key to decrypt the NameID if encrypted
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(String samlLogoutRequestString, PrivateKey key) throws Exception {
return getNameId(samlLogoutRequestString, key, false);
}
/**
* Gets the NameID value provided from the SAML Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
* @param key
* The SP key to decrypt the NameID if encrypted
* @param trimValue
* whether the extracted Name ID value should be trimmed
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(String samlLogoutRequestString, PrivateKey key, boolean trimValue)
throws Exception {
Map<String, String> nameId = getNameIdData(samlLogoutRequestString, key, trimValue);
return nameId.get("Value");
}
/**
* Gets the NameID value provided from the SAML Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
*
* @return the Name ID value
*
* @throws Exception
*/
public static String getNameId(String samlLogoutRequestString) throws Exception {
return getNameId(samlLogoutRequestString, null);
}
/**
* Gets the Issuer from Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
*
* @return the issuer of the logout request
*
* @throws XPathExpressionException
*/
public static String getIssuer(Document samlLogoutRequestDocument) throws XPathExpressionException {
return getIssuer(samlLogoutRequestDocument, false);
}
/**
* Gets the Issuer from Logout Request Document.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
* @param trim
* whether the extracted issuer value should be trimmed
*
* @return the issuer of the logout request
*
* @throws XPathExpressionException
*/
public static String getIssuer(Document samlLogoutRequestDocument, boolean trim) throws XPathExpressionException {
String issuer = null;
NodeList nodes = Util.query(samlLogoutRequestDocument, "/samlp:LogoutRequest/saml:Issuer");
if (nodes.getLength() == 1) {
issuer = nodes.item(0).getTextContent();
}
if (issuer != null && trim) {
issuer = issuer.trim();
}
return issuer;
}
/**
* Gets the Issuer from Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
*
* @return the issuer of the logout request
*
* @throws XPathExpressionException
*/
public static String getIssuer(String samlLogoutRequestString) throws XPathExpressionException {
return getIssuer(samlLogoutRequestString, false);
}
/**
* Gets the Issuer from Logout Request String.
*
* @param samlLogoutRequestString
* A Logout Request string.
* @param trim
* whether the extracted issuer value should be trimmed
*
* @return the issuer of the logout request
*
* @throws XPathExpressionException
*/
public static String getIssuer(String samlLogoutRequestString, boolean trim) throws XPathExpressionException {
Document doc = Util.loadXML(samlLogoutRequestString);
return getIssuer(doc, trim);
}
/**
* Gets the SessionIndexes from the LogoutRequest.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
* @return the SessionIndexes
*
* @throws XPathExpressionException
*/
public static List<String> getSessionIndexes(Document samlLogoutRequestDocument) throws XPathExpressionException {
return getSessionIndexes(samlLogoutRequestDocument, false);
}
/**
* Gets the SessionIndexes from the LogoutRequest.
*
* @param samlLogoutRequestDocument
* A DOMDocument object loaded from the SAML Logout Request.
* @param trim
* whether the extracted session indexes should be trimmed
* @return the SessionIndexes
*
* @throws XPathExpressionException
*/
public static List<String> getSessionIndexes(Document samlLogoutRequestDocument, boolean trim)
throws XPathExpressionException {
List<String> sessionIndexes = new ArrayList<String>();
NodeList nodes = Util.query(samlLogoutRequestDocument, "/samlp:LogoutRequest/samlp:SessionIndex");
for (int i = 0; i < nodes.getLength(); i++) {
String sessionIndex = nodes.item(i).getTextContent();
if (sessionIndex != null) {
if (trim) {
sessionIndex = sessionIndex.trim();
}
sessionIndexes.add(sessionIndex);
}
}
return sessionIndexes;
}
/**
* Gets the SessionIndexes from the LogoutRequest.
*
* @param samlLogoutRequestString
* A Logout Request string.
* @return the SessionIndexes
*
* @throws XPathExpressionException
*/
public static List<String> getSessionIndexes(String samlLogoutRequestString) throws XPathExpressionException {
return getSessionIndexes(samlLogoutRequestString, false);
}
/**
* Gets the SessionIndexes from the LogoutRequest.
*
* @param samlLogoutRequestString
* A Logout Request string.
* @param trim
* whether the extracted session indexes should be trimmed
* @return the SessionIndexes
*
* @throws XPathExpressionException
*/
public static List<String> getSessionIndexes(String samlLogoutRequestString, boolean trim)
throws XPathExpressionException {
Document doc = Util.loadXML(samlLogoutRequestString);
return getSessionIndexes(doc, trim);
}
/**
* After execute a validation process, if fails this method returns the cause
*
* @return the cause of the validation error
*/
public String getError() {
if (validationException != null) {
return validationException.getMessage();
}
return null;
}
/**
* After execute a validation process, if fails this method returns the Exception object
*
* @return the cause of the validation error
*/
public Exception getValidationException() {
return validationException;
}
/**
* Sets the validation exception that this {@link LogoutRequest} should return
* when a validation error occurs.
*
* @param validationException
* the validation exception to set
*/
protected void setValidationException(Exception validationException) {
this.validationException = validationException;
}