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

lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git


The following commit(s) were added to refs/heads/master by this push:
     new f3cbdce  [SSHD-1076] Break down ClientUserAuthService#auth method into 
several to allow for flexible override
f3cbdce is described below

commit f3cbdce88ef863dec20a20b5b43e95991368d840
Author: Lyor Goldstein <lgoldst...@apache.org>
AuthorDate: Sun Sep 13 19:58:40 2020 +0300

    [SSHD-1076] Break down ClientUserAuthService#auth method into several to 
allow for flexible override
---
 CHANGES.md                                         |  1 +
 .../sshd/client/session/ClientUserAuthService.java | 65 ++++++++++++++--------
 2 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 1893207..a86f891 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -26,6 +26,7 @@ or `-key-file` command line option.
 * [SSHD-1040](https://issues.apache.org/jira/browse/SSHD-1040) Make server key 
available after KEX completed.
 * [SSHD-1060](https://issues.apache.org/jira/browse/SSHD-1060) Do not store 
logger level in fields.
 * [SSHD-1064](https://issues.apache.org/jira/browse/SSHD-1064) Fixed 
`ClientSession#executeRemoteCommand` handling of STDERR in case of exception to 
behave according to its documentation
+* [SSHD-1076](https://issues.apache.org/jira/browse/SSHD-1076) Break down 
`ClientUserAuthService#auth` method into several to allow for flexible override
 
 ## Behavioral changes and enhancements
 
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
index 52410b7..ab2b55c 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
@@ -39,6 +39,7 @@ import org.apache.sshd.common.Service;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.auth.UserAuthMethodFactory;
+import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.session.Session;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.ValidateUtils;
@@ -56,15 +57,15 @@ public class ClientUserAuthService extends 
AbstractCloseable implements Service,
      * The AuthFuture that is being used by the current auth request. This 
encodes the state. isSuccess ->
      * authenticated, else if isDone -> server waiting for user auth, else 
authenticating.
      */
-    private final AtomicReference<AuthFuture> authFutureHolder = new 
AtomicReference<>();
-    private final Map<String, Object> properties = new ConcurrentHashMap<>();
+    protected final AtomicReference<AuthFuture> authFutureHolder = new 
AtomicReference<>();
+    protected final ClientSessionImpl clientSession;
+    protected final List<UserAuthFactory> authFactories;
+    protected final List<String> clientMethods;
+    protected List<String> serverMethods;
 
-    private final ClientSessionImpl clientSession;
-    private final List<String> clientMethods;
-    private final List<UserAuthFactory> authFactories;
+    private final Map<String, Object> properties = new ConcurrentHashMap<>();
 
     private String service;
-    private List<String> serverMethods;
     private UserAuth userAuth;
     private int currentMethod;
 
@@ -125,24 +126,15 @@ public class ClientUserAuthService extends 
AbstractCloseable implements Service,
         // ignored
     }
 
+    public String getCurrentServiceName() {
+        return service;
+    }
+
     public AuthFuture auth(String service) throws IOException {
         this.service = ValidateUtils.checkNotNullAndNotEmpty(service, "No 
service name");
 
         ClientSession session = getClientSession();
-        // check if any previous future in use
-        AuthFuture authFuture = new DefaultAuthFuture(service, 
clientSession.getFutureLock());
-        AuthFuture currentFuture = authFutureHolder.getAndSet(authFuture);
-        boolean debugEnabled = log.isDebugEnabled();
-        if (currentFuture != null) {
-            if (currentFuture.isDone()) {
-                if (debugEnabled) {
-                    log.debug("auth({})[{}] request new authentication", 
session, service);
-                }
-            } else {
-                currentFuture.setException(
-                        new InterruptedIOException("New authentication started 
before previous completed"));
-            }
-        }
+        AuthFuture authFuture = updateCurrentAuthFuture(session, service);
 
         // start from scratch
         serverMethods = null;
@@ -155,7 +147,34 @@ public class ClientUserAuthService extends 
AbstractCloseable implements Service,
             }
         }
 
-        if (debugEnabled) {
+        sendInitialAuthRequest(session, service);
+        return authFuture;
+    }
+
+    protected AuthFuture updateCurrentAuthFuture(ClientSession session, String 
service) throws IOException {
+        // check if any previous future in use
+        AuthFuture authFuture = createAuthFuture(session, service);
+        AuthFuture currentFuture = authFutureHolder.getAndSet(authFuture);
+        if (currentFuture != null) {
+            if (currentFuture.isDone()) {
+                if (log.isDebugEnabled()) {
+                    log.debug("updateCurrentAuthFuture({})[{}] request new 
authentication", session, service);
+                }
+            } else {
+                currentFuture.setException(
+                        new InterruptedIOException("New authentication started 
before previous completed"));
+            }
+        }
+
+        return authFuture;
+    }
+
+    protected AuthFuture createAuthFuture(ClientSession session, String 
service) throws IOException {
+        return new DefaultAuthFuture(service, clientSession.getFutureLock());
+    }
+
+    protected IoWriteFuture sendInitialAuthRequest(ClientSession session, 
String service) throws IOException {
+        if (log.isDebugEnabled()) {
             log.debug("auth({})[{}] send SSH_MSG_USERAUTH_REQUEST for 'none'", 
session, service);
         }
 
@@ -165,9 +184,7 @@ public class ClientUserAuthService extends 
AbstractCloseable implements Service,
         buffer.putString(username);
         buffer.putString(service);
         buffer.putString("none");
-        session.writePacket(buffer);
-
-        return authFuture;
+        return session.writePacket(buffer);
     }
 
     @Override

Reply via email to