Merge branch 'unifiedpush-wip' into 'unifiedpush'

UP: move code to helper + single private key

See merge request tom79/fedilab!1033
This commit is contained in:
Thomas 2021-02-24 09:06:32 +00:00
commit 29a21f6fc5
5 changed files with 47 additions and 51 deletions

View File

@ -38,7 +38,7 @@ android {
} }
productFlavors { productFlavors {
fdroid { fdroid {
applicationId "fr.gouv.etalab.mastodon" applicationId "fr.gouv.etalab.mastodon.updev"
buildConfigField "boolean", "DONATIONS", "true" buildConfigField "boolean", "DONATIONS", "true"
buildConfigField "boolean", "lite", "false" buildConfigField "boolean", "lite", "false"
resValue "string", "app_name", "Fedilab" resValue "string", "app_name", "Fedilab"

View File

@ -5806,7 +5806,7 @@ public class API {
params.put("subscription[endpoint]", endpoint); params.put("subscription[endpoint]", endpoint);
ECDH ecdh = ECDH.getInstance(); ECDH ecdh = ECDH.getInstance();
String pubKey = ecdh.getPublicKey(context, account); String pubKey = ecdh.getPublicKey(context);
byte[] randBytes = new byte[16]; byte[] randBytes = new byte[16];
new Random().nextBytes(randBytes); new Random().nextBytes(randBytes);
String auth = Base64.encodeToString(randBytes, Base64.DEFAULT); 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()); String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/push/subscription"), 10, params, account.getToken());
pushSubscription = parsePushNotifications(new JSONObject(response)); pushSubscription = parsePushNotifications(new JSONObject(response));
Log.v(Helper.TAG, "response: " + response); Log.v(Helper.TAG, "response: " + response);
ecdh.saveServerKey(context, account, pushSubscription.getServer_key());
} catch (HttpsConnection.HttpsConnectionException e) { } catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e); setError(e.getStatusCode(), e);
e.printStackTrace(); e.printStackTrace();

View File

@ -116,7 +116,7 @@ public class ECDH {
return new KeyPair(readPublicKey(pubKeyStr), readPrivateKey(privKeyStr)); return new KeyPair(readPublicKey(pubKeyStr), readPrivateKey(privKeyStr));
} }
KeyPair newPair(Context context, Account account) { KeyPair newPair(Context context) {
SharedPreferences.Editor prefsEditor = PreferenceManager SharedPreferences.Editor prefsEditor = PreferenceManager
.getDefaultSharedPreferences(context).edit(); .getDefaultSharedPreferences(context).edit();
@ -129,21 +129,21 @@ public class ECDH {
return null; return null;
} }
prefsEditor.putString(kp_public + account.getId() + account.getInstance(), base64Encode(kp.getPublic().getEncoded())); prefsEditor.putString(kp_public, base64Encode(kp.getPublic().getEncoded()));
prefsEditor.putString(kp_private + account.getId() + account.getInstance(), base64Encode(kp.getPrivate().getEncoded())); prefsEditor.putString(kp_private, base64Encode(kp.getPrivate().getEncoded()));
prefsEditor.commit(); prefsEditor.commit();
return kp; return kp;
} }
synchronized KeyPair getPair(Context context, Account account) { synchronized KeyPair getPair(Context context) {
SharedPreferences prefs = PreferenceManager SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context); .getDefaultSharedPreferences(context);
String strPub = prefs.getString(kp_public + account.getId() + account.getInstance(), ""); String strPub = prefs.getString(kp_public, "");
String strPriv = prefs.getString(kp_private + account.getId() + account.getInstance(), ""); String strPriv = prefs.getString(kp_private, "");
if (strPub.isEmpty() || strPriv.isEmpty()) { if (strPub.isEmpty() || strPriv.isEmpty()) {
return newPair(context, account); return newPair(context);
} }
try { try {
return readKeyPair(strPub, strPriv); return readKeyPair(strPub, strPriv);
@ -153,11 +153,11 @@ public class ECDH {
return null; return null;
} }
public String getPublicKey(Context context, Account account) { public String getPublicKey(Context context) {
return base64Encode(getPair(context, account).getPublic().getEncoded()); 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 SharedPreferences.Editor prefsEditor = PreferenceManager
.getDefaultSharedPreferences(context).edit(); .getDefaultSharedPreferences(context).edit();
@ -174,7 +174,7 @@ public class ECDH {
byte[] getSecret(Context context, Account account) { byte[] getSecret(Context context, Account account) {
try { try {
return generateSecret(getPair(context, account).getPrivate(), getServerKey(context, account)); return generateSecret(getPair(context).getPrivate(), getServerKey(context, account));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;

View File

@ -1,7 +1,10 @@
package app.fedilab.android.helper; package app.fedilab.android.helper;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import org.unifiedpush.android.connector.Registration; import org.unifiedpush.android.connector.Registration;
@ -9,19 +12,26 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import app.fedilab.android.activities.BaseMainActivity; 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.client.Entities.Account;
import app.fedilab.android.interfaces.OnPostSubscription;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
public class PushNotifications { import static android.content.Context.MODE_PRIVATE;
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);
//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<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts();
for (Account account : accountPush) {
new PostSubscriptionAsyncTask(context, account, endpoint, this);
}
} }
public static void getDistributors(Context context) { public static void getDistributors(Context context) {
@ -39,7 +49,10 @@ public class PushNotifications {
//process with the event //process with the event
// https://openacs.org/webpush-demo/report.html // https://openacs.org/webpush-demo/report.html
// decrypt using AES 128 GCM // decrypt using AES 128 GCM
}
@Override
public void onSubscription(APIResponse apiResponse) {
//TODO je ne sais pas si c'est toujours utile
} }
} }

View File

@ -25,26 +25,21 @@ import app.fedilab.android.sqlite.Sqlite;
import static android.content.Context.MODE_PRIVATE; import static android.content.Context.MODE_PRIVATE;
class handler implements MessagingReceiverHandler, OnPostSubscription { class handler implements MessagingReceiverHandler {
private Context context; private Context context;
private String endpoint; private String endpoint;
@Override @Override
public void onNewEndpoint(@Nullable Context context, @NotNull String s) { public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint) {
Log.v(Helper.TAG, "onNewEndpoint: " + s); Log.v(Helper.TAG, "onNewEndpoint: " + endpoint);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_ENDPOINT, s); editor.putString(Helper.SERVER_ENDPOINT, endpoint);
editor.apply(); editor.apply();
endpoint = s;
this.context = context; new PushNotifications()
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); .registerPushNotifications(context, endpoint);
List<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts();
for (Account account : accountPush) {
new PostSubscriptionAsyncTask(context, account, s, this);
}
} }
@Override @Override
@ -66,23 +61,10 @@ class handler implements MessagingReceiverHandler, OnPostSubscription {
} }
@Override @Override
public void onMessage(@Nullable Context context, @NotNull String s) { public void onMessage(@Nullable Context context, @NotNull String message) {
PushNotifications push = new PushNotifications(); Log.v(Helper.TAG, "Message: " + message);
Log.v(Helper.TAG, "Message: " + s); new PushNotifications()
push.displayNotification(context, null, s); .displayNotification(context, null, message);
}
@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());
}
} }
} }