Skip to content

Commit 1c41c3d

Browse files
committed
Improve code with convenience factory methods for collections
1 parent ec6846e commit 1c41c3d

File tree

3 files changed

+67
-85
lines changed

3 files changed

+67
-85
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050

5151
<properties>
5252
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
53-
<maven.compiler.source>1.8</maven.compiler.source>
54-
<maven.compiler.target>1.8</maven.compiler.target>
53+
<maven.compiler.source>1.9</maven.compiler.source>
54+
<maven.compiler.target>1.9</maven.compiler.target>
5555
</properties>
5656

5757
<!-- local repo for tweetnacl-java -->

src/main/java/org/keepassxc/Connection.java

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ private void sendEncryptedMessage(Map<String, Object> msg) throws IOException {
6464
String strMsg = jsonTxt(msg);
6565
String encrypted = b64encode(box.box(strMsg.getBytes(), nonce));
6666

67-
map = new HashMap<>();
68-
map.put("action", msg.get("action").toString());
69-
map.put("message", encrypted);
70-
map.put("nonce", b64encode(nonce));
71-
map.put("clientID", clientID);
72-
73-
sendCleartextMessage(jsonTxt(map));
67+
sendCleartextMessage(jsonTxt(Map.of(
68+
"action", msg.get("action").toString(),
69+
"message", encrypted,
70+
"nonce", b64encode(nonce),
71+
"clientID", clientID
72+
)));
7473
incrementNonce();
7574

7675
}
@@ -115,13 +114,12 @@ private JSONObject getEncryptedResponseAndDecrypt() throws IOException, KeepassP
115114
*/
116115
protected void changePublibKeys() throws IOException, KeepassProxyAccessException {
117116
// Send change-public-keys request
118-
map = new HashMap<>();
119-
map.put("action", "change-public-keys");
120-
map.put("publicKey", b64encode(keyPair.getPublicKey()));
121-
map.put("nonce", b64encode(nonce));
122-
map.put("clientID", clientID);
123-
124-
sendCleartextMessage(jsonTxt(map));
117+
sendCleartextMessage(jsonTxt(Map.of(
118+
"action", "change-public-keys",
119+
"publicKey", b64encode(keyPair.getPublicKey()),
120+
"nonce", b64encode(nonce),
121+
"clientID", clientID
122+
)));
125123
JSONObject response = getCleartextResponse();
126124

127125
if (!response.has("success")) {
@@ -144,12 +142,11 @@ public void associate() throws IOException, KeepassProxyAccessException {
144142
idKeyPair = TweetNaclFast.Box.keyPair();
145143

146144
// Send associate request
147-
map = new HashMap<>();
148-
map.put("action", "associate");
149-
map.put("key", b64encode(keyPair.getPublicKey()));
150-
map.put("idKey", b64encode(idKeyPair.getPublicKey()));
151-
152-
sendEncryptedMessage(map);
145+
sendEncryptedMessage(Map.of(
146+
"action", "associate",
147+
"key", b64encode(keyPair.getPublicKey()),
148+
"idKey", b64encode(idKeyPair.getPublicKey())
149+
));
153150
JSONObject response = getEncryptedResponseAndDecrypt();
154151

155152
associate_id = response.getString("id");
@@ -164,10 +161,7 @@ public void associate() throws IOException, KeepassProxyAccessException {
164161
*/
165162
public String getDatabasehash() throws IOException, KeepassProxyAccessException {
166163
// Send get-databasehash request
167-
map = new HashMap<>();
168-
map.put("action", "get-databasehash");
169-
170-
sendEncryptedMessage(map);
164+
sendEncryptedMessage(Map.of("action", "get-databasehash"));
171165
JSONObject response = getEncryptedResponseAndDecrypt();
172166

173167
return response.getString("hash");
@@ -184,12 +178,11 @@ public String getDatabasehash() throws IOException, KeepassProxyAccessException
184178
*/
185179
public void testAssociate(String id, String key) throws IOException, KeepassProxyAccessException {
186180
// Send test-associate request
187-
map = new HashMap<>();
188-
map.put("action", "test-associate");
189-
map.put("id", id);
190-
map.put("key", key);
191-
192-
sendEncryptedMessage(map);
181+
sendEncryptedMessage(Map.of(
182+
"action", "test-associate",
183+
"id", id,
184+
"key", key
185+
));
193186
getEncryptedResponseAndDecrypt();
194187

195188
}
@@ -217,14 +210,13 @@ public JSONObject getLogins(String url, String submitUrl, boolean httpAuth, List
217210
}
218211

219212
// Send get-logins
220-
map = new HashMap<>();
221-
map.put("action", "get-logins");
222-
map.put("url", url);
223-
map.put("submitUrl", submitUrl);
224-
map.put("httpAuth", httpAuth);
225-
map.put("keys", array);
226-
227-
sendEncryptedMessage(map);
213+
sendEncryptedMessage(Map.of(
214+
"action", "get-logins",
215+
"url", ensureNotNull(url),
216+
"submitUrl", ensureNotNull(submitUrl),
217+
"httpAuth", httpAuth,
218+
"keys", array
219+
));
228220
return getEncryptedResponseAndDecrypt();
229221

230222
}
@@ -250,19 +242,18 @@ public JSONObject getLogins(String url, String submitUrl, boolean httpAuth, List
250242
*/
251243
public JSONObject setLogin(String url, String submitUrl, String id, String login, String password, String group, String groupUuid, String uuid) throws IOException, KeepassProxyAccessException {
252244
// Send set-login
253-
map = new HashMap<>();
254-
map.put("action", "set-login");
255-
map.put("url", url);
256-
map.put("submitUrl", submitUrl);
257-
map.put("id", id);
258-
map.put("nonce", b64encode(nonce));
259-
map.put("login", login);
260-
map.put("password", password);
261-
map.put("group", group);
262-
map.put("groupUuid", group);
263-
map.put("uuid", uuid);
264-
265-
sendEncryptedMessage(map);
245+
sendEncryptedMessage(Map.of(
246+
"action", "set-login",
247+
"url", ensureNotNull(url),
248+
"submitUrl", ensureNotNull(submitUrl),
249+
"id", ensureNotNull(id),
250+
"nonce", b64encode(nonce),
251+
"login", ensureNotNull(login),
252+
"password", ensureNotNull(password),
253+
"group", ensureNotNull(group),
254+
"groupUuid", ensureNotNull(groupUuid),
255+
"uuid", ensureNotNull(uuid)
256+
));
266257
return getEncryptedResponseAndDecrypt();
267258

268259
}
@@ -276,10 +267,7 @@ public JSONObject setLogin(String url, String submitUrl, String id, String login
276267
*/
277268
public JSONObject getDatabaseGroups() throws IOException, KeepassProxyAccessException {
278269
// Send get-database-groups
279-
map = new HashMap<>();
280-
map.put("action", "get-database-groups");
281-
282-
sendEncryptedMessage(map);
270+
sendEncryptedMessage(Map.of("action", "get-database-groups"));
283271
return getEncryptedResponseAndDecrypt();
284272

285273
}
@@ -293,12 +281,11 @@ public JSONObject getDatabaseGroups() throws IOException, KeepassProxyAccessExce
293281
*/
294282
public JSONObject generatePassword() throws IOException, KeepassProxyAccessException {
295283
// Send generate-password request
296-
map = new HashMap<>();
297-
map.put("action", "generate-password");
298-
map.put("nonce", b64encode(nonce));
299-
map.put("clientID", clientID);
300-
301-
sendEncryptedMessage(map);
284+
sendEncryptedMessage(Map.of(
285+
"action", "generate-password",
286+
"nonce", b64encode(nonce),
287+
"clientID", clientID
288+
));
302289
return getEncryptedResponseAndDecrypt();
303290

304291
}
@@ -312,10 +299,7 @@ public JSONObject generatePassword() throws IOException, KeepassProxyAccessExcep
312299
*/
313300
public JSONObject lockDatabase() throws IOException, KeepassProxyAccessException {
314301
// Send lock-database request
315-
map = new HashMap<>();
316-
map.put("action", "lock-database");
317-
318-
sendEncryptedMessage(map);
302+
sendEncryptedMessage(Map.of("action", "lock-database"));
319303
return getEncryptedResponseAndDecrypt();
320304

321305
}
@@ -332,11 +316,10 @@ public JSONObject lockDatabase() throws IOException, KeepassProxyAccessException
332316
*/
333317
public JSONObject createNewGroup(String path) throws IOException, KeepassProxyAccessException {
334318
// Send create-new-group request
335-
map = new HashMap<>();
336-
map.put("action", "create-new-group");
337-
map.put("groupName", path);
338-
339-
sendEncryptedMessage(map);
319+
sendEncryptedMessage(Map.of(
320+
"action", "create-new-group",
321+
"groupName", ensureNotNull(path)
322+
));
340323
return getEncryptedResponseAndDecrypt();
341324

342325
}
@@ -352,11 +335,10 @@ public JSONObject createNewGroup(String path) throws IOException, KeepassProxyAc
352335
*/
353336
public JSONObject getTotp(String uuid) throws IOException, KeepassProxyAccessException {
354337
// Send get-totp request
355-
map = new HashMap<>();
356-
map.put("action", "get-totp");
357-
map.put("uuid", uuid);
358-
359-
sendEncryptedMessage(map);
338+
sendEncryptedMessage(Map.of(
339+
"action", "get-totp",
340+
"uuid", ensureNotNull(uuid)
341+
));
360342
return getEncryptedResponseAndDecrypt();
361343

362344
}
@@ -404,6 +386,10 @@ private String generateHEXUUID() {
404386
return UUID.randomUUID().toString().replace("-", "");
405387
}
406388

389+
private String ensureNotNull(String param) {
390+
return null == param ? "" : param;
391+
}
392+
407393
// Getters
408394
public String getIdKeyPairPublicKey() {
409395
return null == idKeyPair ? null : b64encode(idKeyPair.getPublicKey());

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public KeepassProxyAccess() {
3232
* @return The agreed associateID and IDKeyPublicKey.
3333
*/
3434
public Map<String, String> exportConnection() {
35-
Map<String, String> m = new HashMap<>();
36-
m.put("id", this.connection.getAssociateId());
37-
m.put("key", this.connection.getIdKeyPairPublicKey());
38-
return m;
35+
return Map.of("id", this.connection.getAssociateId(),
36+
"key", this.connection.getIdKeyPairPublicKey());
3937
}
4038

4139
public void connect() throws IOException, KeepassProxyAccessException {
@@ -92,10 +90,8 @@ public String getTotp(String uuid) throws IOException, KeepassProxyAccessExcepti
9290
* @return Last part of the path name of the group that was created with its according groupUuid.
9391
*/
9492
public Map<String, String> getNewGroupId(JSONObject jo) {
95-
Map<String, String> m = new HashMap<>();
96-
m.put("name", jo.getString("name"));
97-
m.put("uuid", jo.getString("uuid"));
98-
return m;
93+
return Map.of("name", jo.getString("name"),
94+
"uuid", jo.getString("uuid"));
9995
}
10096

10197
/**

0 commit comments

Comments
 (0)