|
| 1 | +/** |
| 2 | + * ContainerProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2020 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.containerproxy.auth.impl.saml; |
| 22 | + |
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.opensaml.saml2.core.Attribute; |
| 25 | +import org.opensaml.xml.XMLObject; |
| 26 | +import org.opensaml.xml.schema.XSAny; |
| 27 | +import org.opensaml.xml.schema.XSString; |
| 28 | +import org.springframework.security.saml.SAMLCredential; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class AttributeUtils { |
| 33 | + |
| 34 | + public static String getAttributeValue(Attribute attribute) { |
| 35 | + // copied from Attribute class ... |
| 36 | + List<XMLObject> attributeValues = attribute.getAttributeValues(); |
| 37 | + if (attributeValues == null || attributeValues.size() == 0) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + XMLObject xmlValue = attributeValues.iterator().next(); |
| 41 | + return getString(xmlValue); |
| 42 | + } |
| 43 | + |
| 44 | + public static String[] getAttributeAsStringArray(Attribute attribute) { |
| 45 | + if (attribute == null) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + List<XMLObject> attributeValues = attribute.getAttributeValues(); |
| 49 | + if (attributeValues == null || attributeValues.size() == 0) { |
| 50 | + return new String[0]; |
| 51 | + } |
| 52 | + String[] result = new String[attributeValues.size()]; |
| 53 | + int i = 0; |
| 54 | + for (XMLObject attributeValue : attributeValues) { |
| 55 | + result[i++] = getString(attributeValue); |
| 56 | + } |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + private static String getString(XMLObject xmlValue) { |
| 61 | + if (xmlValue instanceof XSString) { |
| 62 | + return ((XSString) xmlValue).getValue(); |
| 63 | + } else if (xmlValue instanceof XSAny) { |
| 64 | + return ((XSAny) xmlValue).getTextContent(); |
| 65 | + } else { |
| 66 | + return null; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void logAttributes(Logger logger, SAMLCredential credential) { |
| 71 | + String userID = credential.getNameID().getValue(); |
| 72 | + List<Attribute> attributes = credential.getAttributes(); |
| 73 | + attributes.forEach((attribute) -> { |
| 74 | + logger.info(String.format("[SAML] User: \"%s\" => attribute => name=\"%s\"(\"%s\") => value \"%s\" - \"%s\"", |
| 75 | + userID, |
| 76 | + attribute.getName(), |
| 77 | + attribute.getFriendlyName(), |
| 78 | + getAttributeValue(attribute), |
| 79 | + String.join(", ", getAttributeAsStringArray(attribute)))); |
| 80 | + }); |
| 81 | + |
| 82 | + } |
| 83 | +} |
0 commit comments