Fix issue with sharedpref
This commit is contained in:
parent
9d40901631
commit
7ed9398009
|
@ -47,7 +47,7 @@ public class PostSubscriptionAsyncTask {
|
|||
|
||||
protected void doInBackground() {
|
||||
new Thread(() -> {
|
||||
apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(account, endpoint);
|
||||
apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(endpoint, account);
|
||||
List<Account> accountList = new ArrayList<>();
|
||||
accountList.add(account);
|
||||
apiResponse.setAccounts(accountList);
|
||||
|
|
|
@ -23,6 +23,7 @@ import android.os.Bundle;
|
|||
import android.text.Html;
|
||||
import android.text.SpannableString;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
|
@ -5785,7 +5786,7 @@ public class API {
|
|||
*
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse pushSubscription(Account account, String endpoint) {
|
||||
public APIResponse pushSubscription(String endpoint, Account account) {
|
||||
PushSubscription pushSubscription = new PushSubscription();
|
||||
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
||||
|
@ -5805,15 +5806,16 @@ public class API {
|
|||
params.put("subscription[endpoint]", endpoint);
|
||||
|
||||
ECDH ecdh = ECDH.getInstance();
|
||||
String pubKey = ecdh.getPublicKey(context);
|
||||
String pubKey = ecdh.getPublicKey(context, account);
|
||||
byte[] randBytes = new byte[16];
|
||||
new Random().nextBytes(randBytes);
|
||||
String auth = Base64.encodeToString(randBytes, Base64.DEFAULT);
|
||||
params.put("subscription[keys][p256dh]", pubKey);
|
||||
params.put("subscription[keys][auth]", auth);
|
||||
try {
|
||||
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/push/subscription"), 10, params, prefKeyOauthTokenT);
|
||||
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);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -24,6 +24,8 @@ import java.security.spec.X509EncodedKeySpec;
|
|||
|
||||
import javax.crypto.KeyAgreement;
|
||||
|
||||
import app.fedilab.android.client.Entities.Account;
|
||||
|
||||
// https://github.com/nelenkov/ecdh-kx/blob/master/src/org/nick/ecdhkx/Crypto.java
|
||||
|
||||
public class ECDH {
|
||||
|
@ -114,7 +116,7 @@ public class ECDH {
|
|||
return new KeyPair(readPublicKey(pubKeyStr), readPrivateKey(privKeyStr));
|
||||
}
|
||||
|
||||
KeyPair newPair(Context context) {
|
||||
KeyPair newPair(Context context, Account account) {
|
||||
SharedPreferences.Editor prefsEditor = PreferenceManager
|
||||
.getDefaultSharedPreferences(context).edit();
|
||||
|
||||
|
@ -127,21 +129,21 @@ public class ECDH {
|
|||
return null;
|
||||
}
|
||||
|
||||
prefsEditor.putString(kp_public, base64Encode(kp.getPublic().getEncoded()));
|
||||
prefsEditor.putString(kp_private, base64Encode(kp.getPrivate().getEncoded()));
|
||||
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.commit();
|
||||
return kp;
|
||||
}
|
||||
|
||||
synchronized KeyPair getPair(Context context) {
|
||||
synchronized KeyPair getPair(Context context, Account account) {
|
||||
SharedPreferences prefs = PreferenceManager
|
||||
.getDefaultSharedPreferences(context);
|
||||
|
||||
String strPub = prefs.getString(kp_public, "");
|
||||
String strPriv = prefs.getString(kp_private, "");
|
||||
String strPub = prefs.getString(kp_public + account.getId() + account.getInstance(), "");
|
||||
String strPriv = prefs.getString(kp_private + account.getId() + account.getInstance(), "");
|
||||
|
||||
if (strPub.isEmpty() || strPriv.isEmpty()) {
|
||||
return newPair(context);
|
||||
return newPair(context, account);
|
||||
}
|
||||
try {
|
||||
return readKeyPair(strPub, strPriv);
|
||||
|
@ -151,8 +153,8 @@ public class ECDH {
|
|||
return null;
|
||||
}
|
||||
|
||||
public String getPublicKey(Context context) {
|
||||
return base64Encode(getPair(context).getPublic().getEncoded());
|
||||
public String getPublicKey(Context context, Account account) {
|
||||
return base64Encode(getPair(context, account).getPublic().getEncoded());
|
||||
}
|
||||
|
||||
void saveServerKey(Context context, String strPeerPublic) {
|
||||
|
@ -170,9 +172,9 @@ public class ECDH {
|
|||
);
|
||||
}
|
||||
|
||||
byte[] getSecret(Context context) {
|
||||
byte[] getSecret(Context context, Account account) {
|
||||
try {
|
||||
return generateSecret(getPair(context).getPrivate(), getServerKey(context));
|
||||
return generateSecret(getPair(context, account).getPrivate(), getServerKey(context));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
|
|
|
@ -9,19 +9,18 @@ import java.util.List;
|
|||
import java.util.Random;
|
||||
|
||||
import app.fedilab.android.activities.BaseMainActivity;
|
||||
import app.fedilab.android.client.Entities.Account;
|
||||
|
||||
public class PushNotifications {
|
||||
public void registerPushNotifications(Context context, String endpoint) {
|
||||
public void registerPushNotifications(Context context, Account account, String endpoint, String server_key) {
|
||||
ECDH ecdh = new ECDH();
|
||||
String pubKey = ecdh.getPublicKey(context);
|
||||
String pubKey = ecdh.getPublicKey(context, account);
|
||||
byte[] randBytes = new byte[16];
|
||||
new Random().nextBytes(randBytes);
|
||||
String auth = Base64.encodeToString(randBytes, Base64.DEFAULT);
|
||||
|
||||
//register
|
||||
|
||||
String server_key = ""; //fetch in the json
|
||||
|
||||
ecdh.saveServerKey(context, server_key);
|
||||
}
|
||||
|
||||
|
@ -34,9 +33,9 @@ public class PushNotifications {
|
|||
}
|
||||
}
|
||||
|
||||
public void displayNotification(Context context, String ciphered) {
|
||||
public void displayNotification(Context context, Account account, String ciphered) {
|
||||
ECDH ecdh = new ECDH();
|
||||
byte[] secret = ecdh.getSecret(context);
|
||||
byte[] secret = ecdh.getSecret(context, account);
|
||||
//process with the event
|
||||
// https://openacs.org/webpush-demo/report.html
|
||||
// decrypt using AES 128 GCM
|
||||
|
|
|
@ -28,6 +28,7 @@ import static android.content.Context.MODE_PRIVATE;
|
|||
class handler implements MessagingReceiverHandler, OnPostSubscription {
|
||||
|
||||
private Context context;
|
||||
private String endpoint;
|
||||
|
||||
@Override
|
||||
public void onNewEndpoint(@Nullable Context context, @NotNull String s) {
|
||||
|
@ -36,9 +37,8 @@ class handler implements MessagingReceiverHandler, OnPostSubscription {
|
|||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.SERVER_ENDPOINT, s);
|
||||
editor.apply();
|
||||
endpoint = s;
|
||||
|
||||
PushNotifications push = new PushNotifications();
|
||||
push.registerPushNotifications(context, s);
|
||||
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();
|
||||
|
@ -68,8 +68,8 @@ class handler implements MessagingReceiverHandler, OnPostSubscription {
|
|||
@Override
|
||||
public void onMessage(@Nullable Context context, @NotNull String s) {
|
||||
PushNotifications push = new PushNotifications();
|
||||
push.displayNotification(context, s);
|
||||
Log.v(Helper.TAG, "onMessage: " + s);
|
||||
Log.v(Helper.TAG, "Message: " + s);
|
||||
push.displayNotification(context, null, s);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -77,8 +77,11 @@ class handler implements MessagingReceiverHandler, OnPostSubscription {
|
|||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue