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