some improvements

This commit is contained in:
Thomas 2021-02-26 19:03:36 +01:00
parent 5215576c8f
commit f39ec92bcf
4 changed files with 15 additions and 32 deletions

View File

@ -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<String, String> 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);

View File

@ -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 {

View File

@ -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<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts();
List<byte[]> 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

View File

@ -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);
}
}