]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
CallbackHandler JSCH user info
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 8 Nov 2012 17:52:59 +0000 (17:52 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 8 Nov 2012 17:52:59 +0000 (17:52 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5750 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/CallbackHandlerUserInfo.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SwingUserInfo.java

diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/CallbackHandlerUserInfo.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/CallbackHandlerUserInfo.java
new file mode 100644 (file)
index 0000000..03dd877
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.slc.jsch;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.PasswordCallback;
+
+import org.argeo.slc.SlcException;
+
+/** Retrieve a password or a passphrase using a standard callback handler. */
+public final class CallbackHandlerUserInfo extends SimpleUserInfo {
+       private CallbackHandler callbackHandler;
+
+       private Boolean alwaysPrompt = false;
+
+       public boolean promptPassphrase(String message) {
+               if (passphrase != null)
+                       return true;
+
+               if (!alwaysPrompt && passphraseSafe != null)
+                       return true;
+
+               reset();
+               PasswordCallback passwordCb = new PasswordCallback("SSH Passphrase",
+                               false);
+               Callback[] dialogCbs = new Callback[] { passwordCb };
+               try {
+                       callbackHandler.handle(dialogCbs);
+                       passphraseSafe = passwordCb.getPassword();
+                       return passphraseSafe != null;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot ask for a password", e);
+               }
+       }
+
+       public boolean promptPassword(String message) {
+               if (password != null)
+                       return true;
+
+               if (!alwaysPrompt && passwordSafe != null)
+                       return true;
+
+               reset();
+               PasswordCallback passwordCb = new PasswordCallback("SSH Password",
+                               false);
+               Callback[] dialogCbs = new Callback[] { passwordCb };
+               try {
+                       callbackHandler.handle(dialogCbs);
+                       passwordSafe = passwordCb.getPassword();
+                       return passwordSafe != null;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot ask for a password", e);
+               }
+       }
+
+       public void setAlwaysPrompt(Boolean alwaysPrompt) {
+               this.alwaysPrompt = alwaysPrompt;
+       }
+
+       public void setCallbackHandler(CallbackHandler defaultCallbackHandler) {
+               this.callbackHandler = defaultCallbackHandler;
+       }
+
+}
index 8e49c3979798b25ef7e758959edbc01015bfe42d..54b068155b6ea0e2cdfdf1e9d0f8d482a51310da 100644 (file)
@@ -26,6 +26,7 @@ import org.argeo.slc.SlcException;
 
 import com.jcraft.jsch.UserInfo;
 
+/** Basic implementation of user info. */
 public class SimpleUserInfo implements UserInfo {
        private Boolean permissive = true;
        private Boolean verbose = false;
index a10dcefcbc04c9e276ad0fc50724d2179db072a8..972580dd839c028e0b0196e53e2470415458bb52 100644 (file)
@@ -29,7 +29,7 @@ import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JPasswordField;
 
-
+/** Retrieves a password or a passphrase using standard Swing */
 public class SwingUserInfo extends SimpleUserInfo {
 
        private Boolean alwaysPrompt = false;