GUACAMOLE-38: Put userInfo directly into configuration.

This commit is contained in:
Nick Couchman
2018-05-17 19:58:12 -04:00
parent aaada49dfe
commit 84e71a029c
2 changed files with 32 additions and 36 deletions

View File

@@ -137,13 +137,7 @@ public class QCParser {
if (userInfo != null && !userInfo.isEmpty()) { if (userInfo != null && !userInfo.isEmpty()) {
try { try {
Map<String, String> userInfoParams = parseUserInfo(userInfo); parseUserInfo(userInfo, qcConfig);
if (userInfoParams.containsKey("username"))
qcConfig.setParameter("username", userInfoParams.get("username"));
if (userInfoParams.containsKey("password"))
qcConfig.setParameter("password", userInfoParams.get("password"));
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
throw new GuacamoleServerException("Unexpected lack of UTF-8 encoding support.", e); throw new GuacamoleServerException("Unexpected lack of UTF-8 encoding support.", e);
@@ -186,39 +180,38 @@ public class QCParser {
/** /**
* Parse the given string for username and password values, * Parse the given string for username and password values,
* and return a map containing the username, password * and, if values are present, decode them and set them in
* or both. * the provided GuacamoleConfiguration object.
* *
* @param userInfo * @param userInfo
* The string to parse for username/password values. * The string to parse for username/password values.
* *
* @return * @param config
* A map with the username, password, or both. * The GuacamoleConfiguration object to store the username
* and password in.
* *
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
* If Java lacks UTF-8 support. * If Java lacks UTF-8 support.
*/ */
public static Map<String, String> parseUserInfo(String userInfo) public static void parseUserInfo(String userInfo,
GuacamoleConfiguration config)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
Map<String, String> userInfoMap = new HashMap<String, String>();
Matcher userinfoMatcher = userinfoPattern.matcher(userInfo); Matcher userinfoMatcher = userinfoPattern.matcher(userInfo);
if (userinfoMatcher.matches()) { if (userinfoMatcher.matches()) {
String username = URLDecoder.decode( String username = userinfoMatcher.group(USERNAME_GROUP);
userinfoMatcher.group(USERNAME_GROUP), "UTF-8"); String password = userinfoMatcher.group(PASSWORD_GROUP);
String password = URLDecoder.decode(
userinfoMatcher.group(PASSWORD_GROUP), "UTF-8");
if (username != null && !username.isEmpty()) if (username != null && !username.isEmpty())
userInfoMap.put("username", username); config.setParameter("username",
URLDecoder.decode(username, "UTF-8"));
if (password != null && !password.isEmpty()) if (password != null && !password.isEmpty())
userInfoMap.put("password", password); config.setParameter("password",
URLDecoder.decode(password, "UTF-8"));
} }
return userInfoMap;
} }
/** /**

View File

@@ -25,7 +25,7 @@ import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.protocol.GuacamoleConfiguration; import org.apache.guacamole.protocol.GuacamoleConfiguration;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull;
/** /**
* Class to test methods in the QCParser utility class. * Class to test methods in the QCParser utility class.
@@ -62,22 +62,25 @@ public class QCParserTest {
Map<String, String> userInfoMap; Map<String, String> userInfoMap;
userInfoMap = QCParser.parseUserInfo("guacuser:secretpw"); GuacamoleConfiguration config1 = new GuacamoleConfiguration();
assertEquals("guacuser", userInfoMap.get("username")); QCParser.parseUserInfo("guacuser:secretpw", config1);
assertEquals("secretpw", userInfoMap.get("password")); assertEquals("guacuser", config1.getParameter("username"));
assertEquals("secretpw", config1.getParameter("password"));
GuacamoleConfiguration config2 = new GuacamoleConfiguration();
QCParser.parseUserInfo("guacuser", config2);
assertEquals("guacuser", config2.getParameter("username"));
assertNull(config2.getParameter("password"));
userInfoMap = QCParser.parseUserInfo("guacuser"); GuacamoleConfiguration config3 = new GuacamoleConfiguration();
assertEquals("guacuser", userInfoMap.get("username")); QCParser.parseUserInfo("guacuser:P%40ssw0rd%21", config3);
assertFalse(userInfoMap.containsKey("password")); assertEquals("guacuser", config3.getParameter("username"));
assertEquals("P@ssw0rd!", config3.getParameter("password"));
userInfoMap = QCParser.parseUserInfo("guacuser:P%40ssw0rd%21"); GuacamoleConfiguration config4 = new GuacamoleConfiguration();
assertEquals("guacuser", userInfoMap.get("username")); QCParser.parseUserInfo("domain%5cguacuser:domain%2fpassword", config4);
assertEquals("P@ssw0rd!", userInfoMap.get("password")); assertEquals("domain\\guacuser", config4.getParameter("username"));
assertEquals("domain/password", config4.getParameter("password"));
userInfoMap = QCParser.parseUserInfo("domain%5cguacuser:domain%2fpassword");
assertEquals("domain\\guacuser", userInfoMap.get("username"));
assertEquals("domain/password", userInfoMap.get("password"));
} }