[java-identity-provider] 16/19: IDP-1793 Use Suppliers for HttpRequest/Response

Rod Widdowson rdw at steadingsoftware.com
Wed Aug 10 13:17:08 UTC 2022


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch maint-4
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=a54e797660815261d52e9e4cc56ba98aaa2bb188

commit a54e797660815261d52e9e4cc56ba98aaa2bb188
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Aug 7 20:17:17 2022 +0100

    IDP-1793  Use Suppliers for HttpRequest/Response
    
    https://shibboleth.atlassian.net/browse/IDP-1793
    
    net.shibboleth.idp.authn.impl.StorageBackedAccountLockoutManager.UsernameIPLockoutKeyStrategy
    
    Gains a setter/getter for a Supplier for the HttpServletRequest
    and loses the setter for the raw object.
---
 .../impl/StorageBackedAccountLockoutManager.java   | 29 ++++++++++++++++------
 .../StorageBackedAccountLockoutManagerTest.java    |  4 ++-
 .../net/shibboleth/idp/conf/global-system.xml      |  2 +-
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/idp-authn-impl/src/main/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManager.java b/idp-authn-impl/src/main/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManager.java
index 1d93429dc..dfd967c00 100644
--- a/idp-authn-impl/src/main/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManager.java
+++ b/idp-authn-impl/src/main/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManager.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.function.Function;
+import java.util.function.Supplier;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -388,16 +389,28 @@ public class StorageBackedAccountLockoutManager extends AbstractIdentifiableInit
      */
     public static class UsernameIPLockoutKeyStrategy implements Function<ProfileRequestContext,String> { 
         
-        /** Servlet request to pull client ip from. **/
-        @Nullable private HttpServletRequest httpRequest;
+        /** Supplier for the Servlet request to pull client ip from. **/
+        @Nullable private Supplier<HttpServletRequest> httpRequestSupplier;
         
         /**
-         * Set the servlet request to read from.
+         * Set the Supplier for the servlet request to read from.
          * 
-         * @param request servlet request
+         * @param requestSupplier servlet request Supplier
          */
-        public void setHttpServletRequest(@Nonnull final HttpServletRequest request) {
-            httpRequest = Constraint.isNotNull(request, "HttpServletRequest cannot be null");
+        public void setHttpServletRequestSupplier(@Nonnull final Supplier<HttpServletRequest> requestSupplier) {
+            httpRequestSupplier = Constraint.isNotNull(requestSupplier, "HttpServletRequest cannot be null");
+        }
+
+        /**
+         * Get the current HTTP request if available.
+         *
+         * @return current HTTP request
+         */
+        @Nullable private HttpServletRequest getHttpServletRequest() {
+            if (httpRequestSupplier == null) {
+                return null;
+            }
+            return httpRequestSupplier.get();
         }
 
         /** {@inheritDoc} */
@@ -412,7 +425,7 @@ public class StorageBackedAccountLockoutManager extends AbstractIdentifiableInit
                 return lockoutManagerContext.getKey();
             }
 
-            if (httpRequest == null) {
+            if (getHttpServletRequest() == null) {
                 return null;
             }
 
@@ -429,7 +442,7 @@ public class StorageBackedAccountLockoutManager extends AbstractIdentifiableInit
             }
             
             final String username = upContext.getUsername();
-            final String ipAddr = HttpServletSupport.getRemoteAddr(httpRequest);
+            final String ipAddr = HttpServletSupport.getRemoteAddr(getHttpServletRequest());
             if (username == null || username.isEmpty() || ipAddr == null || ipAddr.isEmpty()) {
                 return null;
             }
diff --git a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManagerTest.java b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManagerTest.java
index d4505c73b..cd7875844 100644
--- a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManagerTest.java
+++ b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/StorageBackedAccountLockoutManagerTest.java
@@ -19,6 +19,7 @@ package net.shibboleth.idp.authn.impl;
 
 
 import java.time.Duration;
+import java.util.function.Supplier;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -47,7 +48,8 @@ public class StorageBackedAccountLockoutManagerTest extends BaseAuthenticationCo
         ss.initialize();
         
         final UsernameIPLockoutKeyStrategy keyStrategy = new UsernameIPLockoutKeyStrategy();
-        keyStrategy.setHttpServletRequest((HttpServletRequest) src.getExternalContext().getNativeRequest());
+        final HttpServletRequest request = (HttpServletRequest) src.getExternalContext().getNativeRequest();
+        keyStrategy.setHttpServletRequestSupplier(new Supplier<>() {public HttpServletRequest get() {return request;}});
         manager = new StorageBackedAccountLockoutManager();
         manager.setId("test");
         manager.setStorageService(ss);
diff --git a/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/global-system.xml b/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/global-system.xml
index fd0b0c35b..92148b692 100644
--- a/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/global-system.xml
+++ b/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/global-system.xml
@@ -273,7 +273,7 @@
             p:storageService-ref="shibboleth.StorageService">
         <property name="lockoutKeyStrategy">
             <bean class="net.shibboleth.idp.authn.impl.StorageBackedAccountLockoutManager.UsernameIPLockoutKeyStrategy"
-                p:httpServletRequest-ref="shibboleth.HttpServletRequest" />
+                p:httpServletRequestSupplier-ref="shibboleth.HttpServletRequestSupplier" />
         </property>
     </bean>
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list