@@ -147,23 +147,23 @@ public boolean isValid(String... requestId){
147147 throw new Exception ("There is an EncryptedAttribute in the Response and this SP not support them" );
148148 }
149149
150+ // Check Audience
151+ Set <String > validAudiences = this .getAudiences ();
152+
153+ if (!validAudiences .isEmpty () && !this .audienceUrl .equals (currentUrl )) {
154+ throw new Exception ( this .audienceUrl + " is not a valid audience for this Response" );
155+ }
156+
150157 // Check destination
151158 if (rootElement .hasAttribute ("Destination" )){
152159 String destinationUrl = rootElement .getAttribute ("Destination" );
153160 if (destinationUrl != null ) {
154- if (!destinationUrl .equals (currentUrl )){
161+ if (!destinationUrl .isEmpty () && ! destinationUrl . equals (currentUrl )){
155162 throw new Exception ("The response was received at " + currentUrl + " instead of " + destinationUrl );
156163 }
157164 }
158165 }
159166
160- // Check Audience
161- Set <String > validAudiences = this .getAudiences ();
162-
163- if (validAudiences .isEmpty () || !this .audienceUrl .equals (currentUrl )) {
164- throw new Exception ( this .audienceUrl + " is not a valid audience for this Response" );
165- }
166-
167167 // Check the issuers
168168 Set <String > issuers = this .getIssuers ();
169169 for (String issuer : issuers ){
@@ -196,7 +196,7 @@ public boolean isValid(String... requestId){
196196 if (subjectConfirmationDataNodes .item (c ).getLocalName ().equals ("SubjectConfirmationData" )){
197197
198198 Node recipient = subjectConfirmationDataNodes .item (c ).getAttributes ().getNamedItem ("Recipient" );
199- if (recipient != null && !recipient .getNodeValue ().equals (currentUrl )){
199+ if (recipient != null && !recipient .getNodeValue ().isEmpty () && ! recipient . getNodeValue (). equals (currentUrl )){
200200 validSubjectConfirmation = false ;
201201 }
202202
@@ -314,13 +314,17 @@ public Set<String> getAudiences() throws XPathExpressionException
314314 NodeList entries = this .queryAssertion ("/saml:Conditions/saml:AudienceRestriction/saml:Audience" );
315315
316316 if (entries .getLength () > 0 ){
317- this .audienceUrl = entries .item (0 ).getChildNodes ().item (0 ).getNodeValue ();
317+ if (entries .item (0 )!= null && entries .item (0 ).getChildNodes ().getLength () > 0 ){
318+ this .audienceUrl = entries .item (0 ).getChildNodes ().item (0 ).getNodeValue ();
319+ }
318320 }
319321
320322 for (int i =0 ; i < entries .getLength (); i ++) {
321- String value = entries .item (i ).getTextContent ().trim ();
322- if (value != null && !value .isEmpty ()) {
323- audiences .add (value );
323+ if (entries .item (i ) != null ){
324+ String value = entries .item (i ).getTextContent ();
325+ if (value != null && !value .trim ().isEmpty ()) {
326+ audiences .add (value .trim ());
327+ }
324328 }
325329 }
326330 return audiences ;
0 commit comments