Skip to content

Commit 10d1117

Browse files
author
Emmanuel Hugonnet
committed
Bug #3591 : using a password field for prompting password instead of clear field
1 parent df9f5cd commit 10d1117

File tree

6 files changed

+175
-179
lines changed

6 files changed

+175
-179
lines changed

src/main/java/com/silverpeas/openoffice/AuthenticationInfo.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@
2424

2525
package com.silverpeas.openoffice;
2626

27+
import java.util.Arrays;
28+
2729
/**
2830
* Authentication information.
2931
* @author Ludovic Bertin
3032
*/
3133
public class AuthenticationInfo {
3234

33-
String login = null;
34-
String password = null;
35+
private String login = null;
36+
private char[] password = new char[0];
3537

3638
/**
3739
* @param login
38-
* @param password
40+
* @param pass
3941
*/
40-
public AuthenticationInfo(String login, String password) {
42+
public AuthenticationInfo(String login, char[] pass) {
4143
this.login = login;
42-
this.password = password;
44+
this.password = Arrays.copyOf(pass, pass.length);
4345
}
4446

4547
/**
@@ -59,22 +61,16 @@ public void setLogin(String login) {
5961
/**
6062
* @return the password
6163
*/
62-
public String getPassword() {
63-
return password;
64+
public char[] getPassword() {
65+
return Arrays.copyOf(password, password.length);
6466
}
6567

66-
/**
67-
* @param password the password to set
68-
*/
69-
public void setPassword(String password) {
70-
this.password = password;
71-
}
7268

7369
@Override
7470
public int hashCode() {
75-
int hash = 3;
71+
int hash = 7;
7672
hash = 47 * hash + (this.login != null ? this.login.hashCode() : 0);
77-
hash = 47 * hash + (this.password != null ? this.password.hashCode() : 0);
73+
hash = 47 * hash + Arrays.hashCode(this.password);
7874
return hash;
7975
}
8076

@@ -87,14 +83,14 @@ public boolean equals(Object obj) {
8783
return false;
8884
}
8985
final AuthenticationInfo other = (AuthenticationInfo) obj;
90-
if ((this.login == null) ? (other.login != null)
91-
: !this.login.equals(other.login)) {
86+
if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
9287
return false;
9388
}
94-
if ((this.password == null) ? (other.password != null)
95-
: !this.password.equals(other.password)) {
89+
if (!Arrays.equals(this.password, other.password)) {
9690
return false;
9791
}
9892
return true;
9993
}
94+
95+
10096
}
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
/**
22
* Copyright (C) 2000 - 2009 Silverpeas
33
*
4-
* This program is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU Affero General Public License as
6-
* published by the Free Software Foundation, either version 3 of the
7-
* License, or (at your option) any later version.
4+
* This program is free software: you can redistribute it and/or modify it under the terms of the
5+
* GNU Affero General Public License as published by the Free Software Foundation, either version 3
6+
* of the License, or (at your option) any later version.
87
*
9-
* As a special exception to the terms and conditions of version 3.0 of
10-
* the GPL, you may redistribute this Program in connection with Free/Libre
11-
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12-
* FLOSS exception. You should have received a copy of the text describing
13-
* the FLOSS exception, and it is also available here:
8+
* As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9+
* redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10+
* applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11+
* text describing the FLOSS exception, and it is also available here:
1412
* "http://repository.silverpeas.com/legal/licensing"
1513
*
16-
* This program is distributed in the hope that it will be useful,
17-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19-
* GNU Affero General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Affero General Public License for more details.
2017
*
21-
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
* You should have received a copy of the GNU Affero General Public License along with this program.
19+
* If not, see <http://www.gnu.org/licenses/>.
2320
*/
24-
2521
package com.silverpeas.openoffice.util;
2622

23+
import javax.swing.JLabel;
2724
import javax.swing.JOptionPane;
25+
import javax.swing.JPasswordField;
2826

2927
/**
3028
* Simple utility class to display messages graphically.
29+
*
3130
* @author ehugonnet
3231
*/
3332
public class MessageDisplayer {
@@ -41,9 +40,12 @@ public static void displayError(Throwable t) {
4140
JOptionPane.showMessageDialog(null, t.getMessage(),
4241
MessageUtil.getMessage("error.title"), JOptionPane.ERROR_MESSAGE);
4342
}
44-
45-
public static String displayPromptPassword() {
46-
return JOptionPane.showInputDialog(null, MessageUtil.getMessage("info.missing.password.label"),
47-
MessageUtil.getMessage("info.missing.password.title"), JOptionPane.PLAIN_MESSAGE);
43+
44+
public static char[] displayPromptPassword() {
45+
JLabel label = new JLabel(MessageUtil.getMessage("info.missing.password.label"));
46+
JPasswordField jpf = new JPasswordField();
47+
JOptionPane.showConfirmDialog(null, new Object[]{label, jpf},
48+
MessageUtil.getMessage("info.missing.password.title"), JOptionPane.OK_CANCEL_OPTION);
49+
return jpf.getPassword();
4850
}
4951
}
Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
/**
22
* Copyright (C) 2000 - 2009 Silverpeas
33
*
4-
* This program is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU Affero General Public License as
6-
* published by the Free Software Foundation, either version 3 of the
7-
* License, or (at your option) any later version.
4+
* This program is free software: you can redistribute it and/or modify it under the terms of the
5+
* GNU Affero General Public License as published by the Free Software Foundation, either version 3
6+
* of the License, or (at your option) any later version.
87
*
9-
* As a special exception to the terms and conditions of version 3.0 of
10-
* the GPL, you may redistribute this Program in connection with Free/Libre
11-
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12-
* FLOSS exception. You should have received a copy of the text describing
13-
* the FLOSS exception, and it is also available here:
8+
* As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9+
* redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10+
* applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11+
* text describing the FLOSS exception, and it is also available here:
1412
* "http://www.silverpeas.com/legal/licensing"
1513
*
16-
* This program is distributed in the hope that it will be useful,
17-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19-
* GNU Affero General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Affero General Public License for more details.
2017
*
21-
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
* You should have received a copy of the GNU Affero General Public License along with this program.
19+
* If not, see <http://www.gnu.org/licenses/>.
2320
*/
24-
2521
package com.silverpeas.openoffice.util;
2622

2723
import java.io.UnsupportedEncodingException;
@@ -43,40 +39,46 @@
4339
*/
4440
public class PasswordManager {
4541

46-
private final static byte[] KEY = new byte[] { -23, -75, -2, -17, 79, -94, -125,
47-
-14 };
42+
private final static byte[] KEY = new byte[]{-23, -75, -2, -17, 79, -94, -125,
43+
-14};
4844
private final static Key decryptionKey = new SecretKeySpec(KEY, "DES");
4945
private final static String DIGITS = "0123456789abcdef";
5046

5147
/**
5248
* Converts a hexadecimal String to clear password.
53-
* @param hex hexadecimal String to convert
49+
*
50+
* @param encodedPassword password to decode.
5451
* @return resulting password
52+
* @throws UnsupportedEncodingException
53+
* @throws GeneralSecurityException
5554
*/
56-
public static String decodePassword(String encodedPassword) throws
57-
UnsupportedEncodingException, GeneralSecurityException {
55+
public static char[] decodePassword(String encodedPassword) throws UnsupportedEncodingException,
56+
GeneralSecurityException {
5857
Cipher cipher = Cipher.getInstance("DES");
5958
cipher.init(Cipher.DECRYPT_MODE, decryptionKey);
6059
byte[] bytes = new BigInteger(encodedPassword, 16).toByteArray();
61-
Logger.getLogger(Launcher.class.getName()).log(Level.INFO, "decrypted password byte array length : {0}", bytes.length);
60+
Logger.getLogger(Launcher.class.getName()).log(Level.INFO,
61+
"decrypted password byte array length : {0}", bytes.length);
6262
int nbCaracToRemove = (bytes.length) % 8;
6363
byte[] result = new byte[bytes.length - nbCaracToRemove];
64-
System.arraycopy(bytes, nbCaracToRemove, result, 0, bytes.length -
65-
nbCaracToRemove);
66-
return new String(cipher.doFinal(result), "UTF-8");
64+
System.arraycopy(bytes, nbCaracToRemove, result, 0, bytes.length - nbCaracToRemove);
65+
return new String(cipher.doFinal(result), "UTF-8").toCharArray();
6766
}
6867

6968
/**
7069
* Converts a password to a hexadecimal String containing the DES encrypted password.
70+
*
7171
* @param password the password to encrypt
7272
* @return resulting hexadecimal String
73+
* @throws UnsupportedEncodingException
74+
* @throws GeneralSecurityException
7375
*/
74-
public static String encodePassword(String password) throws
75-
UnsupportedEncodingException, GeneralSecurityException {
76+
public static String encodePassword(String password) throws UnsupportedEncodingException,
77+
GeneralSecurityException {
7678
Cipher cipher = Cipher.getInstance("DES");
7779
cipher.init(Cipher.ENCRYPT_MODE, decryptionKey);
7880
byte[] cipherText = cipher.doFinal(password.getBytes("UTF-8"));
79-
StringBuilder buf = new StringBuilder();
81+
StringBuilder buf = new StringBuilder(cipherText.length);
8082
for (int i = 0; i != cipherText.length; i++) {
8183
int v = cipherText[i] & 0xff;
8284
buf.append(DIGITS.charAt(v >> 4));
@@ -87,27 +89,30 @@ public static String encodePassword(String password) throws
8789

8890
/**
8991
* Build an authentication objec from arguments
90-
* @param args arguments passed through JNLP
92+
*
93+
* @param login
94+
* @param encodedPassword
9195
* @return the Authentication object
9296
*/
9397
public static AuthenticationInfo extractAuthenticationInfo(String login, String encodedPassword) {
94-
String clearPwd = null;
95-
String decodedLogin = login;
9698
try {
97-
clearPwd = decodePassword(encodedPassword);
98-
if(clearPwd.isEmpty()) {
99+
char[] clearPwd = decodePassword(encodedPassword);
100+
if (clearPwd.length <= 0) {
99101
clearPwd = promptForpassword();
100102
}
101-
decodedLogin = URLDecoder.decode(login, "UTF-8");
102-
} catch (Exception e) {
103-
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, "can't retrieve credentials", e);
103+
String decodedLogin = URLDecoder.decode(login, "UTF-8");
104+
return new AuthenticationInfo(decodedLogin, clearPwd);
105+
} catch (GeneralSecurityException ex) {
106+
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, "can't retrieve credentials", ex);
107+
System.exit(-1);
108+
} catch (UnsupportedEncodingException ex) {
109+
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, "can't retrieve credentials", ex);
104110
System.exit(-1);
105111
}
106-
107-
return new AuthenticationInfo(decodedLogin, clearPwd);
112+
return null;
108113
}
109114

110-
private static String promptForpassword() {
115+
private static char[] promptForpassword() {
111116
return MessageDisplayer.displayPromptPassword();
112117
}
113118
}

src/main/java/com/silverpeas/openoffice/windows/FileWebDavAccessManager.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
/**
22
* Copyright (C) 2000 - 2009 Silverpeas
33
*
4-
* This program is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU Affero General Public License as
6-
* published by the Free Software Foundation, either version 3 of the
7-
* License, or (at your option) any later version.
4+
* This program is free software: you can redistribute it and/or modify it under the terms of the
5+
* GNU Affero General Public License as published by the Free Software Foundation, either version 3
6+
* of the License, or (at your option) any later version.
87
*
9-
* As a special exception to the terms and conditions of version 3.0 of
10-
* the GPL, you may redistribute this Program in connection with Free/Libre
11-
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12-
* FLOSS exception. You should have received a copy of the text describing
13-
* the FLOSS exception, and it is also available here:
8+
* As a special exception to the terms and conditions of version 3.0 of the GPL, you may
9+
* redistribute this Program in connection with Free/Libre Open Source Software ("FLOSS")
10+
* applications as described in Silverpeas's FLOSS exception. You should have received a copy of the
11+
* text describing the FLOSS exception, and it is also available here:
1412
* "http://repository.silverpeas.com/legal/licensing"
1513
*
16-
* This program is distributed in the hope that it will be useful,
17-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19-
* GNU Affero General Public License for more details.
14+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Affero General Public License for more details.
2017
*
21-
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
* You should have received a copy of the GNU Affero General Public License along with this program.
19+
* If not, see <http://www.gnu.org/licenses/>.
2320
*/
24-
2521
package com.silverpeas.openoffice.windows;
2622

2723
import com.silverpeas.openoffice.AuthenticationInfo;
@@ -38,17 +34,19 @@
3834

3935
/**
4036
* This class manage download and upload of documents using webdav protocol.
37+
*
4138
* @author Ludovic Bertin
4239
*/
4340
public class FileWebDavAccessManager {
4441

4542
private String userName;
46-
private String password;
43+
private char[] password;
4744
private String lockToken = null;
4845
static Logger logger = Logger.getLogger(FileWebDavAccessManager.class.getName());
4946

5047
/**
5148
* The AccessManager is inited with authentication info to avoid login prompt
49+
*
5250
* @param auth authentication info
5351
*/
5452
public FileWebDavAccessManager(AuthenticationInfo auth) {
@@ -58,14 +56,15 @@ public FileWebDavAccessManager(AuthenticationInfo auth) {
5856

5957
/**
6058
* Retrieve the file from distant URL to local temp file.
59+
*
6160
* @param url document url
6261
* @return full path of local temp file
6362
* @throws HttpException
6463
* @throws IOException
6564
*/
6665
public String retrieveFile(String url) throws HttpException, IOException {
6766
URI uri = getURI(url);
68-
WebdavManager webdav = new WebdavManager(uri.getHost(), userName, password);
67+
WebdavManager webdav = new WebdavManager(uri.getHost(), userName, new String(password));
6968
// Let's lock the file
7069
lockToken = webdav.lockFile(uri, userName);
7170
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage("info.webdav.locked"),
@@ -78,18 +77,20 @@ public String retrieveFile(String url) throws HttpException, IOException {
7877

7978
/**
8079
* Push back file into remote location using webdav.
80+
*
8181
* @param tmpFilePath full path of local temp file
8282
* @param url remote url
8383
* @throws HttpException
8484
* @throws IOException
8585
*/
86-
public void pushFile(String tmpFilePath, String url) throws HttpException,
87-
IOException {
86+
public void pushFile(String tmpFilePath, String url) throws HttpException, IOException {
8887
URI uri = getURI(url);
89-
WebdavManager webdav = new WebdavManager(uri.getHost(), userName, password);
90-
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage("info.webdav.put"), ' ', tmpFilePath});
88+
WebdavManager webdav = new WebdavManager(uri.getHost(), userName, new String(password));
89+
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage("info.webdav.put"), ' ',
90+
tmpFilePath});
9191
webdav.putFile(uri, tmpFilePath, lockToken);
92-
logger.log(Level.INFO, "{0}{1}{2}", new Object[]{MessageUtil.getMessage("info.webdav.unlocking"), ' ', uri.getEscapedURI()});
92+
logger.log(Level.INFO, "{0}{1}{2}",
93+
new Object[]{MessageUtil.getMessage("info.webdav.unlocking"), ' ', uri.getEscapedURI()});
9394
// Let's unlock the file
9495
webdav.unlockFile(uri, lockToken);
9596
// delete temp file

0 commit comments

Comments
 (0)