Skip to content

Commit 5685f21

Browse files
committed
tag xml id attributes as valid id fields
1 parent 1830c35 commit 5685f21

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

com/onelogin/saml/Response.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.xml.sax.SAXException;
1919

2020
import com.onelogin.AccountSettings;
21+
import java.lang.reflect.Method;
22+
import org.w3c.dom.Element;
23+
import org.w3c.dom.Node;
2124

2225
public class Response {
2326

@@ -51,16 +54,20 @@ public boolean isValid() throws Exception {
5154
NodeList nodes = xmlDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
5255

5356
if(nodes==null || nodes.getLength()==0){
54-
throw new Exception("Can't find signature in document.");
55-
}
56-
57-
X509Certificate cert = certificate.getX509Cert();
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();
5865
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-
}
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

Comments
 (0)