1414import javax .xml .xpath .XPathExpressionException ;
1515
1616import com .onelogin .saml2 .model .hsm .HSM ;
17+
18+ import org .apache .commons .lang3 .StringUtils ;
1719import org .joda .time .DateTime ;
1820import org .joda .time .Instant ;
1921import org .slf4j .Logger ;
@@ -469,7 +471,10 @@ public Map<String,String> getNameIdData() throws Exception {
469471
470472 if (nameIdElem != null ) {
471473 String value = nameIdElem .getTextContent ();
472- if (settings .isStrict () && value .isEmpty ()) {
474+ if (value != null && settings .isTrimNameIds ()) {
475+ value = value .trim ();
476+ }
477+ if (settings .isStrict () && StringUtils .isEmpty (value )) {
473478 throw new ValidationError ("An empty NameID value found" , ValidationError .EMPTY_NAMEID );
474479 }
475480
@@ -596,7 +601,11 @@ public HashMap<String, List<String>> getAttributes() throws XPathExpressionExcep
596601 }
597602 for (int j = 0 ; j < childrens .getLength (); j ++) {
598603 if ("AttributeValue" .equals (childrens .item (j ).getLocalName ())) {
599- attrValues .add (childrens .item (j ).getTextContent ());
604+ String attrValue = childrens .item (j ).getTextContent ();
605+ if (attrValue != null && settings .isTrimAttributeValues ()) {
606+ attrValue = attrValue .trim ();
607+ }
608+ attrValues .add (attrValue );
600609 }
601610 }
602611
@@ -699,8 +708,11 @@ public List<String> getAudiences() throws XPathExpressionException {
699708 for (int i = 0 ; i < entries .getLength (); i ++) {
700709 if (entries .item (i ) != null ) {
701710 String value = entries .item (i ).getTextContent ();
702- if (value != null && !value .trim ().isEmpty ()) {
703- audiences .add (value .trim ());
711+ if (value != null ) {
712+ value = value .trim ();
713+ }
714+ if (!StringUtils .isEmpty (value )) {
715+ audiences .add (value );
704716 }
705717 }
706718 }
@@ -722,7 +734,11 @@ public String getResponseIssuer() throws XPathExpressionException, ValidationErr
722734 NodeList responseIssuer = Util .query (samlResponseDocument , "/samlp:Response/saml:Issuer" );
723735 if (responseIssuer .getLength () > 0 ) {
724736 if (responseIssuer .getLength () == 1 ) {
725- return responseIssuer .item (0 ).getTextContent ();
737+ String value = responseIssuer .item (0 ).getTextContent ();
738+ if (value != null && settings .isTrimNameIds ()) {
739+ value = value .trim ();
740+ }
741+ return value ;
726742 } else {
727743 throw new ValidationError ("Issuer of the Response is multiple." , ValidationError .ISSUER_MULTIPLE_IN_RESPONSE );
728744 }
@@ -745,7 +761,11 @@ public String getResponseIssuer() throws XPathExpressionException, ValidationErr
745761 public String getAssertionIssuer () throws XPathExpressionException , ValidationError {
746762 NodeList assertionIssuer = this .queryAssertion ("/saml:Issuer" );
747763 if (assertionIssuer .getLength () == 1 ) {
748- return assertionIssuer .item (0 ).getTextContent ();
764+ String value = assertionIssuer .item (0 ).getTextContent ();
765+ if (value != null && settings .isTrimNameIds ()) {
766+ value = value .trim ();
767+ }
768+ return value ;
749769 } else {
750770 throw new ValidationError ("Issuer of the Assertion not found or multiple." , ValidationError .ISSUER_NOT_FOUND_IN_ASSERTION );
751771 }
0 commit comments