Skip to content
Snippets Groups Projects
Commit fa9d3bcf authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6339 Debug Logs

parent a3310ad7
No related branches found
No related tags found
No related merge requests found
...@@ -56,6 +56,9 @@ import org.springframework.security.saml2.core.Saml2X509Credential; ...@@ -56,6 +56,9 @@ import org.springframework.security.saml2.core.Saml2X509Credential;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import lombok.extern.log4j.Log4j2;
@Log4j2
@Service @Service
public class Saml2Decrypter { public class Saml2Decrypter {
private Decrypter decrypter; private Decrypter decrypter;
...@@ -102,6 +105,7 @@ public class Saml2Decrypter { ...@@ -102,6 +105,7 @@ public class Saml2Decrypter {
try { try {
return decrypter.decrypt(assertion); return decrypter.decrypt(assertion);
} catch (Exception ex) { } catch (Exception ex) {
LOG.error("failed to decrypt assertion {}", assertion);
throw new Saml2Exception(ex); throw new Saml2Exception(ex);
} }
} }
...@@ -115,12 +119,14 @@ public class Saml2Decrypter { ...@@ -115,12 +119,14 @@ public class Saml2Decrypter {
private Saml2X509Credential getDecryptionCredential() { private Saml2X509Credential getDecryptionCredential() {
var privateKey = readPrivateKey(decryptionPrivateKeyLocation); var privateKey = readPrivateKey(decryptionPrivateKeyLocation);
var certificate = readCertificateFromResource(decryptionCertificateLocation); var certificate = readCertificateFromResource(decryptionCertificateLocation);
return new Saml2X509Credential(privateKey, certificate, Saml2X509Credential.Saml2X509CredentialType.DECRYPTION); return new Saml2X509Credential(privateKey, certificate, Saml2X509Credential.Saml2X509CredentialType.DECRYPTION);
} }
private RSAPrivateKey readPrivateKey(Resource location) { private RSAPrivateKey readPrivateKey(Resource location) {
Assert.state(location != null, "No private key location specified"); Assert.state(location != null, "No private key location specified");
Assert.state(location.exists(), () -> "Private key location '" + location + "' does not exist"); Assert.state(location.exists(), () -> "Private key location '" + location + "' does not exist");
try (var inputStream = location.getInputStream()) { try (var inputStream = location.getInputStream()) {
return RsaKeyConverters.pkcs8().convert(inputStream); return RsaKeyConverters.pkcs8().convert(inputStream);
} catch (IOException e) { } catch (IOException e) {
...@@ -131,8 +137,8 @@ public class Saml2Decrypter { ...@@ -131,8 +137,8 @@ public class Saml2Decrypter {
private X509Certificate readCertificateFromResource(Resource location) { private X509Certificate readCertificateFromResource(Resource location) {
Assert.state(location != null, "No certificate location specified"); Assert.state(location != null, "No certificate location specified");
Assert.state(location.exists(), () -> "Certificate location '" + location + "' does not exist"); Assert.state(location.exists(), () -> "Certificate location '" + location + "' does not exist");
try (var inputStream = location.getInputStream()) {
try (var inputStream = location.getInputStream()) {
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inputStream); return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inputStream);
} catch (IOException | CertificateException e) { } catch (IOException | CertificateException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
......
...@@ -25,12 +25,10 @@ ...@@ -25,12 +25,10 @@
package de.ozgcloud.fachstelle.security; package de.ozgcloud.fachstelle.security;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport; import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.core.xml.io.UnmarshallingException; import org.opensaml.core.xml.io.UnmarshallingException;
import org.opensaml.saml.saml2.core.Response; import org.opensaml.saml.saml2.core.Response;
...@@ -39,23 +37,23 @@ import org.springframework.security.saml2.Saml2Exception; ...@@ -39,23 +37,23 @@ import org.springframework.security.saml2.Saml2Exception;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException; import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
import net.shibboleth.utilities.java.support.xml.BasicParserPool; import net.shibboleth.utilities.java.support.xml.BasicParserPool;
import net.shibboleth.utilities.java.support.xml.ParserPool; import net.shibboleth.utilities.java.support.xml.ParserPool;
import net.shibboleth.utilities.java.support.xml.XMLParserException; import net.shibboleth.utilities.java.support.xml.XMLParserException;
@Log4j2
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Saml2Parser { public class Saml2Parser {
static Response parse(String samlResponse) { static Response parse(String samlResponse) {
return (Response) getXmlObject(new ByteArrayInputStream(samlResponse.getBytes(StandardCharsets.UTF_8)));
}
static XMLObject getXmlObject(InputStream inputStream) throws Saml2Exception {
try { try {
var inputStream = new ByteArrayInputStream(samlResponse.getBytes(StandardCharsets.UTF_8));
var document = getParserPool().parse(inputStream); var document = getParserPool().parse(inputStream);
return getResponseUnmarshaller().unmarshall(document.getDocumentElement()); return (Response) getResponseUnmarshaller().unmarshall(document.getDocumentElement());
} catch (ComponentInitializationException | XMLParserException | UnmarshallingException e) { } catch (ComponentInitializationException | XMLParserException | UnmarshallingException e) {
LOG.error("failed to parse samlResponse {}", samlResponse);
throw new Saml2Exception("Failed to parse samlResponse", e); throw new Saml2Exception("Failed to parse samlResponse", e);
} }
} }
......
...@@ -27,7 +27,6 @@ import java.util.Objects; ...@@ -27,7 +27,6 @@ import java.util.Objects;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.opensaml.core.xml.XMLObject; import org.opensaml.core.xml.XMLObject;
import org.opensaml.saml.saml2.core.Attribute;
import org.springframework.security.saml2.Saml2Exception; import org.springframework.security.saml2.Saml2Exception;
import org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal; import org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -95,9 +94,11 @@ class UserAttributeProvider { ...@@ -95,9 +94,11 @@ class UserAttributeProvider {
var addressBuilder = new StringBuilder(); var addressBuilder = new StringBuilder();
for (XMLObject node : addressPartNodes) { for (XMLObject node : addressPartNodes) {
if (node instanceof Attribute attribute) { var attributeName = node.getElementQName().getLocalPart();
var attributeName = attribute.getName(); var textContent = Objects.requireNonNull(node.getDOM()).getTextContent().trim();
var textContent = Objects.requireNonNull(attribute.getAttributeValues().getFirst().getDOM()).getTextContent().trim();
LOG.info("NodeName: {}", attributeName);
LOG.info("TextContent: {}", textContent);
switch (attributeName) { switch (attributeName) {
case SAML_XML_STRASSE_NODE_NAME, SAML_XML_PLZ_NODE_NAME -> addressBuilder.append(textContent).append(" "); case SAML_XML_STRASSE_NODE_NAME, SAML_XML_PLZ_NODE_NAME -> addressBuilder.append(textContent).append(" ");
...@@ -105,9 +106,11 @@ class UserAttributeProvider { ...@@ -105,9 +106,11 @@ class UserAttributeProvider {
case SAML_XML_LAND_NODE_NAME -> addressBuilder.append(textContent); case SAML_XML_LAND_NODE_NAME -> addressBuilder.append(textContent);
} }
} }
}
return addressBuilder.toString().trim(); var address = addressBuilder.toString().trim();
LOG.info("Address: {}", address);
return address;
} }
} catch (IllegalArgumentException | Saml2Exception | NoSuchElementException e) { } catch (IllegalArgumentException | Saml2Exception | NoSuchElementException e) {
LOG.error("Failed parsing company address from SamlResponse: {}", samlResponse); LOG.error("Failed parsing company address from SamlResponse: {}", samlResponse);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment