]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/SimpleUserInfo.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.support / src / org / argeo / slc / jsch / SimpleUserInfo.java
1 package org.argeo.slc.jsch;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PushbackInputStream;
6 import java.util.Arrays;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11
12 import com.jcraft.jsch.UserInfo;
13
14 /** Basic implementation of user info. */
15 public class SimpleUserInfo implements UserInfo {
16 private Boolean permissive = true;
17 private Boolean verbose = false;
18
19 private final static Log log = LogFactory.getLog(SimpleUserInfo.class);
20
21 protected String password;
22 protected char[] passwordSafe;
23 protected String passphrase;
24 protected char[] passphraseSafe;
25
26 public void reset() {
27 if (passwordSafe != null)
28 Arrays.fill(passwordSafe, (char) 0);
29 passwordSafe = null;
30 if (passphraseSafe != null)
31 Arrays.fill(passphraseSafe, (char) 0);
32 passphraseSafe = null;
33 }
34
35 public void setPassword(String password) {
36 this.password = password;
37 }
38
39 public void setPassphrase(String passphrase) {
40 this.passphrase = passphrase;
41 }
42
43 public String getPassphrase() {
44 if (passphraseSafe != null)
45 return new String(passphraseSafe);
46 return passphrase;
47 }
48
49 public String getPassword() {
50 if (passwordSafe != null)
51 return new String(passwordSafe);
52 return password;
53 }
54
55 public boolean promptPassphrase(String message) {
56 if (permissive)
57 return true;
58 else {
59 log.info(message);
60 passwordSafe = readPassword(System.in);
61 return passwordSafe != null;
62 }
63 }
64
65 public boolean promptPassword(String message) {
66 if (permissive)
67 return true;
68 else {
69 log.info(message);
70 passwordSafe = readPassword(System.in);
71 return passwordSafe != null;
72 }
73 }
74
75 public boolean promptYesNo(String message) {
76 String msg = message + " (y/n): ";
77 if (permissive) {
78 if (verbose)
79 log.info(msg + "y");
80 return true;
81 } else {
82 log.info(msg);
83 char c;
84 try {
85 c = (char) System.in.read();
86 } catch (IOException e) {
87 throw new SlcException("Cannot read stdin", e);
88 }
89 if (c == 'y')
90 return true;
91 else
92 return false;
93 }
94 }
95
96 public void showMessage(String message) {
97 log.info(message);
98 }
99
100 public void setPermissive(Boolean permissive) {
101 this.permissive = permissive;
102 }
103
104 public void setVerbose(Boolean verbose) {
105 this.verbose = verbose;
106 }
107
108 protected char[] readPassword(InputStream in) {
109
110 try {
111 char[] lineBuffer;
112 char[] buf;
113 // int i;
114
115 buf = lineBuffer = new char[128];
116
117 int room = buf.length;
118 int offset = 0;
119 int c;
120
121 loop: while (true) {
122 switch (c = in.read()) {
123 case -1:
124 case '\n':
125 break loop;
126
127 case '\r':
128 int c2 = in.read();
129 if ((c2 != '\n') && (c2 != -1)) {
130 if (!(in instanceof PushbackInputStream)) {
131 in = new PushbackInputStream(in);
132 }
133 ((PushbackInputStream) in).unread(c2);
134 } else
135 break loop;
136
137 default:
138 if (--room < 0) {
139 buf = new char[offset + 128];
140 room = buf.length - offset - 1;
141 System.arraycopy(lineBuffer, 0, buf, 0, offset);
142 Arrays.fill(lineBuffer, ' ');
143 lineBuffer = buf;
144 }
145 buf[offset++] = (char) c;
146 break;
147 }
148 }
149
150 if (offset == 0) {
151 return null;
152 }
153
154 char[] ret = new char[offset];
155 System.arraycopy(buf, 0, ret, 0, offset);
156 Arrays.fill(buf, ' ');
157
158 return ret;
159 } catch (IOException e) {
160 throw new SlcException("Cannot read password.", e);
161 }
162 }
163
164 }