Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUACAMOLE-1239: Configurable username letter case #956

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public interface Environment {

};

public static final StringGuacamoleProperty USERNAME_LETTER_CASE = new StringGuacamoleProperty() {

@Override
public String getName() { return "username-letter-case"; }

};

/**
* Returns the Guacamole home directory as determined when this Environment
* object was created. The Guacamole home directory is found by checking, in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
Expand All @@ -36,6 +37,7 @@
import javax.ws.rs.core.MultivaluedMap;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleResourceNotFoundException;
import org.apache.guacamole.environment.Environment;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.Credentials;
import org.apache.guacamole.net.auth.UserContext;
Expand All @@ -55,13 +57,22 @@ public class TokenRESTService {
* Logger for this class.
*/
private static final Logger logger = LoggerFactory.getLogger(TokenRESTService.class);
private static final String DEFAULT_LETTER_CASE = "mixed-case";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All constants should be documented.



/**
* Service for authenticating users and managing their Guacamole sessions.
*/
@Inject
private AuthenticationService authenticationService;


/**
* The Guacamole server environment.
*/
@Inject
private Environment environment;

/**
* Returns the credentials associated with the given request, using the
* provided username and password.
Expand Down Expand Up @@ -122,6 +133,35 @@ private Credentials getCredentials(HttpServletRequest request,

}

/**
* Alters letter casing of username, if configured.
* Configuration options:
* <ul>
* <li>mixed-case (default)</li>
* <li>lower-case</li>
* <li>upper-case</li>
* </ul>
* @param credentials
* The Credentials provided by the user in the most recent
* authentication attempt.
* @throws GuacamoleException If an error occurs while parsing the value
* for the given property in
* guacamole.properties, or if the property is
* not specified.
*/
private void adaptUsernameLetterCase(Credentials credentials) throws GuacamoleException {
switch (environment.getProperty(Environment.USERNAME_LETTER_CASE,DEFAULT_LETTER_CASE)){
case "lower-case":
Optional.ofNullable(credentials.getUsername()).map(String::toLowerCase).ifPresent(credentials::setUsername);
break;
case "upper-case":
Optional.ofNullable(credentials.getUsername()).filter(un -> !"guacadmin".equals(un)).map(String::toUpperCase).ifPresent(credentials::setUsername);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't expect any particular username - what is this check intending to accomplish?

break;
default:
/* don't change */
}
}

/**
* Authenticates a user, generates an auth token, associates that auth token
* with the user's UserContext for use by further requests. If an existing
Expand Down Expand Up @@ -169,7 +209,7 @@ public APIAuthenticationResult createToken(@FormParam("username") String usernam

// Build credentials from request
Credentials credentials = getCredentials(request, username, password);

adaptUsernameLetterCase(credentials);
// Create/update session producing possibly-new token
token = authenticationService.authenticate(credentials, token);

Expand Down