mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-19 19:49:33 +01:00
refactoring PushUtils
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
a7c298b224
commit
412af46ed5
@ -97,11 +97,11 @@ public class PushUtils {
|
|||||||
keysFile = NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeyStore", Context.MODE_PRIVATE);
|
keysFile = NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeyStore", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
publicKeyFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeystore",
|
publicKeyFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeystore",
|
||||||
Context.MODE_PRIVATE), "push_key.pub");
|
Context.MODE_PRIVATE), "push_key.pub");
|
||||||
privateKeyFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeystore",
|
privateKeyFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("PushKeystore",
|
||||||
Context.MODE_PRIVATE), "push_key.priv");
|
Context.MODE_PRIVATE), "push_key.priv");
|
||||||
proxyServer = NextcloudTalkApplication.Companion.getSharedApplication().getResources().
|
proxyServer = NextcloudTalkApplication.Companion.getSharedApplication().getResources().
|
||||||
getString(R.string.nc_push_server_url);
|
getString(R.string.nc_push_server_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SignatureVerification verifySignature(byte[] signatureBytes, byte[] subjectBytes) {
|
public SignatureVerification verifySignature(byte[] signatureBytes, byte[] subjectBytes) {
|
||||||
@ -118,9 +118,9 @@ public class PushUtils {
|
|||||||
for (UserEntity userEntity : userEntities) {
|
for (UserEntity userEntity : userEntities) {
|
||||||
if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
|
if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
|
||||||
pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(),
|
pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(),
|
||||||
PushConfigurationState.class);
|
PushConfigurationState.class);
|
||||||
publicKey = (PublicKey) readKeyFromString(true,
|
publicKey = (PublicKey) readKeyFromString(true,
|
||||||
pushConfigurationState.getUserPublicKey());
|
pushConfigurationState.getUserPublicKey());
|
||||||
signature.initVerify(publicKey);
|
signature.initVerify(publicKey);
|
||||||
signature.update(subjectBytes);
|
signature.update(subjectBytes);
|
||||||
if (signature.verify(signatureBytes)) {
|
if (signature.verify(signatureBytes)) {
|
||||||
@ -183,7 +183,7 @@ public class PushUtils {
|
|||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
for (byte individualByte : bytes) {
|
for (byte individualByte : bytes) {
|
||||||
result.append(Integer.toString((individualByte & 0xff) + 0x100, 16)
|
result.append(Integer.toString((individualByte & 0xff) + 0x100, 16)
|
||||||
.substring(1));
|
.substring(1));
|
||||||
}
|
}
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
@ -230,11 +230,11 @@ public class PushUtils {
|
|||||||
String pushTokenHash = generateSHA512Hash(token).toLowerCase();
|
String pushTokenHash = generateSHA512Hash(token).toLowerCase();
|
||||||
PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
|
PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
|
||||||
if (devicePublicKey != null) {
|
if (devicePublicKey != null) {
|
||||||
byte[] publicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
|
byte[] devicePublicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
|
||||||
String publicKey = new String(publicKeyBytes);
|
String devicePublicKeyBase64 = new String(devicePublicKeyBytes);
|
||||||
publicKey = publicKey.replaceAll("(.{64})", "$1\n");
|
devicePublicKeyBase64 = devicePublicKeyBase64.replaceAll("(.{64})", "$1\n");
|
||||||
|
|
||||||
publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----\n";
|
devicePublicKeyBase64 = "-----BEGIN PUBLIC KEY-----\n" + devicePublicKeyBase64 + "\n-----END PUBLIC KEY-----\n";
|
||||||
|
|
||||||
if (userUtils.anyUserExists()) {
|
if (userUtils.anyUserExists()) {
|
||||||
String providerValue;
|
String providerValue;
|
||||||
@ -253,51 +253,17 @@ public class PushUtils {
|
|||||||
accountPushData = null;
|
accountPushData = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (((TextUtils.isEmpty(providerValue) || accountPushData == null) && !userEntity.getScheduledForDeletion()) ||
|
if (((TextUtils.isEmpty(providerValue) || accountPushData == null)
|
||||||
(accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion())) {
|
&& !userEntity.getScheduledForDeletion()) ||
|
||||||
|
(accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion())) {
|
||||||
|
|
||||||
Map<String, String> queryMap = new HashMap<>();
|
Map<String, String> nextcloudRegisterPushMap = new HashMap<>();
|
||||||
queryMap.put("format", "json");
|
nextcloudRegisterPushMap.put("format", "json");
|
||||||
queryMap.put("pushTokenHash", pushTokenHash);
|
nextcloudRegisterPushMap.put("pushTokenHash", pushTokenHash);
|
||||||
queryMap.put("devicePublicKey", publicKey);
|
nextcloudRegisterPushMap.put("devicePublicKey", devicePublicKeyBase64);
|
||||||
queryMap.put("proxyServer", proxyServer);
|
nextcloudRegisterPushMap.put("proxyServer", proxyServer);
|
||||||
|
|
||||||
credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken());
|
registerDeviceWithNextcloud(nextcloudRegisterPushMap, token, userEntity);
|
||||||
|
|
||||||
ncApi.registerDeviceForNotificationsWithNextcloud(
|
|
||||||
credentials,
|
|
||||||
ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), queryMap)
|
|
||||||
.subscribe(new Observer<PushRegistrationOverall>() {
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(@NonNull Disposable d) {
|
|
||||||
// unused atm
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNext(@NonNull PushRegistrationOverall pushRegistrationOverall) {
|
|
||||||
Map<String, String> proxyMap = new HashMap<>();
|
|
||||||
proxyMap.put("pushToken", token);
|
|
||||||
proxyMap.put("deviceIdentifier", pushRegistrationOverall.getOcs().getData().
|
|
||||||
getDeviceIdentifier());
|
|
||||||
proxyMap.put("deviceIdentifierSignature", pushRegistrationOverall.getOcs()
|
|
||||||
.getData().getSignature());
|
|
||||||
proxyMap.put("userPublicKey", pushRegistrationOverall.getOcs()
|
|
||||||
.getData().getPublicKey());
|
|
||||||
|
|
||||||
registerDeviceWithPushProxy(pushRegistrationOverall, proxyMap, token, userEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(@NonNull Throwable e) {
|
|
||||||
eventBus.post(new EventStatus(userEntity.getId(),
|
|
||||||
EventStatus.EventType.PUSH_REGISTRATION, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete() {
|
|
||||||
// unused atm
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -307,91 +273,125 @@ public class PushUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerDeviceWithPushProxy(
|
private void registerDeviceWithNextcloud(Map<String, String> nextcloudRegisterPushMap, String token, UserEntity userEntity) {
|
||||||
@androidx.annotation.NonNull PushRegistrationOverall pushRegistrationOverall,
|
String credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken());
|
||||||
Map<String, String> proxyMap,
|
|
||||||
String token,
|
ncApi.registerDeviceForNotificationsWithNextcloud(
|
||||||
UserEntity userEntity) {
|
credentials,
|
||||||
ncApi.registerDeviceForNotificationsWithPushProxy(
|
ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), nextcloudRegisterPushMap)
|
||||||
ApiUtils.getUrlPushProxy(), proxyMap)
|
.subscribe(new Observer<PushRegistrationOverall>() {
|
||||||
.subscribeOn(Schedulers.io())
|
@Override
|
||||||
.subscribe(new Observer<Void>() {
|
public void onSubscribe(@NonNull Disposable d) {
|
||||||
@Override
|
// unused atm
|
||||||
public void onSubscribe(@NonNull Disposable d) {
|
}
|
||||||
// unused atm
|
|
||||||
|
@Override
|
||||||
|
public void onNext(@NonNull PushRegistrationOverall pushRegistrationOverall) {
|
||||||
|
Map<String, String> proxyMap = new HashMap<>();
|
||||||
|
proxyMap.put("pushToken", token);
|
||||||
|
proxyMap.put("deviceIdentifier", pushRegistrationOverall.getOcs().getData().
|
||||||
|
getDeviceIdentifier());
|
||||||
|
proxyMap.put("deviceIdentifierSignature", pushRegistrationOverall.getOcs()
|
||||||
|
.getData().getSignature());
|
||||||
|
proxyMap.put("userPublicKey", pushRegistrationOverall.getOcs()
|
||||||
|
.getData().getPublicKey());
|
||||||
|
|
||||||
|
registerDeviceWithPushProxy(proxyMap, userEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(@NonNull Throwable e) {
|
||||||
|
eventBus.post(new EventStatus(userEntity.getId(),
|
||||||
|
EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
// unused atm
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerDeviceWithPushProxy(Map<String, String> proxyMap, UserEntity userEntity) {
|
||||||
|
ncApi.registerDeviceForNotificationsWithPushProxy(ApiUtils.getUrlPushProxy(), proxyMap)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.subscribe(new Observer<Void>() {
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(@NonNull Disposable d) {
|
||||||
|
// unused atm
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(@NonNull Void aVoid) {
|
||||||
|
try {
|
||||||
|
createOrUpdateUser(proxyMap, userEntity);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "IOException while updating user", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(@NonNull Void aVoid) {
|
public void onError(@NonNull Throwable e) {
|
||||||
PushConfigurationState pushConfigurationState =
|
eventBus.post(new EventStatus(userEntity.getId(),
|
||||||
new PushConfigurationState();
|
EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||||
pushConfigurationState.setPushToken(token);
|
}
|
||||||
pushConfigurationState.setDeviceIdentifier(
|
|
||||||
pushRegistrationOverall.getOcs()
|
|
||||||
.getData().getDeviceIdentifier());
|
|
||||||
pushConfigurationState.setDeviceIdentifierSignature(
|
|
||||||
pushRegistrationOverall
|
|
||||||
.getOcs().getData().getSignature());
|
|
||||||
pushConfigurationState.setUserPublicKey(
|
|
||||||
pushRegistrationOverall.getOcs()
|
|
||||||
.getData().getPublicKey());
|
|
||||||
pushConfigurationState.setUsesRegularPass(false);
|
|
||||||
|
|
||||||
try {
|
@Override
|
||||||
userUtils.createOrUpdateUser(null,
|
public void onComplete() {
|
||||||
null, null,
|
// unused atm
|
||||||
userEntity.getDisplayName(),
|
}
|
||||||
LoganSquare.serialize(pushConfigurationState), null,
|
});
|
||||||
null, userEntity.getId(), null, null, null)
|
}
|
||||||
.subscribe(new Observer<UserEntity>() {
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(@NonNull Disposable d) {
|
|
||||||
// unused atm
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private void createOrUpdateUser(Map<String, String> proxyMap, UserEntity userEntity) throws IOException {
|
||||||
public void onNext(@NonNull UserEntity userEntity) {
|
PushConfigurationState pushConfigurationState = new PushConfigurationState();
|
||||||
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, true));
|
pushConfigurationState.setPushToken(proxyMap.get("pushToken"));
|
||||||
}
|
pushConfigurationState.setDeviceIdentifier(proxyMap.get("deviceIdentifier"));
|
||||||
|
pushConfigurationState.setDeviceIdentifierSignature(proxyMap.get("deviceIdentifierSignature"));
|
||||||
|
pushConfigurationState.setUserPublicKey(proxyMap.get("userPublicKey"));
|
||||||
|
pushConfigurationState.setUsesRegularPass(false);
|
||||||
|
|
||||||
@Override
|
userUtils.createOrUpdateUser(null,
|
||||||
public void onError(@NonNull Throwable e) {
|
null,
|
||||||
eventBus.post(new EventStatus
|
null,
|
||||||
(userEntity.getId(),
|
userEntity.getDisplayName(),
|
||||||
EventStatus.EventType
|
LoganSquare.serialize(pushConfigurationState),
|
||||||
.PUSH_REGISTRATION, false));
|
null,
|
||||||
}
|
null,
|
||||||
|
userEntity.getId(),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null)
|
||||||
|
.subscribe(new Observer<UserEntity>() {
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(@NonNull Disposable d) {
|
||||||
|
// unused atm
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onNext(@NonNull UserEntity userEntity) {
|
||||||
// unused atm
|
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, true));
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "IOException while updating user", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(@NonNull Throwable e) {
|
public void onError(@NonNull Throwable e) {
|
||||||
eventBus.post(new EventStatus(userEntity.getId(),
|
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, false));
|
||||||
EventStatus.EventType.PUSH_REGISTRATION, false));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onComplete() {
|
||||||
// unused atm
|
// unused atm
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private Key readKeyFromString(boolean readPublicKey, String keyString) {
|
private Key readKeyFromString(boolean readPublicKey, String keyString) {
|
||||||
if (readPublicKey) {
|
if (readPublicKey) {
|
||||||
keyString = keyString.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----",
|
keyString = keyString.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----",
|
||||||
"").replace("-----END PUBLIC KEY-----", "");
|
"").replace("-----END PUBLIC KEY-----", "");
|
||||||
} else {
|
} else {
|
||||||
keyString = keyString.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----",
|
keyString = keyString.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----",
|
||||||
"").replace("-----END PRIVATE KEY-----", "");
|
"").replace("-----END PRIVATE KEY-----", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyFactory keyFactory = null;
|
KeyFactory keyFactory = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user