use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project keycloak by keycloak.
the class SamlDescriptorIDPKeysExtractor method parse.
public MultivaluedHashMap<String, KeyInfo> parse(InputStream stream) throws ParsingException {
MultivaluedHashMap<String, KeyInfo> res = new MultivaluedHashMap<>();
try {
DocumentBuilder builder = DocumentUtil.getDocumentBuilder();
Document doc = builder.parse(stream);
XPathExpression expr = xpath.compile("//m:EntityDescriptor/m:IDPSSODescriptor/m:KeyDescriptor");
NodeList keyDescriptors = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < keyDescriptors.getLength(); i++) {
Node keyDescriptor = keyDescriptors.item(i);
Element keyDescriptorEl = (Element) keyDescriptor;
KeyInfo ki = processKeyDescriptor(keyDescriptorEl);
if (ki != null) {
String use = keyDescriptorEl.getAttribute(JBossSAMLConstants.USE.get());
res.add(use, ki);
}
}
} catch (SAXException | IOException | ParserConfigurationException | MarshalException | XPathExpressionException e) {
throw new ParsingException("Error parsing SAML descriptor", e);
}
return res;
}
use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.
the class KeyInfoTest method testgetContent.
@org.junit.Test
@SuppressWarnings("unchecked")
public void testgetContent() {
KeyInfo[] infos = new KeyInfo[2];
infos[0] = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "skeleton");
infos[1] = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")));
for (int j = 0; j < infos.length; j++) {
KeyInfo ki = infos[j];
List<XMLStructure> li = ki.getContent();
assertNotNull(ki.getContent());
Object[] content = li.toArray();
for (int i = 0; i < content.length; i++) {
if (!(content[i] instanceof XMLStructure)) {
fail("KeyInfo element has the wrong type");
}
}
}
}
use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.
the class KeyInfoTest method testMarshal.
@org.junit.Test
public void testMarshal() throws Exception {
KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "keyid");
try {
ki.marshal(null, null);
fail("Should raise a NullPointerException");
} catch (NullPointerException npe) {
}
Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
Element elem = doc.createElementNS("http://acme.org", "parent");
doc.appendChild(elem);
DOMStructure parent = new DOMStructure(elem);
try {
ki.marshal(parent, null);
} catch (Exception e) {
fail("Should not throw an exception: " + e);
}
Element kiElem = DOMUtils.getFirstChildElement(elem);
if (!kiElem.getLocalName().equals("KeyInfo")) {
fail("Should be KeyInfo element: " + kiElem.getLocalName());
}
Element knElem = DOMUtils.getFirstChildElement(kiElem);
if (!knElem.getLocalName().equals("KeyName")) {
fail("Should be KeyName element: " + knElem.getLocalName());
}
// check if key info is inserted before nextSibling
doc = XMLUtils.createDocumentBuilder(false).newDocument();
elem = doc.createElementNS("http://acme.org", "parent");
doc.appendChild(elem);
Element nextSib = doc.createElementNS("http://acme.org", "nextSib");
elem.appendChild(nextSib);
Key key = new Key() {
private static final long serialVersionUID = 1L;
@Override
public String getAlgorithm() {
return null;
}
@Override
public byte[] getEncoded() {
return null;
}
@Override
public String getFormat() {
return null;
}
};
DOMSignContext ctx = new DOMSignContext(key, elem, nextSib);
parent = new DOMStructure(elem);
ki.marshal(parent, ctx);
// no need for catching/calling fail() explicitly ... if it fails, it fails ...
assertEquals(elem.getFirstChild().getLocalName(), "KeyInfo");
}
use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.
the class KeyInfoTest method testisFeatureSupported.
@org.junit.Test
public void testisFeatureSupported() {
KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), "keyid");
try {
ki.isFeatureSupported(null);
fail("Should raise a NPE for null feature");
} catch (NullPointerException npe) {
}
assertTrue(!ki.isFeatureSupported("not supported"));
}
use of javax.xml.crypto.dsig.keyinfo.KeyInfo in project santuario-java by apache.
the class KeyInfoTest method testConstructor.
@org.junit.Test
public void testConstructor() {
final String id = "keyId";
// test newKeyInfo(List, String id)
KeyInfo ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")), id);
assertEquals(id, ki.getId());
try {
ki = fac.newKeyInfo(null, id);
fail("Should raise a NullPointerException");
} catch (NullPointerException npe) {
}
// test newKeyInfo(List)
ki = fac.newKeyInfo(Collections.singletonList(fac.newKeyName("foo")));
}
Aggregations