|
| 1 | +package io.github.danilopiazza.spring.boot.saml.security; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Reader; |
| 5 | +import java.io.StringReader; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import org.opensaml.core.xml.XMLObject; |
| 12 | +import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport; |
| 13 | +import org.opensaml.core.xml.io.UnmarshallingException; |
| 14 | +import org.opensaml.core.xml.schema.XSString; |
| 15 | +import org.opensaml.core.xml.schema.impl.XSAnyImpl; |
| 16 | +import org.opensaml.saml.saml2.core.Response; |
| 17 | +import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | +import org.w3c.dom.Element; |
| 20 | + |
| 21 | +import net.shibboleth.utilities.java.support.component.ComponentInitializationException; |
| 22 | +import net.shibboleth.utilities.java.support.xml.BasicParserPool; |
| 23 | +import net.shibboleth.utilities.java.support.xml.XMLParserException; |
| 24 | + |
| 25 | +@Service |
| 26 | +public class Saml2AttributeService { |
| 27 | + public Map<String, List<String>> getAttributes(Saml2Authentication authentication) { |
| 28 | + Element element = getDocumentElement(authentication); |
| 29 | + Response response = getResponse(element); |
| 30 | + return response.getAssertions().stream().flatMap(assertion -> assertion.getAttributeStatements().stream()) |
| 31 | + .flatMap(attributeStatement -> attributeStatement.getAttributes().stream()) |
| 32 | + .collect(Collectors.toMap(attribute -> attribute.getName(), |
| 33 | + attribute -> getAttributeValues(attribute.getAttributeValues()))); |
| 34 | + } |
| 35 | + |
| 36 | + private Element getDocumentElement(Saml2Authentication authentication) { |
| 37 | + try (Reader reader = new StringReader(authentication.getSaml2Response())) { |
| 38 | + BasicParserPool basicParserPool = new BasicParserPool(); |
| 39 | + basicParserPool.initialize(); |
| 40 | + return basicParserPool.parse(reader).getDocumentElement(); |
| 41 | + } catch (ComponentInitializationException | IOException | XMLParserException e) { |
| 42 | + throw new IllegalArgumentException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private Response getResponse(Element element) { |
| 47 | + try { |
| 48 | + return (Response) XMLObjectProviderRegistrySupport.getUnmarshallerFactory().getUnmarshaller(element) |
| 49 | + .unmarshall(element); |
| 50 | + } catch (UnmarshallingException e) { |
| 51 | + throw new IllegalArgumentException(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private List<String> getAttributeValues(Collection<XMLObject> collection) { |
| 56 | + return collection.stream().map(this::getAttributeValue).collect(Collectors.toList()); |
| 57 | + } |
| 58 | + |
| 59 | + private String getAttributeValue(XMLObject attributeValue) { |
| 60 | + return attributeValue == null ? null |
| 61 | + : attributeValue instanceof XSString ? getStringAttributeValue((XSString) attributeValue) |
| 62 | + : attributeValue instanceof XSAnyImpl ? getAnyAttributeValue((XSAnyImpl) attributeValue) |
| 63 | + : attributeValue.toString(); |
| 64 | + } |
| 65 | + |
| 66 | + private String getStringAttributeValue(XSString attributeValue) { |
| 67 | + return attributeValue.getValue(); |
| 68 | + } |
| 69 | + |
| 70 | + private String getAnyAttributeValue(XSAnyImpl attributeValue) { |
| 71 | + return attributeValue.getTextContent(); |
| 72 | + } |
| 73 | +} |
0 commit comments