Repository: guacamole-client Updated Branches: refs/heads/master 3089e71e6 -> dfd433276
GUACAMOLE-524: Consistently generate token names from LDAP attributes with arbitrary naming conventions. Project: http://git-wip-us.apache.org/repos/asf/guacamole-client/repo Commit: http://git-wip-us.apache.org/repos/asf/guacamole-client/commit/cb30b148 Tree: http://git-wip-us.apache.org/repos/asf/guacamole-client/tree/cb30b148 Diff: http://git-wip-us.apache.org/repos/asf/guacamole-client/diff/cb30b148 Branch: refs/heads/master Commit: cb30b148b9fe8a2561dc1db9e134ee16c7845310 Parents: 98bd3ea Author: Michael Jumper <mjum...@apache.org> Authored: Fri Oct 5 00:12:50 2018 -0700 Committer: Michael Jumper <mjum...@apache.org> Committed: Fri Oct 5 12:47:26 2018 -0700 ---------------------------------------------------------------------- extensions/guacamole-auth-ldap/pom.xml | 8 ++ .../ldap/AuthenticationProviderService.java | 8 +- .../apache/guacamole/auth/ldap/TokenName.java | 106 +++++++++++++++++++ .../guacamole/auth/ldap/TokenNameTest.java | 53 ++++++++++ 4 files changed, 168 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/cb30b148/extensions/guacamole-auth-ldap/pom.xml ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-ldap/pom.xml b/extensions/guacamole-auth-ldap/pom.xml index 6872f6a..f0ddb54 100644 --- a/extensions/guacamole-auth-ldap/pom.xml +++ b/extensions/guacamole-auth-ldap/pom.xml @@ -153,6 +153,14 @@ <version>3.0</version> </dependency> + <!-- JUnit --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + </dependencies> </project> http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/cb30b148/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java index aa4382e..4f5d76c 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java @@ -54,12 +54,6 @@ public class AuthenticationProviderService { private final Logger logger = LoggerFactory.getLogger(AuthenticationProviderService.class); /** - * The prefix string to add to each parameter token generated from an LDAP - * attribute name. - */ - private static final String LDAP_ATTRIBUTE_TOKEN_PREFIX = "LDAP_ATTR_"; - - /** * Service for creating and managing connections to LDAP servers. */ @Inject @@ -302,7 +296,7 @@ public class AuthenticationProviderService { // Convert each retrieved attribute into a corresponding token for (Object attrObj : attrSet) { LDAPAttribute attr = (LDAPAttribute)attrObj; - tokens.put(LDAP_ATTRIBUTE_TOKEN_PREFIX + attr.getName(), attr.getStringValue()); + tokens.put(TokenName.fromAttribute(attr.getName()), attr.getStringValue()); } } http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/cb30b148/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java new file mode 100644 index 0000000..2a99c3d --- /dev/null +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.ldap; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Utility class for generating parameter token names. + */ +public class TokenName { + + /** + * The prefix string to add to each parameter token generated from an LDAP + * attribute name. + */ + private static final String LDAP_ATTRIBUTE_TOKEN_PREFIX = "LDAP_ATTR_"; + + /** + * Pattern which matches logical groupings of words within an LDAP + * attribute name. This pattern is intended to match logical groupings + * regardless of the naming convention used: "CamelCase", + * "headlessCamelCase", "lowercase_with_underscores", + * "lowercase-with-dashes" or even "aVery-INCONSISTENTMix_ofAllStyles". + */ + private static final Pattern LDAP_ATTRIBUTE_NAME_GROUPING = Pattern.compile( + + // "Camel" word groups + "\\p{javaUpperCase}\\p{javaLowerCase}+" + + // Groups of digits + + "|[0-9]+" + + // Groups of uppercase letters, excluding the uppercase letter + // which begins a following "Camel" group + + "|\\p{javaUpperCase}+(?!\\p{javaLowerCase})" + + // Groups of lowercase letters which match no other pattern + + "|\\p{javaLowerCase}+" + + // Groups of word characters letters which match no other pattern + + "|\\b\\w+\\b" + + ); + + /** + * This utility class should not be instantiated. + */ + private TokenName() {} + + /** + * Generates the name of the parameter token that should be populated with + * the value of the given LDAP attribute. The name of the LDAP attribute + * will automatically be transformed from "CamelCase", "headlessCamelCase", + * "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent + * "UPPERCASE_WITH_UNDERSCORES". Each returned attribute will be prefixed + * with "LDAP_ATTR_". + * + * @param name + * The name of the LDAP attribute to use to generate the token name. + * + * @return + * The name of the parameter token that should be populated with the + * value of the LDAP attribute having the given name. + */ + public static String fromAttribute(String name) { + + // If even one logical word grouping cannot be found, default to + // simply converting the attribute to uppercase and adding the + // prefix + Matcher groupMatcher = LDAP_ATTRIBUTE_NAME_GROUPING.matcher(name); + if (!groupMatcher.find()) + return LDAP_ATTRIBUTE_TOKEN_PREFIX + name.toUpperCase(); + + // Split the given name into logical word groups, separated by + // underscores and converted to uppercase + StringBuilder builder = new StringBuilder(LDAP_ATTRIBUTE_TOKEN_PREFIX); + builder.append(groupMatcher.group(0).toUpperCase()); + + while (groupMatcher.find()) { + builder.append("_"); + builder.append(groupMatcher.group(0).toUpperCase()); + } + + return builder.toString(); + + } + +} http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/cb30b148/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/TokenNameTest.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/TokenNameTest.java b/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/TokenNameTest.java new file mode 100644 index 0000000..3ce2cc8 --- /dev/null +++ b/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/TokenNameTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.ldap; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * Test which verifies automatic generation of LDAP-specific connection + * parameter token names. + */ +public class TokenNameTest { + + /** + * Verifies that TokenName.fromAttribute() generates token names as + * specified, regardless of the naming convention of the attribute. + */ + @Test + public void testFromAttribute() { + assertEquals("LDAP_ATTR_A", TokenName.fromAttribute("a")); + assertEquals("LDAP_ATTR_B", TokenName.fromAttribute("b")); + assertEquals("LDAP_ATTR_1", TokenName.fromAttribute("1")); + assertEquals("LDAP_ATTR_SOME_URL", TokenName.fromAttribute("someURL")); + assertEquals("LDAP_ATTR_LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); + assertEquals("LDAP_ATTR_HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); + assertEquals("LDAP_ATTR_CAMEL_CASE", TokenName.fromAttribute("CamelCase")); + assertEquals("LDAP_ATTR_CAMEL_CASE", TokenName.fromAttribute("CamelCase")); + assertEquals("LDAP_ATTR_LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); + assertEquals("LDAP_ATTR_UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("LDAP_ATTR_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("LDAP_ATTR_ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); + assertEquals("LDAP_ATTR_ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); + assertEquals("LDAP_ATTR_WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); + } + +}