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

remm pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 4187b91dc4 Add missing algorithm callback
4187b91dc4 is described below

commit 4187b91dc47c5000e97caf266ed3e0c3585136bc
Author: remm <r...@apache.org>
AuthorDate: Tue Jun 25 11:24:00 2024 +0200

    Add missing algorithm callback
---
 .../apache/catalina/realm/JAASCallbackHandler.java |  8 ++++
 test/org/apache/catalina/realm/TestJAASRealm.java  | 43 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  4 ++
 3 files changed, 55 insertions(+)

diff --git a/java/org/apache/catalina/realm/JAASCallbackHandler.java 
b/java/org/apache/catalina/realm/JAASCallbackHandler.java
index 5d540b01d0..77de1246eb 100644
--- a/java/org/apache/catalina/realm/JAASCallbackHandler.java
+++ b/java/org/apache/catalina/realm/JAASCallbackHandler.java
@@ -97,6 +97,7 @@ public class JAASCallbackHandler implements CallbackHandler {
         this.realmName = realmName;
         this.digestA2 = digestA2;
         this.authMethod = authMethod;
+        this.algorithm = algorithm;
     }
 
     // ----------------------------------------------------- Instance Variables
@@ -157,6 +158,11 @@ public class JAASCallbackHandler implements 
CallbackHandler {
      */
     protected final String authMethod;
 
+    /**
+     * Algorithm.
+     */
+    protected final String algorithm;
+
     // --------------------------------------------------------- Public Methods
 
 
@@ -204,6 +210,8 @@ public class JAASCallbackHandler implements CallbackHandler 
{
                     cb.setText(digestA2);
                 } else if (cb.getPrompt().equals("authMethod")) {
                     cb.setText(authMethod);
+                } else if (cb.getPrompt().equals("algorithm")) {
+                    cb.setText(algorithm);
                 } else if (cb.getPrompt().equals("catalinaBase")) {
                     
cb.setText(realm.getContainer().getCatalinaBase().getAbsolutePath());
                 } else {
diff --git a/test/org/apache/catalina/realm/TestJAASRealm.java 
b/test/org/apache/catalina/realm/TestJAASRealm.java
index 8cee07d7dc..f77239ae49 100644
--- a/test/org/apache/catalina/realm/TestJAASRealm.java
+++ b/test/org/apache/catalina/realm/TestJAASRealm.java
@@ -35,6 +35,12 @@ public class TestJAASRealm extends TomcatBaseTest {
             "    sufficient;\n" +
             "};";
 
+    private static final String CONFIG_MEMORY =
+            "MemoryLogin {\n" +
+            "    org.apache.catalina.realm.JAASMemoryLoginModule\n" +
+            "    sufficient pathname=\"tomcat-users-lm.xml\";\n" +
+            "};";
+
     @Test
     public void testRealm() throws Exception {
 
@@ -67,4 +73,41 @@ public class TestJAASRealm extends TomcatBaseTest {
         Assert.assertTrue(gp.hasRole("role1"));
     }
 
+    @Test
+    public void testMemoryLoginModule() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File tomcatUsersXml = new File(getTemporaryDirectory(), 
"tomcat-users-lm.xml");
+        try (PrintWriter writer = new PrintWriter(tomcatUsersXml)) {
+            writer.write(TestMemoryRealm.CONFIG);
+        }
+        addDeleteOnTearDown(tomcatUsersXml);
+
+        // Write login config to the temp path
+        File loginConfFile = new File(getTemporaryDirectory(), 
"memoryLoginConfig.conf");
+        try (PrintWriter writer = new PrintWriter(loginConfFile)) {
+            writer.write(CONFIG_MEMORY.replace("tomcat-users-lm.xml", 
tomcatUsersXml.getAbsolutePath()));
+        }
+        addDeleteOnTearDown(loginConfFile);
+
+        JAASRealm jaasRealm = new JAASRealm();
+        jaasRealm.setAppName("MemoryLogin");
+        jaasRealm.setCredentialHandler(new MessageDigestCredentialHandler());
+        jaasRealm.setUserClassNames(GenericPrincipal.class.getName());
+        jaasRealm.setRoleClassNames(GenericPrincipal.class.getName());
+        jaasRealm.setConfigFile(loginConfFile.getAbsolutePath());
+        Context context = tomcat.addContext("/jaastest", null);
+        context.setRealm(jaasRealm);
+
+        tomcat.start();
+
+        Principal p = jaasRealm.authenticate("foo", "bar");
+        Assert.assertNull(p);
+        p = jaasRealm.authenticate("admin", "sekr3t");
+        Assert.assertNotNull(p);
+        Assert.assertTrue(p instanceof GenericPrincipal);
+        GenericPrincipal gp = (GenericPrincipal) p;
+        Assert.assertTrue(gp.hasRole("testrole"));
+    }
+
 }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index a66dce58cc..b3a1d09dec 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -111,6 +111,10 @@
         Allow <code>JAASRealm</code> to use the configuration source to load a
         configured <code>configFile</code>, for easier use with testing. (remm)
       </fix>
+      <fix>
+        Add missing algorithm callback to the <code>JAASCallbackHandler</code>.
+        (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to