This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push: new 18059a6d PROTON-2681 Add linked resource APIs to the SASL context types 18059a6d is described below commit 18059a6d1dd966606e556fc4e88dd3459b5015c8 Author: Timothy Bish <tabish...@gmail.com> AuthorDate: Tue Feb 14 17:42:32 2023 -0500 PROTON-2681 Add linked resource APIs to the SASL context types Allows a resource to be linked for repeated use during SASL authentication --- .../engine/impl/sasl/ProtonSaslContext.java | 18 +++++++++++++ .../qpid/protonj2/engine/sasl/SaslContext.java | 30 ++++++++++++++++++++++ .../engine/impl/sasl/ProtonSaslServerTest.java | 10 ++++++++ 3 files changed, 58 insertions(+) diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslContext.java b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslContext.java index 2a2bc882..a90e9d93 100644 --- a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslContext.java +++ b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslContext.java @@ -36,6 +36,7 @@ abstract class ProtonSaslContext implements SaslContext { protected final ProtonSaslHandler saslHandler; private ProtonAttachments attachments; + private Object linkedResource; // Client negotiations tracking. protected Symbol[] serverMechanisms; @@ -55,6 +56,23 @@ abstract class ProtonSaslContext implements SaslContext { return attachments == null ? attachments = new ProtonAttachments() : attachments; } + @Override + public ProtonSaslContext setLinkedResource(Object resource) { + this.linkedResource = resource; + return this; + } + + @SuppressWarnings("unchecked") + @Override + public <T> T getLinkedResource() { + return (T) linkedResource; + } + + @Override + public <T> T getLinkedResource(Class<T> typeClass) { + return typeClass.cast(linkedResource); + } + /** * Return the Role of the context implementation. * diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslContext.java b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslContext.java index 1e301a5c..347d9f2e 100644 --- a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslContext.java +++ b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslContext.java @@ -30,6 +30,36 @@ public interface SaslContext { */ enum Role { CLIENT, SERVER } + /** + * Links a given resource to this {@link SaslContext}. + * + * @param resource + * The resource to link to this {@link SaslContext}. + * + * @return this {@link SaslContext} instance. + */ + SaslContext setLinkedResource(Object resource); + + /** + * @param <T> The type that the linked resource should be cast to on return. + * + * @return the user set linked resource for this {@link SaslContext} instance. + */ + <T> T getLinkedResource(); + + /** + * Gets the linked resource (if set) and returns it using the type information + * provided to cast the returned value. + * + * @param <T> The type to cast the linked resource to if one is set. + * @param typeClass the type's Class which is used for casting the returned value. + * + * @return the user set linked resource for this Context instance. + * + * @throws ClassCastException if the linked resource cannot be cast to the type requested. + */ + <T> T getLinkedResource(Class<T> typeClass); + /** * Returns a mutable context that the application layer can use to store meaningful data for itself * in relation to this specific SASL context object. diff --git a/protonj2/src/test/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslServerTest.java b/protonj2/src/test/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslServerTest.java index 1af0bde4..373c34a5 100644 --- a/protonj2/src/test/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslServerTest.java +++ b/protonj2/src/test/java/org/apache/qpid/protonj2/engine/impl/sasl/ProtonSaslServerTest.java @@ -210,6 +210,10 @@ public class ProtonSaslServerTest extends ProtonEngineTestSupport { @Override public void handleSaslInit(SaslServerContext context, Symbol mechanism, ProtonBuffer initResponse) { + if (!"ANONYMOUS".equals(context.getLinkedResource(String.class))) { + throw new AssertionError("SASL context did not preserve the linked resource"); + } + if (mechanism.equals(Symbol.valueOf("ANONYMOUS"))) { context.sendOutcome(SaslOutcome.SASL_OK, null); } else { @@ -219,6 +223,7 @@ public class ProtonSaslServerTest extends ProtonEngineTestSupport { @Override public void handleSaslHeader(SaslServerContext context, AMQPHeader header) { + context.setLinkedResource("ANONYMOUS"); context.sendMechanisms(new Symbol[] { Symbol.valueOf("ANONYMOUS") }); } }; @@ -234,6 +239,10 @@ public class ProtonSaslServerTest extends ProtonEngineTestSupport { @Override public void handleSaslInit(SaslServerContext context, Symbol mechanism, ProtonBuffer initResponse) { + if (!"PLAIN".equals(context.getLinkedResource(String.class))) { + throw new AssertionError("SASL context did not preserve the linked resource"); + } + if (mechanism.equals(Symbol.valueOf("PLAIN"))) { context.sendOutcome(SaslOutcome.SASL_OK, null); } else { @@ -243,6 +252,7 @@ public class ProtonSaslServerTest extends ProtonEngineTestSupport { @Override public void handleSaslHeader(SaslServerContext context, AMQPHeader header) { + context.setLinkedResource("PLAIN"); context.sendMechanisms(new Symbol[] { Symbol.valueOf("PLAIN") }); } }; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org