GUACAMOLE-579: Change fromAttribute to canonicalize

This commit is contained in:
Virtually Nick
2019-06-21 12:54:42 -04:00
parent d8db630dbd
commit a6601a2bfd
5 changed files with 41 additions and 74 deletions

View File

@@ -1,33 +0,0 @@
/*
* 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.cas;
import org.apache.guacamole.token.TokenName;
/**
* Utility class for generating parameter token names.
*/
public class CASTokenName extends TokenName {
public static String fromAttribute(String name) {
return fromAttribute(name, "CAS_");
}
}

View File

@@ -119,7 +119,7 @@ public class TicketValidationService {
// Convert remaining attributes that have values to Strings // Convert remaining attributes that have values to Strings
for (Entry <String, Object> attr : ticketAttrs.entrySet()) { for (Entry <String, Object> attr : ticketAttrs.entrySet()) {
String tokenName = TokenName.fromAttribute(attr.getKey(), String tokenName = TokenName.canonicalize(attr.getKey(),
CAS_ATTRIBUTE_TOKEN_PREFIX); CAS_ATTRIBUTE_TOKEN_PREFIX);
Object value = attr.getValue(); Object value = attr.getValue();
if (value != null) if (value != null)

View File

@@ -315,7 +315,7 @@ public class AuthenticationProviderService {
// Convert each retrieved attribute into a corresponding token // Convert each retrieved attribute into a corresponding token
for (Object attrObj : attrSet) { for (Object attrObj : attrSet) {
LDAPAttribute attr = (LDAPAttribute)attrObj; LDAPAttribute attr = (LDAPAttribute)attrObj;
tokens.put(TokenName.fromAttribute(attr.getName(), tokens.put(TokenName.canonicalize(attr.getName(),
LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue()); LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue());
} }

View File

@@ -28,13 +28,13 @@ import java.util.regex.Pattern;
public abstract class TokenName { public abstract class TokenName {
/** /**
* Pattern which matches logical groupings of words within an * Pattern which matches logical groupings of words within a
* attribute name. This pattern is intended to match logical groupings * string. This pattern is intended to match logical groupings
* regardless of the naming convention used: "CamelCase", * regardless of the naming convention used: "CamelCase",
* "headlessCamelCase", "lowercase_with_underscores", * "headlessCamelCase", "lowercase_with_underscores",
* "lowercase-with-dashes" or even "aVery-INCONSISTENTMix_ofAllStyles". * "lowercase-with-dashes" or even "aVery-INCONSISTENTMix_ofAllStyles".
*/ */
private static final Pattern ATTRIBUTE_NAME_GROUPING = Pattern.compile( private static final Pattern STRING_NAME_GROUPING = Pattern.compile(
// "Camel" word groups // "Camel" word groups
"\\p{javaUpperCase}\\p{javaLowerCase}+" "\\p{javaUpperCase}\\p{javaLowerCase}+"
@@ -61,29 +61,29 @@ public abstract class TokenName {
/** /**
* Generates the name of the parameter token that should be populated with * Generates the name of the parameter token that should be populated with
* the value of the given attribute. The name of the attribute will * the given string. The provided string will be automatically transformed
* automatically be transformed from "CamelCase", "headlessCamelCase", * from "CamelCase", "headlessCamelCase", "lowercase_with_underscores",
* "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent * and "mixes_ofBoth_Styles" to consistent "UPPERCASE_WITH_UNDERSCORES".
* "UPPERCASE_WITH_UNDERSCORES". Each returned attribute will be prefixed * Each returned attribute will be prefixed with the string value provided
* with value provided in the prefix. The value provided in prefix will * in the prefix. The value provided in prefix will be prepended to the
* be prepended to the attribute name, but will itself not be transformed. * attribute name, but will itself not be transformed.
* *
* @param name * @param name
* The name of the attribute to use to generate the token name. * The string to be used to generate the token name.
* *
* @param prefix * @param prefix
* The prefix to prepend to the generated token name. * The prefix to prepend to the generated token name.
* *
* @return * @return
* The name of the parameter token that should be populated with the * The name of the parameter token that should be populated with the
* value of the attribute having the given name. * given string.
*/ */
public static String fromAttribute(final String name, final String prefix) { public static String canonicalize(final String name, final String prefix) {
// If even one logical word grouping cannot be found, default to // If even one logical word grouping cannot be found, default to
// simply converting the attribute to uppercase and adding the // simply converting the string to uppercase and adding the
// prefix // prefix
Matcher groupMatcher = ATTRIBUTE_NAME_GROUPING.matcher(name); Matcher groupMatcher = STRING_NAME_GROUPING.matcher(name);
if (!groupMatcher.find()) if (!groupMatcher.find())
return prefix + name.toUpperCase(); return prefix + name.toUpperCase();
@@ -102,19 +102,19 @@ public abstract class TokenName {
} }
/** /**
* Generate the name of a parameter from the value of the given attribute, * Generate the name of a parameter from the given string, and with a blank
* and with a blank prefix such that the token name will simply be the * prefix such that the token name will simply be the transformed version
* transformed version of the attribute name. * of the string.
* *
* @param name * @param name
* The name of the attribute to use to generate the token name. * The string to use to generate the token name.
* *
* @return * @return
* The name of the parameter token that should be populated with the * The name of the parameter token that should be populated with the
* value of the attribute having the given name. * given string.
*/ */
public static String fromAttribute(final String name) { public static String canonicalize(final String name) {
return fromAttribute(name, ""); return canonicalize(name, "");
} }
} }

View File

@@ -29,28 +29,28 @@ import org.junit.Test;
public class TokenNameTest { public class TokenNameTest {
/** /**
* Verifies that TokenName.fromAttribute() generates token names as * Verifies that TokenName.canonicalize() generates token names as
* specified, regardless of the naming convention of the attribute. * specified, regardless of the naming convention of the attribute.
*/ */
@Test @Test
public void testFromAttribute() { public void testCanonicalize() {
assertEquals("A", TokenName.fromAttribute("a")); assertEquals("A", TokenName.canonicalize("a"));
assertEquals("B", TokenName.fromAttribute("b")); assertEquals("B", TokenName.canonicalize("b"));
assertEquals("1", TokenName.fromAttribute("1")); assertEquals("1", TokenName.canonicalize("1"));
assertEquals("SOME_URL", TokenName.fromAttribute("someURL")); assertEquals("SOME_URL", TokenName.canonicalize("someURL"));
assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); assertEquals("LOWERCASE_WITH_DASHES", TokenName.canonicalize("lowercase-with-dashes"));
assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); assertEquals("HEADLESS_CAMEL_CASE", TokenName.canonicalize("headlessCamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase"));
assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.canonicalize("lowercase_with_underscores"));
assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.canonicalize("UPPERCASE_WITH_UNDERSCORES"));
assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.canonicalize("aVery-INCONSISTENTMix_ofAllStyles"));
assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); assertEquals("ABC_123_DEF_456", TokenName.canonicalize("abc123def456"));
assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); assertEquals("ABC_123_DEF_456", TokenName.canonicalize("ABC123DEF456"));
assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.canonicalize("WordAWordABWordABCWord"));
assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_")); assertEquals("AUTH_ATTRIBUTE", TokenName.canonicalize("Attribute", "AUTH_"));
assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_")); assertEquals("auth_SOMETHING", TokenName.canonicalize("Something", "auth_"));
} }
} }