2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jsch
;
18 import javax
.security
.auth
.callback
.Callback
;
19 import javax
.security
.auth
.callback
.CallbackHandler
;
20 import javax
.security
.auth
.callback
.PasswordCallback
;
22 import org
.argeo
.slc
.SlcException
;
24 /** Retrieve a password or a passphrase using a standard callback handler. */
25 public final class CallbackHandlerUserInfo
extends SimpleUserInfo
{
26 private CallbackHandler callbackHandler
;
28 private Boolean alwaysPrompt
= false;
30 public boolean promptPassphrase(String message
) {
31 if (passphrase
!= null)
34 if (!alwaysPrompt
&& passphraseSafe
!= null)
38 PasswordCallback passwordCb
= new PasswordCallback("SSH Passphrase",
40 Callback
[] dialogCbs
= new Callback
[] { passwordCb
};
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
);
50 public boolean promptPassword(String message
) {
54 if (!alwaysPrompt
&& passwordSafe
!= null)
58 PasswordCallback passwordCb
= new PasswordCallback("SSH Password",
60 Callback
[] dialogCbs
= new Callback
[] { passwordCb
};
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
);
70 public void setAlwaysPrompt(Boolean alwaysPrompt
) {
71 this.alwaysPrompt
= alwaysPrompt
;
74 public void setCallbackHandler(CallbackHandler defaultCallbackHandler
) {
75 this.callbackHandler
= defaultCallbackHandler
;