GUACAMOLE-641: Allow tokens to be easily injected on-demand.

This commit is contained in:
Michael Jumper
2022-01-21 15:23:40 -08:00
parent e56becc258
commit 2f946d962b
3 changed files with 79 additions and 4 deletions

View File

@@ -19,6 +19,7 @@
package org.apache.guacamole.net.auth; package org.apache.guacamole.net.auth;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
@@ -37,6 +38,22 @@ public class TokenInjectingConnection extends DelegatingConnection {
*/ */
private final Map<String, String> tokens; private final Map<String, String> tokens;
/**
* Returns the tokens which should be added to an in-progress call to
* connect(). If not overridden, this function will return the tokens
* provided when this instance of TokenInjectingConnection was created.
*
* @return
* The tokens which should be added to the in-progress call to
* connect().
*
* @throws GuacamoleException
* If the applicable tokens cannot be generated.
*/
protected Map<String, String> getTokens() throws GuacamoleException {
return tokens;
}
/** /**
* Wraps the given Connection, automatically adding the given tokens to * Wraps the given Connection, automatically adding the given tokens to
* each invocation of connect(). Any additional tokens which have the same * each invocation of connect(). Any additional tokens which have the same
@@ -54,13 +71,26 @@ public class TokenInjectingConnection extends DelegatingConnection {
this.tokens = tokens; this.tokens = tokens;
} }
/**
* Wraps the given Connection such that the additional parameter tokens
* returned by getTokens() are included with each invocation of connect().
* Any additional tokens which have the same name as existing tokens will
* override the existing values.
*
* @param connection
* The Connection to wrap.
*/
public TokenInjectingConnection(Connection connection) {
this(connection, Collections.<String, String>emptyMap());
}
@Override @Override
public GuacamoleTunnel connect(GuacamoleClientInformation info, public GuacamoleTunnel connect(GuacamoleClientInformation info,
Map<String, String> tokens) throws GuacamoleException { Map<String, String> tokens) throws GuacamoleException {
// Apply provided tokens over those given to connect() // Apply provided tokens over those given to connect()
tokens = new HashMap<>(tokens); tokens = new HashMap<>(tokens);
tokens.putAll(this.tokens); tokens.putAll(getTokens());
return super.connect(info, tokens); return super.connect(info, tokens);

View File

@@ -19,6 +19,7 @@
package org.apache.guacamole.net.auth; package org.apache.guacamole.net.auth;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
@@ -37,6 +38,23 @@ public class TokenInjectingConnectionGroup extends DelegatingConnectionGroup {
*/ */
private final Map<String, String> tokens; private final Map<String, String> tokens;
/**
* Returns the tokens which should be added to an in-progress call to
* connect(). If not overridden, this function will return the tokens
* provided when this instance of TokenInjectingConnectionGroup was
* created.
*
* @return
* The tokens which should be added to the in-progress call to
* connect().
*
* @throws GuacamoleException
* If the applicable tokens cannot be generated.
*/
protected Map<String, String> getTokens() throws GuacamoleException {
return tokens;
}
/** /**
* Wraps the given ConnectionGroup, automatically adding the given tokens * Wraps the given ConnectionGroup, automatically adding the given tokens
* to each invocation of connect(). Any additional tokens which have the * to each invocation of connect(). Any additional tokens which have the
@@ -54,13 +72,26 @@ public class TokenInjectingConnectionGroup extends DelegatingConnectionGroup {
this.tokens = tokens; this.tokens = tokens;
} }
/**
* Wraps the given ConnectionGroup such that the additional parameter
* tokens returned by getTokens() are included with each invocation of
* connect(). Any additional tokens which have the same name as existing
* tokens will override the existing values.
*
* @param connectionGroup
* The ConnectionGroup to wrap.
*/
public TokenInjectingConnectionGroup(ConnectionGroup connectionGroup) {
this(connectionGroup, Collections.<String, String>emptyMap());
}
@Override @Override
public GuacamoleTunnel connect(GuacamoleClientInformation info, public GuacamoleTunnel connect(GuacamoleClientInformation info,
Map<String, String> tokens) throws GuacamoleException { Map<String, String> tokens) throws GuacamoleException {
// Apply provided tokens over those given to connect() // Apply provided tokens over those given to connect()
tokens = new HashMap<>(tokens); tokens = new HashMap<>(tokens);
tokens.putAll(this.tokens); tokens.putAll(getTokens());
return super.connect(info, tokens); return super.connect(info, tokens);

View File

@@ -122,7 +122,14 @@ public class TokenInjectingUserContext extends DelegatingUserContext {
@Override @Override
protected ConnectionGroup decorate(ConnectionGroup object) throws GuacamoleException { protected ConnectionGroup decorate(ConnectionGroup object) throws GuacamoleException {
return new TokenInjectingConnectionGroup(object, getTokens(object)); return new TokenInjectingConnectionGroup(object) {
@Override
protected Map<String, String> getTokens() throws GuacamoleException {
return TokenInjectingUserContext.this.getTokens(object);
}
};
} }
@Override @Override
@@ -140,7 +147,14 @@ public class TokenInjectingUserContext extends DelegatingUserContext {
@Override @Override
protected Connection decorate(Connection object) throws GuacamoleException { protected Connection decorate(Connection object) throws GuacamoleException {
return new TokenInjectingConnection(object, getTokens(object)); return new TokenInjectingConnection(object) {
@Override
protected Map<String, String> getTokens() throws GuacamoleException {
return TokenInjectingUserContext.this.getTokens(object);
}
};
} }
@Override @Override