Skip to content

Commit ba7d12c

Browse files
committed
Move SAML logging to separate class
1 parent 8112e62 commit ba7d12c

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/main/java/eu/openanalytics/containerproxy/auth/impl/saml/SAMLConfiguration.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040
import org.opensaml.saml2.metadata.provider.MetadataProvider;
4141
import org.opensaml.saml2.metadata.provider.MetadataProviderException;
4242
import org.opensaml.util.resource.ResourceException;
43+
import org.opensaml.xml.XMLObject;
4344
import org.opensaml.xml.parse.StaticBasicParserPool;
4445
import org.opensaml.xml.parse.XMLParserException;
46+
import org.opensaml.xml.schema.XSAny;
47+
import org.opensaml.xml.schema.XSString;
4548
import org.springframework.beans.factory.annotation.Qualifier;
4649
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4750
import org.springframework.context.annotation.Bean;
@@ -344,26 +347,17 @@ public SAMLFilterSet samlFilter() throws Exception {
344347

345348
private final Logger log = LogManager.getLogger(getClass());
346349

350+
351+
347352
@Bean
348353
public SAMLAuthenticationProvider samlAuthenticationProvider() {
349354
SAMLAuthenticationProvider samlAuthenticationProvider = new SAMLAuthenticationProvider();
350355
samlAuthenticationProvider.setUserDetails(new SAMLUserDetailsService() {
351356
@Override
352357
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
353-
List<Attribute> attributes = credential.getAttributes();
354358

355359
if (Boolean.parseBoolean(environment.getProperty(PROP_LOG_ATTRIBUTES, "false"))) {
356-
// don't use nameValue from below so that in the case this attribute isn't correctly setup,
357-
// we can still log the attribtues (and the correct attribute can be found)
358-
String userID = credential.getNameID().getValue();
359-
attributes.forEach((attribute) -> {
360-
log.info(String.format("[SAML] User: \"%s\" => attribute => name=\"%s\"(\"%s\") => value \"%s\" - \"%s\"",
361-
userID,
362-
attribute.getName(),
363-
attribute.getFriendlyName(),
364-
credential.getAttributeAsString(attribute.getName()),
365-
String.join(", ", credential.getAttributeAsStringArray(attribute.getName()))));
366-
});
360+
AttributeUtils.logAttributes(log, credential);
367361
}
368362

369363
String nameAttribute = environment.getProperty("proxy.saml.name-attribute", DEFAULT_NAME_ATTRIBUTE);

0 commit comments

Comments
 (0)