/
Login.java
314 lines (282 loc) · 10.1 KB
/
Login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package ch.cyberduck.core;
/*
* Copyright (c) 2005 David Kocher. All rights reserved.
* http://cyberduck.ch/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Bug fixes, suggestions and comments should be sent to:
* dkocher@cyberduck.ch
*/
import ch.cyberduck.ui.LoginController;
import com.apple.cocoa.foundation.NSAutoreleasePool;
import com.apple.cocoa.foundation.NSBundle;
import org.apache.log4j.Logger;
/**
* @version $Id$
*/
public class Login {
private static Logger log = Logger.getLogger(Login.class);
public Object clone() {
Login l = new Login(this.hostname, this.protocol,
this.getUsername(), this.getPassword());
l.setPrivateKeyFile(this.privateKeyFile);
return l;
}
private String hostname;
/**
* See #Session
*/
private String protocol;
private String user;
private transient String pass;
/**
* If not null, use public key authentication if SSH is the protocol
*/
private String privateKeyFile;
private boolean shouldBeAddedToKeychain;
/**
* ProtocolTypeConstants as defined in SecKeychain.h; see #708
*/
private static final String kSecProtocolTypeFTP = "ftp ";
private static final String kSecProtocolTypeFTPS = "ftps";
private static final String kSecProtocolTypeSSH = "ssh ";
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getUsername() {
return this.user;
}
public void setUsername(String user) {
this.init(user, this.getPassword());
}
public String getPassword() {
return this.pass;
}
public void setPassword(String pass) {
this.init(this.getUsername(), pass);
}
/**
* Use this to define if passwords should be added to the keychain
*
* @param shouldBeAddedToKeychain If true, the password of the login is added to the keychain uppon
* successfull login
*/
public void setUseKeychain(boolean shouldBeAddedToKeychain) {
this.shouldBeAddedToKeychain = shouldBeAddedToKeychain;
}
/**
*
* @return true if the password will be added to the system keychain when logged in successfully
*/
public boolean usesKeychain() {
return this.shouldBeAddedToKeychain;
}
/**
*
* @param protocol
* @return The protocol code as defined in SecKeychain.h
*/
private String getKSecProtocolType(String protocol) {
if(this.protocol.equals(Session.SFTP)) {
return kSecProtocolTypeSSH;
}
if(this.protocol.equals(Session.FTP_TLS)) {
return kSecProtocolTypeFTPS;
}
return kSecProtocolTypeFTP;
}
/**
*
* @return the password fetched from the keychain or null if it was not found
*/
public String getInternetPasswordFromKeychain() {
int pool = NSAutoreleasePool.push();
log.info("Fetching password from Keychain for:" + this.getUsername());
String password = Keychain.instance().getInternetPasswordFromKeychain(this.getKSecProtocolType(this.protocol),
this.hostname, this.getUsername());
NSAutoreleasePool.pop(pool);
return password;
}
/**
* Adds the password to the system keychain
*/
public void addInternetPasswordToKeychain() {
if (this.shouldBeAddedToKeychain && !this.isAnonymousLogin() && this.hasReasonableValues()) {
int pool = NSAutoreleasePool.push();
Keychain.instance().addInternetPasswordToKeychain(this.getKSecProtocolType(this.protocol),
this.hostname, this.getUsername(), this.getPassword());
NSAutoreleasePool.pop(pool);
}
}
/**
*
* @return the password fetched from the system keychain or null if it was not found
* @deprecated
*/
public String getPasswordFromKeychain() {
int pool = NSAutoreleasePool.push();
String pass = Keychain.instance().getPasswordFromKeychain(this.hostname, this.getUsername());
NSAutoreleasePool.pop(pool);
return pass;
}
/**
* @param hostname The service to use when looking up the password in the keychain
* @param user Login with this username
* @param pass Passphrase
*/
public Login(String hostname, String protocol, String user, String pass) {
this(hostname, protocol, user, pass, false);
}
/**
* @param hostname The hostname to use when looking up the password in the keychain
* @param user Login with this username
* @param pass Passphrase
* @param shouldBeAddedToKeychain if the credential should be added to the keychain uppon successful login
*/
public Login(String hostname, String protocol, String user, String pass, boolean shouldBeAddedToKeychain) {
this.hostname = hostname;
this.protocol = protocol;
this.shouldBeAddedToKeychain = shouldBeAddedToKeychain;
this.init(user, pass);
}
/**
* @param u The username to use or null if anonymous
* @param p The password to use or null if anonymous
*/
private void init(String u, String p) {
if (null == u || u.equals("")) {
if(this.protocol.equals(Session.FTP)) {
this.user = Preferences.instance().getProperty("ftp.anonymous.name");
}
else {
this.user = Preferences.instance().getProperty("connection.login.name");
}
}
else {
this.user = u;
}
if (null == p || p.equals("")) {
if (this.isAnonymousLogin()) {
this.pass = Preferences.instance().getProperty("ftp.anonymous.pass");
}
else {
this.pass = p;
}
}
else {
this.pass = p;
}
}
/**
*
* @return true if the username is anononymous
*/
public boolean isAnonymousLogin() {
return Preferences.instance().getProperty("ftp.anonymous.name").equals(this.getUsername());
}
/**
* SSH specific
*
* @return true if public key authentication should be used. This is the case, if a
* private key file has been specified
* @see #setPrivateKeyFile
*/
public boolean usesPublicKeyAuthentication() {
return this.privateKeyFile != null && this.protocol.equals(Session.SFTP);
}
public void setPrivateKeyFile(String file) {
this.privateKeyFile = file;
}
public String getPrivateKeyFile() {
return this.privateKeyFile;
}
/**
* Checks if both username and password qualify for a possible reasonable login attempt
* @return true if both username and password could be valid
*/
public boolean hasReasonableValues() {
if (this.usesPublicKeyAuthentication()) {
return true;
}
if (this.getUsername() != null && this.getPassword() != null) {
// anonymous login is ok
if (Preferences.instance().getProperty("ftp.anonymous.name").equals(this.getUsername()) &&
Preferences.instance().getProperty("ftp.anonymous.pass").equals(this.getPassword())) {
return true;
}
// if both name and pass are custom it is ok
if (!(Preferences.instance().getProperty("ftp.anonymous.name").equals(this.getUsername())) &&
!(Preferences.instance().getProperty("ftp.anonymous.pass").equals(this.getPassword()))) {
return true;
}
}
return false;
}
/**
*
* @param controller
* @return true if reasonable values have been found localy or in the keychain or the user
* was prompted to for the credentials and new values got entered.
*/
public boolean check(LoginController controller) {
if (!this.hasReasonableValues()) {
if (Preferences.instance().getBoolean("connection.login.useKeychain")) {
log.info("Searching keychain for password...");
String passFromKeychain = this.getInternetPasswordFromKeychain();
if (null == passFromKeychain || passFromKeychain.equals("")) {
passFromKeychain = this.getPasswordFromKeychain(); //legacy support
}
if (null == passFromKeychain || passFromKeychain.equals("")) {
if(null == controller) {
throw new IllegalArgumentException("No login controller given");
}
controller.promptUser(this,
NSBundle.localizedString("Login with username and password", "Credentials", ""),
NSBundle.localizedString("No login credentials could be found in the Keychain", "Credentials", ""));
return this.tryAgain();
}
else {
this.pass = passFromKeychain;
return true;
}
}
else {
if(null == controller) {
throw new IllegalArgumentException("No login controller given");
}
controller.promptUser(this,
NSBundle.localizedString("Login with username and password", "Credentials", ""),
NSBundle.localizedString("The use of the Keychain is disabled in the Preferences", "Credentials", ""));
return this.tryAgain();
}
}
return true;
}
private boolean tryAgain;
/**
*
* @return true if the user decided to try login again with new credentials
*/
public boolean tryAgain() {
return this.tryAgain;
}
/**
*
* @param v
*/
public void setTryAgain(boolean v) {
this.tryAgain = v;
}
}