From f39ec92bcfea5733e4fcd4c387f5078a3e8f2a8d Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 26 Feb 2021 19:03:36 +0100 Subject: [PATCH] some improvements --- .../java/app/fedilab/android/client/API.java | 22 ------------------- .../java/app/fedilab/android/helper/ECDH.java | 5 +++-- .../android/helper/PushNotifications.java | 13 +++++++++-- .../android/services/UnifiedPushService.java | 7 +----- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index a360a9ca3..1e10afbc7 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -5790,7 +5790,6 @@ public class API { PushSubscription pushSubscription = null; try { String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/push/subscription"), 10, null, prefKeyOauthTokenT); - Log.v(Helper.TAG, "response GET: " + response); pushSubscription = parsePushNotifications(new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); @@ -5835,7 +5834,6 @@ public class API { String response = new HttpsConnection(context, this.instance).put(getAbsoluteUrl("/push/subscription"), 10, params, prefKeyOauthTokenT); pushSubscription = parsePushNotifications(new JSONObject(response)); - Log.v(Helper.TAG, "response3: " + response); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); e.printStackTrace(); @@ -5861,32 +5859,14 @@ public class API { boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, true); boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true); - HashMap params = new HashMap<>(); - - /*try { - endpoint = URLEncoder.encode(endpoint, "UTF-8"); - } catch (UnsupportedEncodingException ignored) { - }*/ - ECDH ecdh = ECDH.getInstance(); String pubKey = ecdh.getPublicKey(context); byte[] randBytes = new byte[16]; new Random().nextBytes(randBytes); String auth = ECDH.base64Encode(randBytes); - - /*try { - pubKey = URLEncoder.encode(pubKey.replaceAll("\n",""), "UTF-8"); - } catch (UnsupportedEncodingException ignored) { - } - try { - auth = URLEncoder.encode(auth.replaceAll("\n",""), "UTF-8"); - } catch (UnsupportedEncodingException ignored) { - }*/ - JSONObject jsonObject = new JSONObject(); - try { JSONObject jsonObjectSub = new JSONObject(); @@ -5909,11 +5889,9 @@ public class API { } catch (JSONException e) { e.printStackTrace(); } - Log.v(Helper.TAG, "jsonObject: " + jsonObject); try { String response = new HttpsConnection(context, this.instance).postJson(getAbsoluteUrl("/push/subscription"), 10, jsonObject, prefKeyOauthTokenT); pushSubscription = parsePushNotifications(new JSONObject(response)); - Log.v(Helper.TAG, "response: " + response); ecdh.saveServerKey(context, account, pushSubscription.getServer_key()); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); diff --git a/app/src/main/java/app/fedilab/android/helper/ECDH.java b/app/src/main/java/app/fedilab/android/helper/ECDH.java index f6917b6b6..3e4cda9f0 100644 --- a/app/src/main/java/app/fedilab/android/helper/ECDH.java +++ b/app/src/main/java/app/fedilab/android/helper/ECDH.java @@ -51,6 +51,7 @@ public class ECDH { private static ECDH instance; + static { // Security.removeProvider("BC"); Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider()); @@ -85,7 +86,7 @@ public class ECDH { } static byte[] base64Decode(String str) { - return Base64.decode(str, Base64.DEFAULT); + return Base64.decode(str, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); } synchronized KeyPair generateKeyPair() @@ -166,7 +167,7 @@ public class ECDH { String strPub = prefs.getString(kp_public, ""); String strPriv = prefs.getString(kp_private, ""); - if (strPub.isEmpty() || strPriv.isEmpty() || 1 == 1) { + if (strPub.isEmpty() || strPriv.isEmpty()) { return newPair(context); } try { diff --git a/app/src/main/java/app/fedilab/android/helper/PushNotifications.java b/app/src/main/java/app/fedilab/android/helper/PushNotifications.java index 27e7f4ae4..451787fd7 100644 --- a/app/src/main/java/app/fedilab/android/helper/PushNotifications.java +++ b/app/src/main/java/app/fedilab/android/helper/PushNotifications.java @@ -8,6 +8,7 @@ import android.util.Log; import org.unifiedpush.android.connector.Registration; +import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -43,9 +44,17 @@ public class PushNotifications implements OnPostSubscription { } } - public void displayNotification(Context context, Account account, String ciphered) { + public void displayNotification(Context context, String ciphered) { ECDH ecdh = new ECDH(); - byte[] secret = ecdh.getSecret(context, account); + + SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List accountPush = new AccountDAO(context, db).getPushNotificationAccounts(); + List trousseau = new ArrayList<>(); + for (Account account : accountPush) { + byte[] secret = ecdh.getSecret(context, account); + } + + //process with the event // https://openacs.org/webpush-demo/report.html // decrypt using AES 128 GCM diff --git a/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java b/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java index d898ce87b..686c7afff 100644 --- a/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java +++ b/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java @@ -32,7 +32,6 @@ class handler implements MessagingReceiverHandler { @Override public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint) { - Log.v(Helper.TAG, "onNewEndpoint: " + endpoint); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.SERVER_ENDPOINT, endpoint); @@ -44,27 +43,23 @@ class handler implements MessagingReceiverHandler { @Override public void onRegistrationFailed(@Nullable Context context) { - Log.v(Helper.TAG, "onRegistrationFailed: "); // Toast ? } @Override public void onRegistrationRefused(@Nullable Context context) { // Toast ? - Log.v(Helper.TAG, "onRegistrationRefused: "); } @Override public void onUnregistered(@Nullable Context context) { - Log.v(Helper.TAG, "onUnregistered: "); // Remove endpoint & ServerKey } @Override public void onMessage(@Nullable Context context, @NotNull String message) { - Log.v(Helper.TAG, "Message: " + message); new PushNotifications() - .displayNotification(context, null, message); + .displayNotification(context, message); } }