@@ -64,13 +64,12 @@ private void sendEncryptedMessage(Map<String, Object> msg) throws IOException {
64
64
String strMsg = jsonTxt (msg );
65
65
String encrypted = b64encode (box .box (strMsg .getBytes (), nonce ));
66
66
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
+ )));
74
73
incrementNonce ();
75
74
76
75
}
@@ -115,13 +114,12 @@ private JSONObject getEncryptedResponseAndDecrypt() throws IOException, KeepassP
115
114
*/
116
115
protected void changePublibKeys () throws IOException , KeepassProxyAccessException {
117
116
// 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
+ )));
125
123
JSONObject response = getCleartextResponse ();
126
124
127
125
if (!response .has ("success" )) {
@@ -144,12 +142,11 @@ public void associate() throws IOException, KeepassProxyAccessException {
144
142
idKeyPair = TweetNaclFast .Box .keyPair ();
145
143
146
144
// 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
+ ));
153
150
JSONObject response = getEncryptedResponseAndDecrypt ();
154
151
155
152
associate_id = response .getString ("id" );
@@ -164,10 +161,7 @@ public void associate() throws IOException, KeepassProxyAccessException {
164
161
*/
165
162
public String getDatabasehash () throws IOException , KeepassProxyAccessException {
166
163
// 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" ));
171
165
JSONObject response = getEncryptedResponseAndDecrypt ();
172
166
173
167
return response .getString ("hash" );
@@ -184,12 +178,11 @@ public String getDatabasehash() throws IOException, KeepassProxyAccessException
184
178
*/
185
179
public void testAssociate (String id , String key ) throws IOException , KeepassProxyAccessException {
186
180
// 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
+ ));
193
186
getEncryptedResponseAndDecrypt ();
194
187
195
188
}
@@ -217,14 +210,13 @@ public JSONObject getLogins(String url, String submitUrl, boolean httpAuth, List
217
210
}
218
211
219
212
// 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
+ ));
228
220
return getEncryptedResponseAndDecrypt ();
229
221
230
222
}
@@ -250,19 +242,18 @@ public JSONObject getLogins(String url, String submitUrl, boolean httpAuth, List
250
242
*/
251
243
public JSONObject setLogin (String url , String submitUrl , String id , String login , String password , String group , String groupUuid , String uuid ) throws IOException , KeepassProxyAccessException {
252
244
// 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
+ ));
266
257
return getEncryptedResponseAndDecrypt ();
267
258
268
259
}
@@ -276,10 +267,7 @@ public JSONObject setLogin(String url, String submitUrl, String id, String login
276
267
*/
277
268
public JSONObject getDatabaseGroups () throws IOException , KeepassProxyAccessException {
278
269
// 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" ));
283
271
return getEncryptedResponseAndDecrypt ();
284
272
285
273
}
@@ -293,12 +281,11 @@ public JSONObject getDatabaseGroups() throws IOException, KeepassProxyAccessExce
293
281
*/
294
282
public JSONObject generatePassword () throws IOException , KeepassProxyAccessException {
295
283
// 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
+ ));
302
289
return getEncryptedResponseAndDecrypt ();
303
290
304
291
}
@@ -312,10 +299,7 @@ public JSONObject generatePassword() throws IOException, KeepassProxyAccessExcep
312
299
*/
313
300
public JSONObject lockDatabase () throws IOException , KeepassProxyAccessException {
314
301
// 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" ));
319
303
return getEncryptedResponseAndDecrypt ();
320
304
321
305
}
@@ -332,11 +316,10 @@ public JSONObject lockDatabase() throws IOException, KeepassProxyAccessException
332
316
*/
333
317
public JSONObject createNewGroup (String path ) throws IOException , KeepassProxyAccessException {
334
318
// 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
+ ));
340
323
return getEncryptedResponseAndDecrypt ();
341
324
342
325
}
@@ -352,11 +335,10 @@ public JSONObject createNewGroup(String path) throws IOException, KeepassProxyAc
352
335
*/
353
336
public JSONObject getTotp (String uuid ) throws IOException , KeepassProxyAccessException {
354
337
// 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
+ ));
360
342
return getEncryptedResponseAndDecrypt ();
361
343
362
344
}
@@ -404,6 +386,10 @@ private String generateHEXUUID() {
404
386
return UUID .randomUUID ().toString ().replace ("-" , "" );
405
387
}
406
388
389
+ private String ensureNotNull (String param ) {
390
+ return null == param ? "" : param ;
391
+ }
392
+
407
393
// Getters
408
394
public String getIdKeyPairPublicKey () {
409
395
return null == idKeyPair ? null : b64encode (idKeyPair .getPublicKey ());
0 commit comments