1818import org .xml .sax .SAXException ;
1919
2020import com .onelogin .AccountSettings ;
21+ import java .lang .reflect .Method ;
22+ import org .w3c .dom .Element ;
23+ import org .w3c .dom .Node ;
2124
2225public class Response {
2326
@@ -47,20 +50,24 @@ public void loadXmlFromBase64(String response) throws ParserConfigurationExcepti
4750 loadXml (decodedS );
4851 }
4952
50- public boolean isValid () throws Exception {
51- NodeList nodes = xmlDoc .getElementsByTagNameNS (XMLSignature .XMLNS , "Signature" );
52-
53- if (nodes ==null || nodes .getLength ()==0 ){
54- throw new Exception ("Can't find signature in document." );
55- }
56-
57- X509Certificate cert = certificate .getX509Cert ();
58- DOMValidateContext ctx = new DOMValidateContext (cert .getPublicKey () , nodes .item (0 ));
59- XMLSignatureFactory sigF = XMLSignatureFactory .getInstance ("DOM" );
60- XMLSignature xmlSignature = sigF .unmarshalXMLSignature (ctx );
61-
62- return xmlSignature .validate (ctx );
63- }
53+ public boolean isValid () throws Exception {
54+ NodeList nodes = xmlDoc .getElementsByTagNameNS (XMLSignature .XMLNS , "Signature" );
55+
56+ if (nodes == null || nodes .getLength () == 0 ) {
57+ throw new Exception ("Can't find signature in document." );
58+ }
59+
60+ if (setIdAttributeExists ()) {
61+ tagIdAttributes (xmlDoc );
62+ }
63+
64+ X509Certificate cert = certificate .getX509Cert ();
65+ DOMValidateContext ctx = new DOMValidateContext (cert .getPublicKey (), nodes .item (0 ));
66+ XMLSignatureFactory sigF = XMLSignatureFactory .getInstance ("DOM" );
67+ XMLSignature xmlSignature = sigF .unmarshalXMLSignature (ctx );
68+
69+ return xmlSignature .validate (ctx );
70+ }
6471
6572 public String getNameId () throws Exception {
6673 NodeList nodes = xmlDoc .getElementsByTagNameNS ("urn:oasis:names:tc:SAML:2.0:assertion" , "NameID" );
@@ -71,4 +78,27 @@ public String getNameId() throws Exception {
7178
7279 return nodes .item (0 ).getTextContent ();
7380 }
81+
82+ private void tagIdAttributes (Document xmlDoc ) {
83+ NodeList nodeList = xmlDoc .getElementsByTagName ("*" );
84+ for (int i = 0 ; i < nodeList .getLength (); i ++) {
85+ Node node = nodeList .item (i );
86+ if (node .getNodeType () == Node .ELEMENT_NODE ) {
87+ if (node .getAttributes ().getNamedItem ("ID" ) != null ) {
88+ ((Element ) node ).setIdAttribute ("ID" , true );
89+ }
90+ }
91+ }
92+ }
93+
94+ private boolean setIdAttributeExists () {
95+ for (Method method : Element .class .getDeclaredMethods ()) {
96+ if (method .getName ().equals ("setIdAttribute" )) {
97+ return true ;
98+ }
99+ }
100+ return false ;
101+ }
102+
103+
74104}
0 commit comments