This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-rampart.git


The following commit(s) were added to refs/heads/master by this push:
     new 6bd40bbd RAMPART-397: load custom STS issuers with the service class 
loader
6bd40bbd is described below

commit 6bd40bbd8cf7da838c57dae87f201ea99d87c41d
Author: Robert Lazarski <[email protected]>
AuthorDate: Tue Nov 17 12:56:59 2026 -1000

    RAMPART-397: load custom STS issuers with the service class loader
    
    TokenRequestDispatcherConfig loaded issuers, validators, renewers and
    cancelers with the rahas jar's own class loader, which cannot see a
    class packaged in the STS service archive. STSMessageReceiver now hands
    the service class loader to the config. The STS page no longer tells
    readers to remove rampart.mar; nothing needs removing.
    
    Co-Authored-By: Claude Fable 5.1 <[email protected]>
---
 .../rahas/TokenRequestDispatcherConfigTest.java    | 54 ++++++++++++++++++++++
 .../java/org/apache/rahas/STSMessageReceiver.java  | 16 +++++--
 .../apache/rahas/TokenRequestDispatcherConfig.java | 34 ++++++++++----
 src/site/xdoc/setting-up-sts.xml                   |  2 +-
 4 files changed, 92 insertions(+), 14 deletions(-)

diff --git 
a/modules/rampart-tests/src/test/java/org/apache/rahas/TokenRequestDispatcherConfigTest.java
 
b/modules/rampart-tests/src/test/java/org/apache/rahas/TokenRequestDispatcherConfigTest.java
index 83021f25..747038da 100644
--- 
a/modules/rampart-tests/src/test/java/org/apache/rahas/TokenRequestDispatcherConfigTest.java
+++ 
b/modules/rampart-tests/src/test/java/org/apache/rahas/TokenRequestDispatcherConfigTest.java
@@ -16,10 +16,44 @@
 
 package org.apache.rahas;
 
+import java.io.InputStream;
+
 import junit.framework.TestCase;
 
 public class TokenRequestDispatcherConfigTest extends TestCase {
 
+    /**
+     * Stands in for an Axis2 service class loader: it defines the issuer 
class itself,
+     * as a service archive carries a class the rahas module loader cannot see.
+     */
+    private static final class ServiceClassLoader extends ClassLoader {
+
+        ServiceClassLoader(ClassLoader parent) {
+            super(parent);
+        }
+
+        @Override
+        protected Class<?> loadClass(String name, boolean resolve) throws 
ClassNotFoundException {
+            if (!TempIssuer.class.getName().equals(name)) {
+                return super.loadClass(name, resolve);
+            }
+            Class<?> loaded = findLoadedClass(name);
+            if (loaded == null) {
+                try (InputStream in = getParent().getResourceAsStream(
+                        name.replace('.', '/') + ".class")) {
+                    if (in == null) {
+                        throw new ClassNotFoundException(name);
+                    }
+                    byte[] bytes = in.readAllBytes();
+                    loaded = defineClass(name, bytes, 0, bytes.length);
+                } catch (java.io.IOException e) {
+                    throw new ClassNotFoundException(name, e);
+                }
+            }
+            return loaded;
+        }
+    }
+
     public TokenRequestDispatcherConfigTest() {
         super();
     }
@@ -47,6 +81,26 @@ public class TokenRequestDispatcherConfigTest extends 
TestCase {
                 .getName(), issuer.getClass().getName());
     }
 
+    /**
+     * RAMPART-397: the issuer is loaded with the configured service class 
loader, so a
+     * class that only the service archive carries is found.
+     */
+    public void testIssuerLoadedWithServiceClassLoader() throws Exception {
+        TokenRequestDispatcherConfig config = TokenRequestDispatcherConfig
+                .load("test-resources/trust/dispatcher.config.xml");
+        ServiceClassLoader serviceLoader = new 
ServiceClassLoader(getClass().getClassLoader());
+        config.setClassLoader(serviceLoader);
+
+        TokenIssuer issuer = 
config.getIssuer("http://example.org/mySpecialToken1";);
+
+        assertSame("issuer must come from the service class loader",
+                serviceLoader, issuer.getClass().getClassLoader());
+        assertNotSame("the service loader's own definition must be used",
+                TempIssuer.class, issuer.getClass());
+        assertSame("default issuer must use the same loader",
+                serviceLoader, 
config.getDefaultIssuerInstace().getClass().getClassLoader());
+    }
+
     /**
      * Testing expected faliure when the default issuer is not specified
      */
diff --git 
a/modules/rampart-trust/src/main/java/org/apache/rahas/STSMessageReceiver.java 
b/modules/rampart-trust/src/main/java/org/apache/rahas/STSMessageReceiver.java
index 553af5ff..e807c5a5 100644
--- 
a/modules/rampart-trust/src/main/java/org/apache/rahas/STSMessageReceiver.java
+++ 
b/modules/rampart-trust/src/main/java/org/apache/rahas/STSMessageReceiver.java
@@ -39,20 +39,26 @@ public class STSMessageReceiver extends 
AbstractInOutMessageReceiver {
                     
.getParameter(TokenRequestDispatcherConfig.CONFIG_PARAM_KEY);
             Parameter paramFile = inMessage
                     
.getParameter(TokenRequestDispatcherConfig.CONFIG_FILE_KEY);
-            TokenRequestDispatcher dispatcher;
+            TokenRequestDispatcherConfig config;
             if (param != null) {
-                dispatcher = new TokenRequestDispatcher(param
+                config = TokenRequestDispatcherConfig.load(param
                         .getParameterElement().getFirstChildWithName(
                                 new QName("token-dispatcher-configuration")));
             } else if (paramFile != null) {
-                dispatcher = new TokenRequestDispatcher((String) paramFile
+                config = TokenRequestDispatcherConfig.load((String) paramFile
                         .getValue());
             } else {
-                dispatcher = new TokenRequestDispatcher(
+                config = TokenRequestDispatcherConfig.load(
                         (OMElement) inMessage
                                 
.getProperty(TokenRequestDispatcherConfig.CONFIG_PARAM_KEY));
             }
-            
+            // RAMPART-397: a custom issuer may live in the service archive, 
which only
+            // the service class loader can see.
+            if (inMessage.getAxisService() != null) {
+                
config.setClassLoader(inMessage.getAxisService().getClassLoader());
+            }
+            TokenRequestDispatcher dispatcher = new 
TokenRequestDispatcher(config);
+
             SOAPEnvelope responseEnv = dispatcher.handle(inMessage, 
outMessage);
                        outMessage.setEnvelope(responseEnv);
         } catch (TrustException e) {
diff --git 
a/modules/rampart-trust/src/main/java/org/apache/rahas/TokenRequestDispatcherConfig.java
 
b/modules/rampart-trust/src/main/java/org/apache/rahas/TokenRequestDispatcherConfig.java
index 51e54677..de9acd92 100644
--- 
a/modules/rampart-trust/src/main/java/org/apache/rahas/TokenRequestDispatcherConfig.java
+++ 
b/modules/rampart-trust/src/main/java/org/apache/rahas/TokenRequestDispatcherConfig.java
@@ -54,6 +54,28 @@ public class TokenRequestDispatcherConfig {
     private String defaultValidatorClassName;
     private String defaultRenewerClassName;
 
+    private ClassLoader classLoader;
+
+    /**
+     * Sets the class loader used to load the issuer, validator, renewer and 
canceler
+     * classes. RAMPART-397: a custom implementation packaged inside the STS 
service
+     * archive is only visible to that service's class loader, not to the 
loader of
+     * this class, so {@link STSMessageReceiver} passes the service class 
loader here.
+     * When none is set, the loader of this class is used.
+     */
+    public void setClassLoader(ClassLoader classLoader) {
+        this.classLoader = classLoader;
+    }
+
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
+
+    private Object instantiate(String className) throws Exception {
+        ClassLoader loader = classLoader != null ? classLoader : 
this.getClass().getClassLoader();
+        return Loader.loadClass(loader, 
className).getDeclaredConstructor().newInstance();
+    }
+
     public static TokenRequestDispatcherConfig load(OMElement configElem) 
throws TrustException {
 
         if (!DISPATCHER_CONFIG.equals(configElem.getQName())) {
@@ -389,8 +411,7 @@ public class TokenRequestDispatcherConfig {
      * @return TokenIssuer
      */
     private TokenIssuer createIssuer(String issuerClassName) throws Exception {
-        TokenIssuer issuer = (TokenIssuer) 
Loader.loadClass(this.getClass().getClassLoader(), 
-                                                                    
issuerClassName).newInstance();
+        TokenIssuer issuer = (TokenIssuer) instantiate(issuerClassName);
         issuer.setConfigurationElement((OMElement) 
this.configElements.get(issuerClassName));
         issuer.setConfigurationFile((String) 
this.configFiles.get(issuerClassName));
         issuer.setConfigurationParamName((String) 
this.configParamNames.get(issuerClassName));
@@ -398,8 +419,7 @@ public class TokenRequestDispatcherConfig {
     }
 
     private TokenCanceler createCanceler(String cancelerClassName) throws 
Exception {
-        TokenCanceler canceler = (TokenCanceler) 
Loader.loadClass(this.getClass().getClassLoader(),
-                                                                   
cancelerClassName).newInstance();
+        TokenCanceler canceler = (TokenCanceler) 
instantiate(cancelerClassName);
         canceler.setConfigurationElement((OMElement) 
this.configElements.get(cancelerClassName));
         canceler.setConfigurationFile((String) 
this.configFiles.get(cancelerClassName));
         canceler.setConfigurationParamName((String) 
this.configParamNames.get(cancelerClassName));
@@ -407,8 +427,7 @@ public class TokenRequestDispatcherConfig {
     }
     
     private TokenValidator createValidator(String validatorClassName) throws 
Exception {
-        TokenValidator validator = (TokenValidator) 
Loader.loadClass(this.getClass().getClassLoader(),
-                                                                
validatorClassName).newInstance();
+        TokenValidator validator = (TokenValidator) 
instantiate(validatorClassName);
         validator.setConfigurationElement((OMElement) 
this.configElements.get(validatorClassName));
         validator.setConfigurationFile((String) 
this.configFiles.get(validatorClassName));
         validator.setConfigurationParamName((String) 
this.configParamNames.get(validatorClassName));
@@ -416,8 +435,7 @@ public class TokenRequestDispatcherConfig {
     }
     
     private TokenRenewer createRenewer (String renewerClassName) throws 
Exception {
-        TokenRenewer renewer = (TokenRenewer) 
Loader.loadClass(this.getClass().getClassLoader(),
-                                                                    
renewerClassName).newInstance();
+        TokenRenewer renewer = (TokenRenewer) instantiate(renewerClassName);
         renewer.setConfigurationElement((OMElement) 
this.configElements.get(renewerClassName));
         renewer.setConfigurationFile((String) 
this.configFiles.get(renewerClassName));
         renewer.setConfigurationParamName((String) 
this.configParamNames.get(renewerClassName));
diff --git a/src/site/xdoc/setting-up-sts.xml b/src/site/xdoc/setting-up-sts.xml
index da4fd615..b3293dd9 100644
--- a/src/site/xdoc/setting-up-sts.xml
+++ b/src/site/xdoc/setting-up-sts.xml
@@ -27,7 +27,7 @@
             <h3>Setting up a Security Token Service</h3>
         <p>Security Token Service can be set up as per WS-Trust specification 
using Rampart. The default security token service shipped with the rampart 
distribution is contained in the rampart-trust.mar module. It can issue SCT 
tokens and SAML tokens. Sample05 contains a client that connects to the default 
STS and obtain a SAML token. The services.xml in the sample contains 
"saml-issuer-config" parameter that is used to configure the default SAML 
issuer.</p>
         <strong>STS with a custom issuer</strong>
-        <p>First the default rampart.mar has to be removed from the modules. 
Then write you own issuer implementing the 
<code>"org.apache.rahas.TokenIssuer"</code> interface. Let's say that your 
issuer is <code>"org.custom.MyIssuer"</code>. Then create a Axis2 service 
archive with the following in the services.xml. Drop the archive into the 
repository and you have a STS with a CustomToken issuer.</p>
+        <p>Nothing has to be removed from the modules directory. The default 
STS is the RequestSecurityToken operation that the rahas module adds to every 
service it is engaged to, so a service with its own issuer simply does not 
engage rahas. Write your own issuer implementing the 
<code>"org.apache.rahas.TokenIssuer"</code> interface. Let's say that your 
issuer is <code>"org.custom.MyIssuer"</code>. Then create an Axis2 service 
archive with the following in the services.xml. The issuer  [...]
 
         <pre>
 

Reply via email to