GUACAMOLE-579: Clean up comments, implement convenience method for tokens without a prefix, and remove unneeded classes.

This commit is contained in:
Virtually Nick
2019-06-20 20:41:41 -04:00
parent 8ab9e51009
commit d8db630dbd
10 changed files with 62 additions and 167 deletions

View File

@@ -22,9 +22,7 @@ package org.apache.guacamole.auth.cas;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.guacamole.form.Field; import org.apache.guacamole.form.Field;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;

View File

@@ -34,9 +34,9 @@ import java.util.Map.Entry;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException; import org.apache.guacamole.GuacamoleServerException;
import org.apache.guacamole.auth.cas.CASTokenName;
import org.apache.guacamole.auth.cas.conf.ConfigurationService; import org.apache.guacamole.auth.cas.conf.ConfigurationService;
import org.apache.guacamole.net.auth.Credentials; 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.authentication.AttributePrincipal;
import org.jasig.cas.client.validation.Assertion; import org.jasig.cas.client.validation.Assertion;
import org.jasig.cas.client.validation.Cas20ProxyTicketValidator; import org.jasig.cas.client.validation.Cas20ProxyTicketValidator;
@@ -54,6 +54,11 @@ public class TicketValidationService {
* Logger for this class. * Logger for this class.
*/ */
private static final Logger logger = LoggerFactory.getLogger(TicketValidationService.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. * Service for retrieving CAS configuration information.
@@ -96,7 +101,8 @@ public class TicketValidationService {
String confRedirectURI = confService.getRedirectURI(); String confRedirectURI = confService.getRedirectURI();
Assertion a = validator.validate(ticket, confRedirectURI); Assertion a = validator.validate(ticket, confRedirectURI);
AttributePrincipal principal = a.getPrincipal(); AttributePrincipal principal = a.getPrincipal();
Map<String, Object> ticketAttrs = principal.getAttributes(); Map<String, Object> ticketAttrs =
new HashMap<>(principal.getAttributes());
// Retrieve username and set the credentials. // Retrieve username and set the credentials.
String username = principal.getName(); String username = principal.getName();
@@ -112,8 +118,9 @@ public class TicketValidationService {
} }
// Convert remaining attributes that have values to Strings // Convert remaining attributes that have values to Strings
for (Entry attr : ticketAttrs.entrySet()) { for (Entry <String, Object> attr : ticketAttrs.entrySet()) {
String tokenName = CASTokenName.fromAttribute(attr.getKey().toString()); String tokenName = TokenName.fromAttribute(attr.getKey(),
CAS_ATTRIBUTE_TOKEN_PREFIX);
Object value = attr.getValue(); Object value = attr.getValue();
if (value != null) if (value != null)
tokens.put(tokenName, value.toString()); tokens.put(tokenName, value.toString());

View File

@@ -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"));
}
}

View File

@@ -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;
import org.apache.guacamole.net.auth.credentials.CredentialsInfo; import org.apache.guacamole.net.auth.credentials.CredentialsInfo;
import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException; import org.apache.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException;
import org.apache.guacamole.token.TokenName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -53,7 +54,12 @@ public class AuthenticationProviderService {
/** /**
* Logger for this class. * 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. * 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[] attrArray = attrList.toArray(new String[attrList.size()]);
String userDN = getUserBindDN(username); String userDN = getUserBindDN(username);
Map<String, String> tokens = new HashMap<String, String>(); Map<String, String> tokens = new HashMap<>();
try { try {
// Get LDAP attributes by querying LDAP // Get LDAP attributes by querying LDAP
@@ -309,7 +315,8 @@ 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(LDAPTokenName.fromAttribute(attr.getName()), attr.getStringValue()); tokens.put(TokenName.fromAttribute(attr.getName(),
LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue());
} }
} }

View File

@@ -28,7 +28,6 @@ import com.novell.ldap.LDAPJSSEStartTLSFactory;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException; import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.auth.ldap.ReferralAuthHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

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.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_");
}
}

View File

@@ -19,12 +19,9 @@
package org.apache.guacamole.auth.ldap; package org.apache.guacamole.auth.ldap;
import com.google.inject.Inject;
import com.novell.ldap.LDAPAuthHandler; import com.novell.ldap.LDAPAuthHandler;
import com.novell.ldap.LDAPAuthProvider; import com.novell.ldap.LDAPAuthProvider;
import com.novell.ldap.LDAPConnection;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.apache.guacamole.GuacamoleException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -48,6 +45,12 @@ public class ReferralAuthHandler implements LDAPAuthHandler {
* Creates a ReferralAuthHandler object to handle authentication when * Creates a ReferralAuthHandler object to handle authentication when
* following referrals in a LDAP connection, using the provided dn and * following referrals in a LDAP connection, using the provided dn and
* password. * 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) { public ReferralAuthHandler(String dn, String password) {
byte[] passwordBytes; byte[] passwordBytes;

View File

@@ -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"));
}
}

View File

@@ -65,7 +65,8 @@ public abstract class TokenName {
* automatically be transformed from "CamelCase", "headlessCamelCase", * automatically be transformed from "CamelCase", "headlessCamelCase",
* "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent * "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent
* "UPPERCASE_WITH_UNDERSCORES". Each returned attribute will be prefixed * "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 * @param name
* The name of the attribute to use to generate the token name. * The name of the attribute to use to generate the token name.
@@ -99,5 +100,21 @@ public abstract class TokenName {
return builder.toString(); 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, "");
}
} }

View File

@@ -34,20 +34,23 @@ public class TokenNameTest {
*/ */
@Test @Test
public void testFromAttribute() { public void testFromAttribute() {
assertEquals("A", TokenName.fromAttribute("a", "")); assertEquals("A", TokenName.fromAttribute("a"));
assertEquals("B", TokenName.fromAttribute("b", "")); assertEquals("B", TokenName.fromAttribute("b"));
assertEquals("1", TokenName.fromAttribute("1", "")); assertEquals("1", TokenName.fromAttribute("1"));
assertEquals("SOME_URL", TokenName.fromAttribute("someURL", "")); assertEquals("SOME_URL", TokenName.fromAttribute("someURL"));
assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes", "")); assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes"));
assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase", "")); assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase"));
assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase", "")); assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase"));
assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores", "")); assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores"));
assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_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("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("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("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord"));
assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_"));
assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_"));
} }
} }