Skip to content

Commit fe35c12

Browse files
committed
Improve loginExists to see, whether a login with a different or none password exists
1 parent 27c6d29 commit fe35c12

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/main/java/org/purejava/KeepassProxyAccess.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public Map<String, Object> getLogins(String url, String submitUrl, boolean httpA
246246
}
247247

248248
/**
249-
* Checks, if a password is stored in the KeePassXC databases. This method calls
249+
* Checks, whether a login exists and a given password is stored in the KeePassXC databases. This method calls
250250
* {@link org.purejava.KeepassProxyAccess#getLogins(String, String, boolean, List) getLogins} to search
251251
* the KeePassXC databases.
252252
* @see org.purejava.KeepassProxyAccess#getLogins(String, String, boolean, List)
@@ -256,19 +256,19 @@ public Map<String, Object> getLogins(String url, String submitUrl, boolean httpA
256256
* @param httpAuth Include database entries into search that are restricted to HTTP Basic Auth.
257257
* @param list Id / key combinations identifying and granting access to KeePassXC databases.
258258
* @param password Password to check.
259-
* @return True, if the password was found in a KeePassXC database, false otherwise.
259+
* @return ValidLogin The object describes whether a valid login exists for the given URL and whether the given password matches too.
260260
*/
261-
public boolean loginExists(String url, String submitUrl, boolean httpAuth, List<Map<String, String>> list, String password) {
261+
public ValidLogin loginExists(String url, String submitUrl, boolean httpAuth, List<Map<String, String>> list, String password) {
262262
var response = getLogins(url, submitUrl, httpAuth, list);
263263
if (response.isEmpty()) {
264-
return false;
264+
return new ValidLogin(false, null);
265265
}
266266
var array = (ArrayList<Object>) response.get("entries");
267267
for (Object o : array) {
268268
var credentials = (HashMap<String, Object>) o;
269-
if (credentials.get("password").equals(password)) return true;
269+
if (credentials.get("password").equals(password)) return new ValidLogin(true, credentials.get("uuid").toString());
270270
}
271-
return false;
271+
return new ValidLogin(true, null);
272272
}
273273

274274
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.purejava;
2+
3+
import java.util.List;
4+
5+
/**
6+
* This holds the result of a call to {@link org.purejava.KeepassProxyAccess#loginExists(String, String, boolean, List, String)}
7+
* @see org.purejava.KeepassProxyAccess#loginExists(String, String, boolean, List, String)
8+
*/
9+
public class ValidLogin {
10+
private boolean found;
11+
private String uuid;
12+
13+
/**
14+
* Does a valid login exist for the given URL? And does the given password match too?
15+
* @param found True, if an entry was found in the KeePassXC database for the given URL.
16+
* @param uuid If found is true, this contains either the uuid, in case the given password matches
17+
* the password already stored in the entry or null in case the given password does not match.
18+
*/
19+
public ValidLogin(boolean found, String uuid) {
20+
this.found = found;
21+
this.uuid = uuid;
22+
}
23+
24+
public boolean isFound() {
25+
return found;
26+
}
27+
28+
public String getUuid() {
29+
return uuid;
30+
}
31+
}

0 commit comments

Comments
 (0)