diff --git a/app/build.gradle b/app/build.gradle index 997a85f7a..59d563152 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,7 +38,7 @@ android { } productFlavors { fdroid { - applicationId "fr.gouv.etalab.mastodon" + applicationId "fr.gouv.etalab.mastodon.updev" buildConfigField "boolean", "DONATIONS", "true" buildConfigField "boolean", "lite", "false" resValue "string", "app_name", "Fedilab" 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 2bdcde59d..0b872ffc7 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -5806,7 +5806,7 @@ public class API { params.put("subscription[endpoint]", endpoint); ECDH ecdh = ECDH.getInstance(); - String pubKey = ecdh.getPublicKey(context, account); + String pubKey = ecdh.getPublicKey(context); byte[] randBytes = new byte[16]; new Random().nextBytes(randBytes); String auth = Base64.encodeToString(randBytes, Base64.DEFAULT); @@ -5816,6 +5816,7 @@ public class API { String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/push/subscription"), 10, params, account.getToken()); 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); e.printStackTrace(); 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 3f00ad8fe..784bd3f37 100644 --- a/app/src/main/java/app/fedilab/android/helper/ECDH.java +++ b/app/src/main/java/app/fedilab/android/helper/ECDH.java @@ -116,7 +116,7 @@ public class ECDH { return new KeyPair(readPublicKey(pubKeyStr), readPrivateKey(privKeyStr)); } - KeyPair newPair(Context context, Account account) { + KeyPair newPair(Context context) { SharedPreferences.Editor prefsEditor = PreferenceManager .getDefaultSharedPreferences(context).edit(); @@ -129,21 +129,21 @@ public class ECDH { return null; } - prefsEditor.putString(kp_public + account.getId() + account.getInstance(), base64Encode(kp.getPublic().getEncoded())); - prefsEditor.putString(kp_private + account.getId() + account.getInstance(), base64Encode(kp.getPrivate().getEncoded())); + prefsEditor.putString(kp_public, base64Encode(kp.getPublic().getEncoded())); + prefsEditor.putString(kp_private, base64Encode(kp.getPrivate().getEncoded())); prefsEditor.commit(); return kp; } - synchronized KeyPair getPair(Context context, Account account) { + synchronized KeyPair getPair(Context context) { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(context); - String strPub = prefs.getString(kp_public + account.getId() + account.getInstance(), ""); - String strPriv = prefs.getString(kp_private + account.getId() + account.getInstance(), ""); + String strPub = prefs.getString(kp_public, ""); + String strPriv = prefs.getString(kp_private, ""); if (strPub.isEmpty() || strPriv.isEmpty()) { - return newPair(context, account); + return newPair(context); } try { return readKeyPair(strPub, strPriv); @@ -153,11 +153,11 @@ public class ECDH { return null; } - public String getPublicKey(Context context, Account account) { - return base64Encode(getPair(context, account).getPublic().getEncoded()); + public String getPublicKey(Context context) { + return base64Encode(getPair(context).getPublic().getEncoded()); } - void saveServerKey(Context context, Account account, String strPeerPublic) { + public void saveServerKey(Context context, Account account, String strPeerPublic) { SharedPreferences.Editor prefsEditor = PreferenceManager .getDefaultSharedPreferences(context).edit(); @@ -174,7 +174,7 @@ public class ECDH { byte[] getSecret(Context context, Account account) { try { - return generateSecret(getPair(context, account).getPrivate(), getServerKey(context, account)); + return generateSecret(getPair(context).getPrivate(), getServerKey(context, account)); } catch (Exception e) { e.printStackTrace(); return null; 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 85577e508..27e7f4ae4 100644 --- a/app/src/main/java/app/fedilab/android/helper/PushNotifications.java +++ b/app/src/main/java/app/fedilab/android/helper/PushNotifications.java @@ -1,7 +1,10 @@ package app.fedilab.android.helper; import android.content.Context; +import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import android.util.Base64; +import android.util.Log; import org.unifiedpush.android.connector.Registration; @@ -9,19 +12,26 @@ import java.util.List; import java.util.Random; import app.fedilab.android.activities.BaseMainActivity; +import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask; +import app.fedilab.android.client.APIResponse; import app.fedilab.android.client.Entities.Account; +import app.fedilab.android.interfaces.OnPostSubscription; +import app.fedilab.android.sqlite.AccountDAO; +import app.fedilab.android.sqlite.Sqlite; -public class PushNotifications { - public void registerPushNotifications(Context context, Account account, String endpoint, String server_key) { - ECDH ecdh = new ECDH(); - String pubKey = ecdh.getPublicKey(context, account); - byte[] randBytes = new byte[16]; - new Random().nextBytes(randBytes); - String auth = Base64.encodeToString(randBytes, Base64.DEFAULT); +import static android.content.Context.MODE_PRIVATE; - //register +public class PushNotifications implements OnPostSubscription { - ecdh.saveServerKey(context, account, server_key); + private Context context; + + public void registerPushNotifications(Context context, String endpoint) { + this.context = context; + SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List accountPush = new AccountDAO(context, db).getPushNotificationAccounts(); + for (Account account : accountPush) { + new PostSubscriptionAsyncTask(context, account, endpoint, this); + } } public static void getDistributors(Context context) { @@ -39,7 +49,10 @@ public class PushNotifications { //process with the event // https://openacs.org/webpush-demo/report.html // decrypt using AES 128 GCM + } - + @Override + public void onSubscription(APIResponse apiResponse) { + //TODO je ne sais pas si c'est toujours utile } } 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 2325739d8..d898ce87b 100644 --- a/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java +++ b/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java @@ -25,26 +25,21 @@ import app.fedilab.android.sqlite.Sqlite; import static android.content.Context.MODE_PRIVATE; -class handler implements MessagingReceiverHandler, OnPostSubscription { +class handler implements MessagingReceiverHandler { private Context context; private String endpoint; @Override - public void onNewEndpoint(@Nullable Context context, @NotNull String s) { - Log.v(Helper.TAG, "onNewEndpoint: " + s); + 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, s); + editor.putString(Helper.SERVER_ENDPOINT, endpoint); editor.apply(); - endpoint = s; - this.context = context; - SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - List accountPush = new AccountDAO(context, db).getPushNotificationAccounts(); - for (Account account : accountPush) { - new PostSubscriptionAsyncTask(context, account, s, this); - } + new PushNotifications() + .registerPushNotifications(context, endpoint); } @Override @@ -66,23 +61,10 @@ class handler implements MessagingReceiverHandler, OnPostSubscription { } @Override - public void onMessage(@Nullable Context context, @NotNull String s) { - PushNotifications push = new PushNotifications(); - Log.v(Helper.TAG, "Message: " + s); - push.displayNotification(context, null, s); - } - - @Override - public void onSubscription(APIResponse apiResponse) { - if (apiResponse != null && apiResponse.getPushSubscription() != null && apiResponse.getAccounts() != null) { - final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); - SharedPreferences.Editor editor = sharedpreferences.edit(); - Log.v(Helper.TAG, "OK: " + apiResponse.getAccounts().get(0).getId() + apiResponse.getAccounts().get(0).getInstance() + " --- " + apiResponse.getPushSubscription().getServer_key()); - editor.putString(Helper.SERVER_KEY + apiResponse.getAccounts().get(0).getId() + apiResponse.getAccounts().get(0).getInstance(), apiResponse.getPushSubscription().getServer_key()); - editor.apply(); - PushNotifications push = new PushNotifications(); - push.registerPushNotifications(context, apiResponse.getAccounts().get(0), endpoint, apiResponse.getPushSubscription().getServer_key()); - } + public void onMessage(@Nullable Context context, @NotNull String message) { + Log.v(Helper.TAG, "Message: " + message); + new PushNotifications() + .displayNotification(context, null, message); } }