From 2f946d962be9aa6d72df2b24cdb5327833b7b187 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 21 Jan 2022 15:23:40 -0800 Subject: [PATCH] GUACAMOLE-641: Allow tokens to be easily injected on-demand. --- .../net/auth/TokenInjectingConnection.java | 32 +++++++++++++++++- .../auth/TokenInjectingConnectionGroup.java | 33 ++++++++++++++++++- .../net/auth/TokenInjectingUserContext.java | 18 ++++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnection.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnection.java index 8a826d88c..51ca6e33e 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnection.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnection.java @@ -19,6 +19,7 @@ package org.apache.guacamole.net.auth; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.apache.guacamole.GuacamoleException; @@ -37,6 +38,22 @@ public class TokenInjectingConnection extends DelegatingConnection { */ private final Map 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 getTokens() throws GuacamoleException { + return tokens; + } + /** * Wraps the given Connection, automatically adding the given tokens to * each invocation of connect(). Any additional tokens which have the same @@ -54,13 +71,26 @@ public class TokenInjectingConnection extends DelegatingConnection { 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.emptyMap()); + } + @Override public GuacamoleTunnel connect(GuacamoleClientInformation info, Map tokens) throws GuacamoleException { // Apply provided tokens over those given to connect() tokens = new HashMap<>(tokens); - tokens.putAll(this.tokens); + tokens.putAll(getTokens()); return super.connect(info, tokens); diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnectionGroup.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnectionGroup.java index 0ec93baf4..62f71630a 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnectionGroup.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingConnectionGroup.java @@ -19,6 +19,7 @@ package org.apache.guacamole.net.auth; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.apache.guacamole.GuacamoleException; @@ -37,6 +38,23 @@ public class TokenInjectingConnectionGroup extends DelegatingConnectionGroup { */ private final Map 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 getTokens() throws GuacamoleException { + return tokens; + } + /** * Wraps the given ConnectionGroup, automatically adding the given tokens * to each invocation of connect(). Any additional tokens which have the @@ -54,13 +72,26 @@ public class TokenInjectingConnectionGroup extends DelegatingConnectionGroup { 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.emptyMap()); + } + @Override public GuacamoleTunnel connect(GuacamoleClientInformation info, Map tokens) throws GuacamoleException { // Apply provided tokens over those given to connect() tokens = new HashMap<>(tokens); - tokens.putAll(this.tokens); + tokens.putAll(getTokens()); return super.connect(info, tokens); diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingUserContext.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingUserContext.java index 1f6d3d60e..e4940e5d8 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingUserContext.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/TokenInjectingUserContext.java @@ -122,7 +122,14 @@ public class TokenInjectingUserContext extends DelegatingUserContext { @Override protected ConnectionGroup decorate(ConnectionGroup object) throws GuacamoleException { - return new TokenInjectingConnectionGroup(object, getTokens(object)); + return new TokenInjectingConnectionGroup(object) { + + @Override + protected Map getTokens() throws GuacamoleException { + return TokenInjectingUserContext.this.getTokens(object); + } + + }; } @Override @@ -140,7 +147,14 @@ public class TokenInjectingUserContext extends DelegatingUserContext { @Override protected Connection decorate(Connection object) throws GuacamoleException { - return new TokenInjectingConnection(object, getTokens(object)); + return new TokenInjectingConnection(object) { + + @Override + protected Map getTokens() throws GuacamoleException { + return TokenInjectingUserContext.this.getTokens(object); + } + + }; } @Override