GUACAMOLE-524: Merge allow user attributes to be used as tokens.

This commit is contained in:
Nick Couchman
2018-06-21 11:14:06 -04:00
10 changed files with 181 additions and 14 deletions

View File

@@ -20,6 +20,7 @@
package org.apache.guacamole.net.auth;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
@@ -41,4 +42,14 @@ public abstract class AbstractAuthenticatedUser extends AbstractIdentifiable
// Nothing to invalidate
}
@Override
public Map<String, String> getAttributes() {
return Collections.<String, String>emptyMap();
}
@Override
public void setAttributes(Map<String, String> attributes) {
//do nothing
}
}

View File

@@ -25,7 +25,7 @@ import java.util.Set;
* A user of the Guacamole web application who has been authenticated by an
* AuthenticationProvider.
*/
public interface AuthenticatedUser extends Identifiable {
public interface AuthenticatedUser extends Identifiable, Attributes {
/**
* The identifier reserved for representing a user that has authenticated

View File

@@ -21,6 +21,8 @@ package org.apache.guacamole.token;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.Credentials;
@@ -74,6 +76,11 @@ public class StandardTokens {
*/
private static final String TIME_FORMAT = "HHmmss";
/**
* The prefix of the arbitrary attribute tokens.
*/
public static final String ATTR_TOKEN_PREFIX = "GUAC_ATTR_";
/**
* This utility class should not be instantiated.
*/
@@ -143,10 +150,11 @@ public class StandardTokens {
* Adds tokens which are standardized by guacamole-ext to the given
* TokenFilter using the values from the given AuthenticatedUser object,
* including any associated credentials. These standardized tokens include
* the current username (GUAC_USERNAME), password (GUAC_PASSWORD), and the
* server date and time (GUAC_DATE and GUAC_TIME respectively). If either
* the username or password are not set within the given user or their
* provided credentials, the corresponding token(s) will remain unset.
* the current username (GUAC_USERNAME), password (GUAC_PASSWORD), the
* server date and time (GUAC_DATE and GUAC_TIME respectively), and custom
* user attributes. If either the username or password are not set within
* the given user or their provided credentials, the corresponding token(s)
* will remain unset.
*
* @param filter
* The TokenFilter to add standard tokens to.
@@ -164,6 +172,33 @@ public class StandardTokens {
// Add tokens specific to credentials
addStandardTokens(filter, user.getCredentials());
// Add custom attribute tokens
addAttributeTokens(filter, user.getAttributes());
}
/**
* Add attribute tokens to StandardTokens. These are arbitrary
* key/value pairs that may be configured by the various authentication
* extensions.
*
* @param filter
* The TokenFilter to add attribute tokens to.
*
* @param attributes
* The map of key/value pairs to add tokens for.
*/
public static void addAttributeTokens(TokenFilter filter,
Map<String, String> attributes) {
if (attributes != null) {
for (Map.Entry entry : attributes.entrySet()) {
String key = entry.getKey().toString();
String tokenName = ATTR_TOKEN_PREFIX + key.toUpperCase();
String tokenValue = entry.getValue().toString();
filter.setToken(tokenName, tokenValue);
}
}
}
}