]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/jsch/CallbackHandlerUserInfo.java
Merge branch 'master' of https://github.com/argeo/argeo-slc.git
[gpl/argeo-slc.git] / org.argeo.slc.support / src / org / argeo / slc / jsch / CallbackHandlerUserInfo.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jsch;
17
18 import javax.security.auth.callback.Callback;
19 import javax.security.auth.callback.CallbackHandler;
20 import javax.security.auth.callback.PasswordCallback;
21
22 import org.argeo.slc.SlcException;
23
24 /** Retrieve a password or a passphrase using a standard callback handler. */
25 public final class CallbackHandlerUserInfo extends SimpleUserInfo {
26 private CallbackHandler callbackHandler;
27
28 private Boolean alwaysPrompt = false;
29
30 public boolean promptPassphrase(String message) {
31 if (passphrase != null)
32 return true;
33
34 if (!alwaysPrompt && passphraseSafe != null)
35 return true;
36
37 reset();
38 PasswordCallback passwordCb = new PasswordCallback("SSH Passphrase",
39 false);
40 Callback[] dialogCbs = new Callback[] { passwordCb };
41 try {
42 callbackHandler.handle(dialogCbs);
43 passphraseSafe = passwordCb.getPassword();
44 return passphraseSafe != null;
45 } catch (Exception e) {
46 throw new SlcException("Cannot ask for a password", e);
47 }
48 }
49
50 public boolean promptPassword(String message) {
51 if (password != null)
52 return true;
53
54 if (!alwaysPrompt && passwordSafe != null)
55 return true;
56
57 reset();
58 PasswordCallback passwordCb = new PasswordCallback("SSH Password",
59 false);
60 Callback[] dialogCbs = new Callback[] { passwordCb };
61 try {
62 callbackHandler.handle(dialogCbs);
63 passwordSafe = passwordCb.getPassword();
64 return passwordSafe != null;
65 } catch (Exception e) {
66 throw new SlcException("Cannot ask for a password", e);
67 }
68 }
69
70 public void setAlwaysPrompt(Boolean alwaysPrompt) {
71 this.alwaysPrompt = alwaysPrompt;
72 }
73
74 public void setCallbackHandler(CallbackHandler defaultCallbackHandler) {
75 this.callbackHandler = defaultCallbackHandler;
76 }
77
78 }