From 55c4144fbfe7820861b7ea3e1cc0d3b7849fc58f Mon Sep 17 00:00:00 2001 From: Nick Couchman Date: Sat, 8 Dec 2018 11:49:42 -0500 Subject: [PATCH 1/7] GUACAMOLE-579: Parse tokens from attributes provided by the CAS server. --- extensions/guacamole-auth-cas/pom.xml | 8 +++ .../cas/AuthenticationProviderService.java | 18 ++++--- .../auth/cas/CASAuthenticationProvider.java | 15 ++++++ .../guacamole/auth/cas/CASTokenName.java | 33 ++++++++++++ .../cas/ticket/TicketValidationService.java | 36 ++++++++----- ...tedUser.java => CASAuthenticatedUser.java} | 29 +++++++++- .../guacamole/auth/cas/CASTokenNameTest.java | 53 +++++++++++++++++++ .../ldap/AuthenticationProviderService.java | 2 +- .../guacamole/auth/ldap/LDAPTokenName.java | 33 ++++++++++++ ...enNameTest.java => LDAPTokenNameTest.java} | 30 +++++------ .../apache/guacamole/token}/TokenName.java | 35 ++++++------ .../apache/guacamole/token/TokenNameTest.java | 53 +++++++++++++++++++ 12 files changed, 288 insertions(+), 57 deletions(-) create mode 100644 extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java rename extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/{AuthenticatedUser.java => CASAuthenticatedUser.java} (69%) create mode 100644 extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java create mode 100644 extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java rename extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/{TokenNameTest.java => LDAPTokenNameTest.java} (50%) rename {extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap => guacamole-ext/src/main/java/org/apache/guacamole/token}/TokenName.java (75%) create mode 100644 guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java diff --git a/extensions/guacamole-auth-cas/pom.xml b/extensions/guacamole-auth-cas/pom.xml index 69ecab579..d8731203f 100644 --- a/extensions/guacamole-auth-cas/pom.xml +++ b/extensions/guacamole-auth-cas/pom.xml @@ -254,6 +254,14 @@ 2.5 provided + + + + junit + junit + 4.12 + test + diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java index d769d2e16..63999d8a9 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java @@ -22,6 +22,9 @@ package org.apache.guacamole.auth.cas; import com.google.inject.Inject; import com.google.inject.Provider; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import org.apache.guacamole.form.Field; import org.apache.guacamole.GuacamoleException; @@ -31,7 +34,7 @@ import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsExce import org.apache.guacamole.auth.cas.conf.ConfigurationService; import org.apache.guacamole.auth.cas.form.CASTicketField; import org.apache.guacamole.auth.cas.ticket.TicketValidationService; -import org.apache.guacamole.auth.cas.user.AuthenticatedUser; +import org.apache.guacamole.auth.cas.user.CASAuthenticatedUser; /** * Service providing convenience functions for the CAS AuthenticationProvider @@ -55,7 +58,7 @@ public class AuthenticationProviderService { * Provider for AuthenticatedUser objects. */ @Inject - private Provider authenticatedUserProvider; + private Provider authenticatedUserProvider; /** * Returns an AuthenticatedUser representing the user authenticated by the @@ -65,14 +68,14 @@ public class AuthenticationProviderService { * The credentials to use for authentication. * * @return - * An AuthenticatedUser representing the user authenticated by the + * A CASAuthenticatedUser representing the user authenticated by the * given credentials. * * @throws GuacamoleException * If an error occurs while authenticating the user, or if access is * denied. */ - public AuthenticatedUser authenticateUser(Credentials credentials) + public CASAuthenticatedUser authenticateUser(Credentials credentials) throws GuacamoleException { // Pull CAS ticket from request if present @@ -80,10 +83,11 @@ public class AuthenticationProviderService { if (request != null) { String ticket = request.getParameter(CASTicketField.PARAMETER_NAME); if (ticket != null) { - String username = ticketService.validateTicket(ticket, credentials); + Map tokens = ticketService.validateTicket(ticket, credentials); + String username = credentials.getUsername(); if (username != null) { - AuthenticatedUser authenticatedUser = authenticatedUserProvider.get(); - authenticatedUser.init(username, credentials); + CASAuthenticatedUser authenticatedUser = authenticatedUserProvider.get(); + authenticatedUser.init(username, credentials, tokens); return authenticatedUser; } } diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASAuthenticationProvider.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASAuthenticationProvider.java index ed51a31c9..5b4154ef8 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASAuthenticationProvider.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASAuthenticationProvider.java @@ -22,9 +22,12 @@ package org.apache.guacamole.auth.cas; import com.google.inject.Guice; import com.google.inject.Injector; import org.apache.guacamole.GuacamoleException; +import org.apache.guacamole.auth.cas.user.CASAuthenticatedUser; import org.apache.guacamole.net.auth.AbstractAuthenticationProvider; import org.apache.guacamole.net.auth.AuthenticatedUser; import org.apache.guacamole.net.auth.Credentials; +import org.apache.guacamole.net.auth.TokenInjectingUserContext; +import org.apache.guacamole.net.auth.UserContext; /** * Guacamole authentication backend which authenticates users using an @@ -71,5 +74,17 @@ public class CASAuthenticationProvider extends AbstractAuthenticationProvider { return authProviderService.authenticateUser(credentials); } + + @Override + public UserContext decorate(UserContext context, + AuthenticatedUser authenticatedUser, Credentials credentials) + throws GuacamoleException { + + if (!(authenticatedUser instanceof CASAuthenticatedUser)) + return context; + + return new TokenInjectingUserContext(context, + ((CASAuthenticatedUser) authenticatedUser).getTokens()); + } } diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java new file mode 100644 index 000000000..c5193e92b --- /dev/null +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java @@ -0,0 +1,33 @@ +/* + * 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_"); + } + +} diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java index bfc3b69fc..963dd245f 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java @@ -28,13 +28,15 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleServerException; +import org.apache.guacamole.auth.cas.CASTokenName; import org.apache.guacamole.auth.cas.conf.ConfigurationService; import org.apache.guacamole.net.auth.Credentials; -import org.apache.guacamole.net.auth.credentials.CredentialsInfo; -import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException; import org.jasig.cas.client.authentication.AttributePrincipal; import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.Cas20ProxyTicketValidator; @@ -60,9 +62,9 @@ public class TicketValidationService { private ConfigurationService confService; /** - * Validates and parses the given ID ticket, returning the username - * provided by the CAS server in the ticket. If the - * ticket is invalid an exception is thrown. + * Validates and parses the given ID ticket, returning a map of all + * available tokens for the given user based on attributes provided by the + * CAS server. If the ticket is invalid an exception is thrown. * * @param ticket * The ID ticket to validate and parse. @@ -72,13 +74,15 @@ public class TicketValidationService { * password values in. * * @return - * The username derived from the ticket. + * A Map all of tokens for the user parsed from attributes returned + * by the CAS server. * * @throws GuacamoleException * If the ID ticket is not valid or guacamole.properties could * not be parsed. */ - public String validateTicket(String ticket, Credentials credentials) throws GuacamoleException { + public Map validateTicket(String ticket, + Credentials credentials) throws GuacamoleException { // Retrieve the configured CAS URL, establish a ticket validator, // and then attempt to validate the supplied ticket. If that succeeds, @@ -88,9 +92,11 @@ public class TicketValidationService { validator.setAcceptAnyProxy(true); validator.setEncoding("UTF-8"); try { + Map tokens = new HashMap<>(); String confRedirectURI = confService.getRedirectURI(); Assertion a = validator.validate(ticket, confRedirectURI); AttributePrincipal principal = a.getPrincipal(); + Map ticketAttrs = principal.getAttributes(); // Retrieve username and set the credentials. String username = principal.getName(); @@ -98,23 +104,27 @@ public class TicketValidationService { credentials.setUsername(username); // Retrieve password, attempt decryption, and set credentials. - Object credObj = principal.getAttributes().get("credential"); + Object credObj = ticketAttrs.remove("credential"); if (credObj != null) { String clearPass = decryptPassword(credObj.toString()); if (clearPass != null && !clearPass.isEmpty()) credentials.setPassword(clearPass); } + + // Convert remaining attributes that have values to Strings + for (Entry attr : ticketAttrs.entrySet()) { + String tokenName = CASTokenName.fromAttribute(attr.getKey().toString()); + Object value = attr.getValue(); + if (value != null) + tokens.put(tokenName, value.toString()); + } - return username; + return tokens; } catch (TicketValidationException e) { throw new GuacamoleException("Ticket validation failed.", e); } - catch (Throwable t) { - logger.error("Error validating ticket with CAS server: {}", t.getMessage()); - throw new GuacamoleInvalidCredentialsException("CAS login failed.", CredentialsInfo.USERNAME_PASSWORD); - } } diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/AuthenticatedUser.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java similarity index 69% rename from extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/AuthenticatedUser.java rename to extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java index 081971e0c..37811f6fb 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/AuthenticatedUser.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java @@ -20,6 +20,8 @@ package org.apache.guacamole.auth.cas.user; import com.google.inject.Inject; +import java.util.Collections; +import java.util.Map; import org.apache.guacamole.net.auth.AbstractAuthenticatedUser; import org.apache.guacamole.net.auth.AuthenticationProvider; import org.apache.guacamole.net.auth.Credentials; @@ -29,7 +31,7 @@ import org.apache.guacamole.net.auth.Credentials; * username and particular set of credentials with the CAS authentication * provider. */ -public class AuthenticatedUser extends AbstractAuthenticatedUser { +public class CASAuthenticatedUser extends AbstractAuthenticatedUser { /** * Reference to the authentication provider associated with this @@ -42,6 +44,11 @@ public class AuthenticatedUser extends AbstractAuthenticatedUser { * The credentials provided when this user was authenticated. */ private Credentials credentials; + + /** + * Tokens associated with this authenticated user. + */ + private Map tokens; /** * Initializes this AuthenticatedUser using the given username and @@ -52,12 +59,30 @@ public class AuthenticatedUser extends AbstractAuthenticatedUser { * * @param credentials * The credentials provided when this user was authenticated. + * + * @param tokens + * A map of all the name/value pairs that should be available + * as tokens when connections are established with this user. */ - public void init(String username, Credentials credentials) { + public void init(String username, Credentials credentials, + Map tokens) { this.credentials = credentials; + this.tokens = Collections.unmodifiableMap(tokens); setIdentifier(username.toLowerCase()); } + /** + * Returns a Map containing the name/value pairs that can be applied + * as parameter tokens when connections are established by the user. + * + * @return + * A Map containing all of the name/value pairs that can be + * used as parameter tokens by this user. + */ + public Map getTokens() { + return tokens; + } + @Override public AuthenticationProvider getAuthenticationProvider() { return authProvider; diff --git a/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java b/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java new file mode 100644 index 000000000..56b42be83 --- /dev/null +++ b/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.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.cas; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * Test which verifies automatic generation of LDAP-specific connection + * parameter token names. + */ +public class CASTokenNameTest { + + /** + * Verifies that TokenName.fromAttribute() generates token names as + * specified, regardless of the naming convention of the attribute. + */ + @Test + public void testFromAttribute() { + assertEquals("CAS_A", CASTokenName.fromAttribute("a")); + assertEquals("CAS_B", CASTokenName.fromAttribute("b")); + assertEquals("CAS_1", CASTokenName.fromAttribute("1")); + assertEquals("CAS_SOME_URL", CASTokenName.fromAttribute("someURL")); + assertEquals("CAS_LOWERCASE_WITH_DASHES", CASTokenName.fromAttribute("lowercase-with-dashes")); + assertEquals("CAS_HEADLESS_CAMEL_CASE", CASTokenName.fromAttribute("headlessCamelCase")); + assertEquals("CAS_CAMEL_CASE", CASTokenName.fromAttribute("CamelCase")); + assertEquals("CAS_CAMEL_CASE", CASTokenName.fromAttribute("CamelCase")); + assertEquals("CAS_LOWERCASE_WITH_UNDERSCORES", CASTokenName.fromAttribute("lowercase_with_underscores")); + assertEquals("CAS_UPPERCASE_WITH_UNDERSCORES", CASTokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("CAS_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", CASTokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("CAS_ABC_123_DEF_456", CASTokenName.fromAttribute("abc123def456")); + assertEquals("CAS_ABC_123_DEF_456", CASTokenName.fromAttribute("ABC123DEF456")); + assertEquals("CAS_WORD_A_WORD_AB_WORD_ABC_WORD", CASTokenName.fromAttribute("WordAWordABWordABCWord")); + } + +} 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 d9d3ab150..db928d7e0 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 @@ -309,7 +309,7 @@ public class AuthenticationProviderService { // Convert each retrieved attribute into a corresponding token for (Object attrObj : attrSet) { LDAPAttribute attr = (LDAPAttribute)attrObj; - tokens.put(TokenName.fromAttribute(attr.getName()), attr.getStringValue()); + tokens.put(LDAPTokenName.fromAttribute(attr.getName()), attr.getStringValue()); } } diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java new file mode 100644 index 000000000..dfcc2f522 --- /dev/null +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java @@ -0,0 +1,33 @@ +/* + * 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 org.apache.guacamole.token.TokenName; + +/** + * Utility class for generating parameter token names. + */ +public class LDAPTokenName extends TokenName { + + public static String fromAttribute(String name) { + return fromAttribute(name, "LDAP_"); + } + +} \ No newline at end of file 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/LDAPTokenNameTest.java similarity index 50% rename from extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/TokenNameTest.java rename to extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/LDAPTokenNameTest.java index 2ba61dc20..80aba7dcf 100644 --- 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/LDAPTokenNameTest.java @@ -26,7 +26,7 @@ import org.junit.Test; * Test which verifies automatic generation of LDAP-specific connection * parameter token names. */ -public class TokenNameTest { +public class LDAPTokenNameTest { /** * Verifies that TokenName.fromAttribute() generates token names as @@ -34,20 +34,20 @@ public class TokenNameTest { */ @Test public void testFromAttribute() { - assertEquals("LDAP_A", TokenName.fromAttribute("a")); - assertEquals("LDAP_B", TokenName.fromAttribute("b")); - assertEquals("LDAP_1", TokenName.fromAttribute("1")); - assertEquals("LDAP_SOME_URL", TokenName.fromAttribute("someURL")); - assertEquals("LDAP_LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); - assertEquals("LDAP_HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); - assertEquals("LDAP_CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("LDAP_CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("LDAP_LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); - assertEquals("LDAP_UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); - assertEquals("LDAP_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); - assertEquals("LDAP_ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); - assertEquals("LDAP_ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); - assertEquals("LDAP_WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); + assertEquals("LDAP_A", LDAPTokenName.fromAttribute("a")); + assertEquals("LDAP_B", LDAPTokenName.fromAttribute("b")); + assertEquals("LDAP_1", LDAPTokenName.fromAttribute("1")); + assertEquals("LDAP_SOME_URL", LDAPTokenName.fromAttribute("someURL")); + assertEquals("LDAP_LOWERCASE_WITH_DASHES", LDAPTokenName.fromAttribute("lowercase-with-dashes")); + assertEquals("LDAP_HEADLESS_CAMEL_CASE", LDAPTokenName.fromAttribute("headlessCamelCase")); + assertEquals("LDAP_CAMEL_CASE", LDAPTokenName.fromAttribute("CamelCase")); + assertEquals("LDAP_CAMEL_CASE", LDAPTokenName.fromAttribute("CamelCase")); + assertEquals("LDAP_LOWERCASE_WITH_UNDERSCORES", LDAPTokenName.fromAttribute("lowercase_with_underscores")); + assertEquals("LDAP_UPPERCASE_WITH_UNDERSCORES", LDAPTokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("LDAP_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", LDAPTokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("LDAP_ABC_123_DEF_456", LDAPTokenName.fromAttribute("abc123def456")); + assertEquals("LDAP_ABC_123_DEF_456", LDAPTokenName.fromAttribute("ABC123DEF456")); + assertEquals("LDAP_WORD_A_WORD_AB_WORD_ABC_WORD", LDAPTokenName.fromAttribute("WordAWordABWordABCWord")); } } diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java similarity index 75% rename from extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java rename to guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java index 90de5bf84..90e17ded7 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/TokenName.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.guacamole.auth.ldap; +package org.apache.guacamole.token; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,22 +25,16 @@ import java.util.regex.Pattern; /** * Utility class for generating parameter token names. */ -public class TokenName { +public abstract 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_"; - - /** - * Pattern which matches logical groupings of words within an LDAP + * Pattern which matches logical groupings of words within an * 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( + private static final Pattern ATTRIBUTE_NAME_GROUPING = Pattern.compile( // "Camel" word groups "\\p{javaUpperCase}\\p{javaLowerCase}+" @@ -63,35 +57,38 @@ public class TokenName { /** * This utility class should not be instantiated. */ - private TokenName() {} + protected 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", + * the value of the given attribute. The name of the 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_". * * @param name - * The name of the LDAP attribute to use to generate the token name. + * The name of the attribute to use to generate the token name. + * + * @param prefix + * The prefix to prepend to the generated token name. * * @return * The name of the parameter token that should be populated with the - * value of the LDAP attribute having the given name. + * value of the attribute having the given name. */ - public static String fromAttribute(String name) { + public static String fromAttribute(final String name, final String prefix) { // 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); + Matcher groupMatcher = ATTRIBUTE_NAME_GROUPING.matcher(name); if (!groupMatcher.find()) - return LDAP_ATTRIBUTE_TOKEN_PREFIX + name.toUpperCase(); + return 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); + StringBuilder builder = new StringBuilder(prefix); builder.append(groupMatcher.group(0).toUpperCase()); while (groupMatcher.find()) { diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java new file mode 100644 index 000000000..f2d468e78 --- /dev/null +++ b/guacamole-ext/src/test/java/org/apache/guacamole/token/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.token; + +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("A", TokenName.fromAttribute("a", "")); + assertEquals("B", TokenName.fromAttribute("b", "")); + assertEquals("1", TokenName.fromAttribute("1", "")); + assertEquals("SOME_URL", TokenName.fromAttribute("someURL", "")); + assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes", "")); + assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase", "")); + assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); + assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); + assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores", "")); + assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES", "")); + assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles", "")); + assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456", "")); + assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456", "")); + assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord", "")); + } + +} From 8ab9e510096c38a19719c226639ee5823b06ed8c Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Tue, 7 May 2019 10:35:14 -0400 Subject: [PATCH 2/7] GUACAMOLE-579: Put back original init method for compatibility. --- .../auth/cas/user/CASAuthenticatedUser.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java index 37811f6fb..1b3a948cc 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/user/CASAuthenticatedUser.java @@ -52,7 +52,21 @@ public class CASAuthenticatedUser extends AbstractAuthenticatedUser { /** * Initializes this AuthenticatedUser using the given username and - * credentials. + * credentials, and an empty map of parameter tokens. + * + * @param username + * The username of the user that was authenticated. + * + * @param credentials + * The credentials provided when this user was authenticated. + */ + public void init(String username, Credentials credentials) { + this.init(username, credentials, Collections.emptyMap()); + } + + /** + * Initializes this AuthenticatedUser using the given username, + * credentials, and parameter tokens. * * @param username * The username of the user that was authenticated. From d8db630dbd56193642c84b8a19842ba476f7ad85 Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Thu, 20 Jun 2019 20:41:41 -0400 Subject: [PATCH 3/7] GUACAMOLE-579: Clean up comments, implement convenience method for tokens without a prefix, and remove unneeded classes. --- .../cas/AuthenticationProviderService.java | 2 - .../cas/ticket/TicketValidationService.java | 15 ++++-- .../guacamole/auth/cas/CASTokenNameTest.java | 53 ------------------- .../ldap/AuthenticationProviderService.java | 13 +++-- .../auth/ldap/LDAPConnectionService.java | 1 - .../guacamole/auth/ldap/LDAPTokenName.java | 33 ------------ .../auth/ldap/ReferralAuthHandler.java | 9 ++-- .../auth/ldap/LDAPTokenNameTest.java | 53 ------------------- .../org/apache/guacamole/token/TokenName.java | 19 ++++++- .../apache/guacamole/token/TokenNameTest.java | 31 ++++++----- 10 files changed, 62 insertions(+), 167 deletions(-) delete mode 100644 extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java delete mode 100644 extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java delete mode 100644 extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/LDAPTokenNameTest.java diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java index 63999d8a9..ceab17479 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java @@ -22,9 +22,7 @@ package org.apache.guacamole.auth.cas; import com.google.inject.Inject; import com.google.inject.Provider; import java.util.Arrays; -import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import org.apache.guacamole.form.Field; import org.apache.guacamole.GuacamoleException; diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java index 963dd245f..f3198de93 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java @@ -34,9 +34,9 @@ import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleServerException; -import org.apache.guacamole.auth.cas.CASTokenName; import org.apache.guacamole.auth.cas.conf.ConfigurationService; import org.apache.guacamole.net.auth.Credentials; +import org.apache.guacamole.token.TokenName; import org.jasig.cas.client.authentication.AttributePrincipal; import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.Cas20ProxyTicketValidator; @@ -54,6 +54,11 @@ public class TicketValidationService { * Logger for this class. */ private static final Logger logger = LoggerFactory.getLogger(TicketValidationService.class); + + /** + * The prefix to use when generating token names. + */ + public static final String CAS_ATTRIBUTE_TOKEN_PREFIX = "CAS_"; /** * Service for retrieving CAS configuration information. @@ -96,7 +101,8 @@ public class TicketValidationService { String confRedirectURI = confService.getRedirectURI(); Assertion a = validator.validate(ticket, confRedirectURI); AttributePrincipal principal = a.getPrincipal(); - Map ticketAttrs = principal.getAttributes(); + Map ticketAttrs = + new HashMap<>(principal.getAttributes()); // Retrieve username and set the credentials. String username = principal.getName(); @@ -112,8 +118,9 @@ public class TicketValidationService { } // Convert remaining attributes that have values to Strings - for (Entry attr : ticketAttrs.entrySet()) { - String tokenName = CASTokenName.fromAttribute(attr.getKey().toString()); + for (Entry attr : ticketAttrs.entrySet()) { + String tokenName = TokenName.fromAttribute(attr.getKey(), + CAS_ATTRIBUTE_TOKEN_PREFIX); Object value = attr.getValue(); if (value != null) tokens.put(tokenName, value.toString()); diff --git a/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java b/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java deleted file mode 100644 index 56b42be83..000000000 --- a/extensions/guacamole-auth-cas/src/test/java/org/apache/guacamole/auth/cas/CASTokenNameTest.java +++ /dev/null @@ -1,53 +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 static org.junit.Assert.assertEquals; -import org.junit.Test; - -/** - * Test which verifies automatic generation of LDAP-specific connection - * parameter token names. - */ -public class CASTokenNameTest { - - /** - * Verifies that TokenName.fromAttribute() generates token names as - * specified, regardless of the naming convention of the attribute. - */ - @Test - public void testFromAttribute() { - assertEquals("CAS_A", CASTokenName.fromAttribute("a")); - assertEquals("CAS_B", CASTokenName.fromAttribute("b")); - assertEquals("CAS_1", CASTokenName.fromAttribute("1")); - assertEquals("CAS_SOME_URL", CASTokenName.fromAttribute("someURL")); - assertEquals("CAS_LOWERCASE_WITH_DASHES", CASTokenName.fromAttribute("lowercase-with-dashes")); - assertEquals("CAS_HEADLESS_CAMEL_CASE", CASTokenName.fromAttribute("headlessCamelCase")); - assertEquals("CAS_CAMEL_CASE", CASTokenName.fromAttribute("CamelCase")); - assertEquals("CAS_CAMEL_CASE", CASTokenName.fromAttribute("CamelCase")); - assertEquals("CAS_LOWERCASE_WITH_UNDERSCORES", CASTokenName.fromAttribute("lowercase_with_underscores")); - assertEquals("CAS_UPPERCASE_WITH_UNDERSCORES", CASTokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); - assertEquals("CAS_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", CASTokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); - assertEquals("CAS_ABC_123_DEF_456", CASTokenName.fromAttribute("abc123def456")); - assertEquals("CAS_ABC_123_DEF_456", CASTokenName.fromAttribute("ABC123DEF456")); - assertEquals("CAS_WORD_A_WORD_AB_WORD_ABC_WORD", CASTokenName.fromAttribute("WordAWordABWordABCWord")); - } - -} 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 db928d7e0..ac1fee4ce 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 @@ -41,6 +41,7 @@ import org.apache.guacamole.net.auth.AuthenticatedUser; import org.apache.guacamole.net.auth.Credentials; import org.apache.guacamole.net.auth.credentials.CredentialsInfo; import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException; +import org.apache.guacamole.token.TokenName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +54,12 @@ public class AuthenticationProviderService { /** * Logger for this class. */ - private final Logger logger = LoggerFactory.getLogger(AuthenticationProviderService.class); + private static final Logger logger = LoggerFactory.getLogger(AuthenticationProviderService.class); + + /** + * The prefix that will be used when generating tokens. + */ + public static final String LDAP_ATTRIBUTE_TOKEN_PREFIX = "LDAP_"; /** * Service for creating and managing connections to LDAP servers. @@ -294,7 +300,7 @@ public class AuthenticationProviderService { String[] attrArray = attrList.toArray(new String[attrList.size()]); String userDN = getUserBindDN(username); - Map tokens = new HashMap(); + Map tokens = new HashMap<>(); try { // Get LDAP attributes by querying LDAP @@ -309,7 +315,8 @@ public class AuthenticationProviderService { // Convert each retrieved attribute into a corresponding token for (Object attrObj : attrSet) { LDAPAttribute attr = (LDAPAttribute)attrObj; - tokens.put(LDAPTokenName.fromAttribute(attr.getName()), attr.getStringValue()); + tokens.put(TokenName.fromAttribute(attr.getName(), + LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue()); } } diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java index f84912610..3aaf324c9 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPConnectionService.java @@ -28,7 +28,6 @@ import com.novell.ldap.LDAPJSSEStartTLSFactory; import java.io.UnsupportedEncodingException; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleUnsupportedException; -import org.apache.guacamole.auth.ldap.ReferralAuthHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java deleted file mode 100644 index dfcc2f522..000000000 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/LDAPTokenName.java +++ /dev/null @@ -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.ldap; - -import org.apache.guacamole.token.TokenName; - -/** - * Utility class for generating parameter token names. - */ -public class LDAPTokenName extends TokenName { - - public static String fromAttribute(String name) { - return fromAttribute(name, "LDAP_"); - } - -} \ No newline at end of file diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/ReferralAuthHandler.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/ReferralAuthHandler.java index e605b3c0b..a5e359a66 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/ReferralAuthHandler.java +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/ReferralAuthHandler.java @@ -19,12 +19,9 @@ package org.apache.guacamole.auth.ldap; -import com.google.inject.Inject; import com.novell.ldap.LDAPAuthHandler; import com.novell.ldap.LDAPAuthProvider; -import com.novell.ldap.LDAPConnection; import java.io.UnsupportedEncodingException; -import org.apache.guacamole.GuacamoleException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,6 +45,12 @@ public class ReferralAuthHandler implements LDAPAuthHandler { * Creates a ReferralAuthHandler object to handle authentication when * following referrals in a LDAP connection, using the provided dn and * password. + * + * @param dn + * The distinguished name to use for the referral login. + * + * @param password + * The password to use for the referral login. */ public ReferralAuthHandler(String dn, String password) { byte[] passwordBytes; diff --git a/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/LDAPTokenNameTest.java b/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/LDAPTokenNameTest.java deleted file mode 100644 index 80aba7dcf..000000000 --- a/extensions/guacamole-auth-ldap/src/test/java/org/apache/guacamole/auth/ldap/LDAPTokenNameTest.java +++ /dev/null @@ -1,53 +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.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 LDAPTokenNameTest { - - /** - * Verifies that TokenName.fromAttribute() generates token names as - * specified, regardless of the naming convention of the attribute. - */ - @Test - public void testFromAttribute() { - assertEquals("LDAP_A", LDAPTokenName.fromAttribute("a")); - assertEquals("LDAP_B", LDAPTokenName.fromAttribute("b")); - assertEquals("LDAP_1", LDAPTokenName.fromAttribute("1")); - assertEquals("LDAP_SOME_URL", LDAPTokenName.fromAttribute("someURL")); - assertEquals("LDAP_LOWERCASE_WITH_DASHES", LDAPTokenName.fromAttribute("lowercase-with-dashes")); - assertEquals("LDAP_HEADLESS_CAMEL_CASE", LDAPTokenName.fromAttribute("headlessCamelCase")); - assertEquals("LDAP_CAMEL_CASE", LDAPTokenName.fromAttribute("CamelCase")); - assertEquals("LDAP_CAMEL_CASE", LDAPTokenName.fromAttribute("CamelCase")); - assertEquals("LDAP_LOWERCASE_WITH_UNDERSCORES", LDAPTokenName.fromAttribute("lowercase_with_underscores")); - assertEquals("LDAP_UPPERCASE_WITH_UNDERSCORES", LDAPTokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); - assertEquals("LDAP_A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", LDAPTokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); - assertEquals("LDAP_ABC_123_DEF_456", LDAPTokenName.fromAttribute("abc123def456")); - assertEquals("LDAP_ABC_123_DEF_456", LDAPTokenName.fromAttribute("ABC123DEF456")); - assertEquals("LDAP_WORD_A_WORD_AB_WORD_ABC_WORD", LDAPTokenName.fromAttribute("WordAWordABWordABCWord")); - } - -} diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java index 90e17ded7..1e90114e5 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java @@ -65,7 +65,8 @@ public abstract class TokenName { * 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_". + * with value provided in the prefix. The value provided in prefix will + * be prepended to the attribute name, but will itself not be transformed. * * @param name * The name of the attribute to use to generate the token name. @@ -99,5 +100,21 @@ public abstract class TokenName { return builder.toString(); } + + /** + * Generate the name of a parameter from the value of the given attribute, + * and with a blank prefix such that the token name will simply be the + * transformed version of the attribute name. + * + * @param name + * The name of the attribute to use to generate the token name. + * + * @return + * The name of the parameter token that should be populated with the + * value of the attribute having the given name. + */ + public static String fromAttribute(final String name) { + return fromAttribute(name, ""); + } } diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java index f2d468e78..08c819158 100644 --- a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java +++ b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java @@ -34,20 +34,23 @@ public class TokenNameTest { */ @Test public void testFromAttribute() { - assertEquals("A", TokenName.fromAttribute("a", "")); - assertEquals("B", TokenName.fromAttribute("b", "")); - assertEquals("1", TokenName.fromAttribute("1", "")); - assertEquals("SOME_URL", TokenName.fromAttribute("someURL", "")); - assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes", "")); - assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase", "")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); - assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores", "")); - assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES", "")); - assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles", "")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456", "")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456", "")); - assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord", "")); + assertEquals("A", TokenName.fromAttribute("a")); + assertEquals("B", TokenName.fromAttribute("b")); + assertEquals("1", TokenName.fromAttribute("1")); + assertEquals("SOME_URL", TokenName.fromAttribute("someURL")); + assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); + assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); + assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); + assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); + assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); + assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); + assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); + assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); + + assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_")); + assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_")); } } From a6601a2bfde529fa58575721da0b2ad491c1e8a1 Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Fri, 21 Jun 2019 12:54:42 -0400 Subject: [PATCH 4/7] GUACAMOLE-579: Change fromAttribute to canonicalize --- .../guacamole/auth/cas/CASTokenName.java | 33 --------------- .../cas/ticket/TicketValidationService.java | 2 +- .../ldap/AuthenticationProviderService.java | 2 +- .../org/apache/guacamole/token/TokenName.java | 42 +++++++++---------- .../apache/guacamole/token/TokenNameTest.java | 36 ++++++++-------- 5 files changed, 41 insertions(+), 74 deletions(-) delete mode 100644 extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java deleted file mode 100644 index c5193e92b..000000000 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java +++ /dev/null @@ -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_"); - } - -} diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java index f3198de93..628b28dcf 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java @@ -119,7 +119,7 @@ public class TicketValidationService { // Convert remaining attributes that have values to Strings for (Entry attr : ticketAttrs.entrySet()) { - String tokenName = TokenName.fromAttribute(attr.getKey(), + String tokenName = TokenName.canonicalize(attr.getKey(), CAS_ATTRIBUTE_TOKEN_PREFIX); Object value = attr.getValue(); if (value != null) 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 ac1fee4ce..949d1c87d 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 @@ -315,7 +315,7 @@ public class AuthenticationProviderService { // Convert each retrieved attribute into a corresponding token for (Object attrObj : attrSet) { LDAPAttribute attr = (LDAPAttribute)attrObj; - tokens.put(TokenName.fromAttribute(attr.getName(), + tokens.put(TokenName.canonicalize(attr.getName(), LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue()); } diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java index 1e90114e5..4b36377e9 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java @@ -28,13 +28,13 @@ import java.util.regex.Pattern; public abstract class TokenName { /** - * Pattern which matches logical groupings of words within an - * attribute name. This pattern is intended to match logical groupings + * Pattern which matches logical groupings of words within a + * string. 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 ATTRIBUTE_NAME_GROUPING = Pattern.compile( + private static final Pattern STRING_NAME_GROUPING = Pattern.compile( // "Camel" word groups "\\p{javaUpperCase}\\p{javaLowerCase}+" @@ -61,29 +61,29 @@ public abstract class TokenName { /** * Generates the name of the parameter token that should be populated with - * the value of the given attribute. The name of the 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 value provided in the prefix. The value provided in prefix will - * be prepended to the attribute name, but will itself not be transformed. + * the given string. The provided string will be automatically transformed + * from "CamelCase", "headlessCamelCase", "lowercase_with_underscores", + * and "mixes_ofBoth_Styles" to consistent "UPPERCASE_WITH_UNDERSCORES". + * Each returned attribute will be prefixed with the string value provided + * in the prefix. The value provided in prefix will be prepended to the + * attribute name, but will itself not be transformed. * * @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 * The prefix to prepend to the generated token name. * * @return * 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 - // simply converting the attribute to uppercase and adding the + // simply converting the string to uppercase and adding the // prefix - Matcher groupMatcher = ATTRIBUTE_NAME_GROUPING.matcher(name); + Matcher groupMatcher = STRING_NAME_GROUPING.matcher(name); if (!groupMatcher.find()) 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, - * and with a blank prefix such that the token name will simply be the - * transformed version of the attribute name. + * Generate the name of a parameter from the given string, and with a blank + * prefix such that the token name will simply be the transformed version + * of the string. * * @param name - * The name of the attribute to use to generate the token name. + * The string to use to generate the token name. * * @return * 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) { - return fromAttribute(name, ""); + public static String canonicalize(final String name) { + return canonicalize(name, ""); } } diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java index 08c819158..7b9d3c2b1 100644 --- a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java +++ b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java @@ -29,28 +29,28 @@ import org.junit.Test; 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. */ @Test - public void testFromAttribute() { - assertEquals("A", TokenName.fromAttribute("a")); - assertEquals("B", TokenName.fromAttribute("b")); - assertEquals("1", TokenName.fromAttribute("1")); - assertEquals("SOME_URL", TokenName.fromAttribute("someURL")); - assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); - assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); - assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); - assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); - assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); + public void testCanonicalize() { + assertEquals("A", TokenName.canonicalize("a")); + assertEquals("B", TokenName.canonicalize("b")); + assertEquals("1", TokenName.canonicalize("1")); + assertEquals("SOME_URL", TokenName.canonicalize("someURL")); + assertEquals("LOWERCASE_WITH_DASHES", TokenName.canonicalize("lowercase-with-dashes")); + assertEquals("HEADLESS_CAMEL_CASE", TokenName.canonicalize("headlessCamelCase")); + assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase")); + assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase")); + assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.canonicalize("lowercase_with_underscores")); + assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.canonicalize("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.canonicalize("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("ABC_123_DEF_456", TokenName.canonicalize("abc123def456")); + assertEquals("ABC_123_DEF_456", TokenName.canonicalize("ABC123DEF456")); + assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.canonicalize("WordAWordABWordABCWord")); - assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_")); - assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_")); + assertEquals("AUTH_ATTRIBUTE", TokenName.canonicalize("Attribute", "AUTH_")); + assertEquals("auth_SOMETHING", TokenName.canonicalize("Something", "auth_")); } } From a2b3e235e04dc3efc48e62c2993b717527d069b9 Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Sat, 22 Jun 2019 19:27:33 -0400 Subject: [PATCH 5/7] GUACAMOLE-579: Fix up documentation issues. --- .../org/apache/guacamole/token/TokenName.java | 16 +++++++++------- .../apache/guacamole/token/TokenNameTest.java | 5 ++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java index 4b36377e9..ae83346a2 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java @@ -25,7 +25,7 @@ import java.util.regex.Pattern; /** * Utility class for generating parameter token names. */ -public abstract class TokenName { +public class TokenName { /** * Pattern which matches logical groupings of words within a @@ -57,16 +57,16 @@ public abstract class TokenName { /** * This utility class should not be instantiated. */ - protected TokenName() {} + private TokenName() {} /** * Generates the name of the parameter token that should be populated with * the given string. The provided string will be automatically transformed * from "CamelCase", "headlessCamelCase", "lowercase_with_underscores", * and "mixes_ofBoth_Styles" to consistent "UPPERCASE_WITH_UNDERSCORES". - * Each returned attribute will be prefixed with the string value provided + * Each returned token name will be prefixed with the string value provided * in the prefix. The value provided in prefix will be prepended to the - * attribute name, but will itself not be transformed. + * string, but will itself not be transformed. * * @param name * The string to be used to generate the token name. @@ -102,9 +102,11 @@ public abstract class TokenName { } /** - * Generate the name of a parameter from the given string, and with a blank - * prefix such that the token name will simply be the transformed version - * of the string. + * Generate the name of a parameter token from the given string, with no + * added prefix, such that the token name will simply be the transformed + * version of the string. See + * {@link #canonicalize(java.lang.String, java.lang.String)} + * * * @param name * The string to use to generate the token name. diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java index 7b9d3c2b1..a1d3e2fff 100644 --- a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java +++ b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java @@ -23,14 +23,13 @@ import static org.junit.Assert.assertEquals; import org.junit.Test; /** - * Test which verifies automatic generation of LDAP-specific connection - * parameter token names. + * Test which verifies automatic generation of connection parameter token names. */ public class TokenNameTest { /** * Verifies that TokenName.canonicalize() generates token names as - * specified, regardless of the naming convention of the attribute. + * specified, regardless of the format of the provided string. */ @Test public void testCanonicalize() { From 7f26ba7a52f1d5e60e465116d7bcecac989fcf55 Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Sat, 22 Jun 2019 19:27:52 -0400 Subject: [PATCH 6/7] GUACAMOLE-579: Insure that CAS is providing a username. --- .../guacamole/auth/cas/ticket/TicketValidationService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java index 628b28dcf..ba7ac837c 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.DatatypeConverter; import org.apache.guacamole.GuacamoleException; +import org.apache.guacamole.GuacamoleSecurityException; import org.apache.guacamole.GuacamoleServerException; import org.apache.guacamole.auth.cas.conf.ConfigurationService; import org.apache.guacamole.net.auth.Credentials; @@ -106,8 +107,10 @@ public class TicketValidationService { // Retrieve username and set the credentials. String username = principal.getName(); - if (username != null) - credentials.setUsername(username); + if (username == null) + throw new GuacamoleSecurityException("No username provided by CAS."); + + credentials.setUsername(username); // Retrieve password, attempt decryption, and set credentials. Object credObj = ticketAttrs.remove("credential"); From 9c26a7613cf8de6713abb56236cf6ba5d5fb70ff Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Sat, 22 Jun 2019 20:02:46 -0400 Subject: [PATCH 7/7] GUACAMOLE-579: Remove unnecessary JUnit dependencies. --- extensions/guacamole-auth-cas/pom.xml | 8 -------- extensions/guacamole-auth-ldap/pom.xml | 8 -------- 2 files changed, 16 deletions(-) diff --git a/extensions/guacamole-auth-cas/pom.xml b/extensions/guacamole-auth-cas/pom.xml index d8731203f..69ecab579 100644 --- a/extensions/guacamole-auth-cas/pom.xml +++ b/extensions/guacamole-auth-cas/pom.xml @@ -254,14 +254,6 @@ 2.5 provided - - - - junit - junit - 4.12 - test - diff --git a/extensions/guacamole-auth-ldap/pom.xml b/extensions/guacamole-auth-ldap/pom.xml index 79560f31b..898deaf9a 100644 --- a/extensions/guacamole-auth-ldap/pom.xml +++ b/extensions/guacamole-auth-ldap/pom.xml @@ -160,14 +160,6 @@ 3.0 - - - junit - junit - 4.12 - test - -