Search in sources :

Example 1 with PushConfigurationState

use of com.nextcloud.talk.models.json.push.PushConfigurationState in project talk-android by nextcloud.

the class PushUtils method verifySignature.

public SignatureVerification verifySignature(byte[] signatureBytes, byte[] subjectBytes) {
    Signature signature = null;
    PushConfigurationState pushConfigurationState;
    PublicKey publicKey;
    SignatureVerification signatureVerification = new SignatureVerification();
    signatureVerification.setSignatureValid(false);
    List<UserEntity> userEntities = userUtils.getUsers();
    try {
        signature = Signature.getInstance("SHA512withRSA");
        if (userEntities != null && userEntities.size() > 0) {
            for (UserEntity userEntity : userEntities) {
                if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
                    pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(), PushConfigurationState.class);
                    publicKey = (PublicKey) readKeyFromString(true, pushConfigurationState.getUserPublicKey());
                    signature.initVerify(publicKey);
                    signature.update(subjectBytes);
                    if (signature.verify(signatureBytes)) {
                        signatureVerification.setSignatureValid(true);
                        signatureVerification.setUserEntity(userEntity);
                        return signatureVerification;
                    }
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "No such algorithm");
    } catch (IOException e) {
        Log.d(TAG, "Error while trying to parse push configuration state");
    } catch (InvalidKeyException e) {
        Log.d(TAG, "Invalid key while trying to verify");
    } catch (SignatureException e) {
        Log.d(TAG, "Signature exception while trying to verify");
    }
    return signatureVerification;
}
Also used : PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) PublicKey(java.security.PublicKey) Signature(java.security.Signature) SignatureVerification(com.nextcloud.talk.models.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) UserEntity(com.nextcloud.talk.models.database.UserEntity)

Example 2 with PushConfigurationState

use of com.nextcloud.talk.models.json.push.PushConfigurationState in project talk-android by nextcloud.

the class AccountRemovalJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
    PushConfigurationState pushConfigurationState;
    for (Object userEntityObject : userUtils.getUsersScheduledForDeletion()) {
        UserEntity userEntity = (UserEntity) userEntityObject;
        try {
            if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
                pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(), PushConfigurationState.class);
                PushConfigurationState finalPushConfigurationState = pushConfigurationState;
                ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
                ncApi.unregisterDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl())).subscribe(new Observer<GenericOverall>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(GenericOverall genericOverall) {
                        if (genericOverall.getOcs().getMeta().getStatusCode() == 200 || genericOverall.getOcs().getMeta().getStatusCode() == 202) {
                            HashMap<String, String> queryMap = new HashMap<>();
                            queryMap.put("deviceIdentifier", finalPushConfigurationState.deviceIdentifier);
                            queryMap.put("userPublicKey", finalPushConfigurationState.getUserPublicKey());
                            queryMap.put("deviceIdentifierSignature", finalPushConfigurationState.getDeviceIdentifierSignature());
                            ncApi.unregisterDeviceForNotificationsWithProxy(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlPushProxy(), queryMap).subscribe(new Observer<Void>() {

                                @Override
                                public void onSubscribe(Disposable d) {
                                }

                                @Override
                                public void onNext(Void aVoid) {
                                    userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                                        @Override
                                        public void onSubscribe(Disposable d) {
                                        }

                                        @Override
                                        public void onComplete() {
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                        }
                                    });
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

                                @Override
                                public void onComplete() {
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
            } else {
                userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                });
            }
        } catch (IOException e) {
            Log.d(TAG, "Something went wrong while removing job at parsing PushConfigurationState");
            userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                @Override
                public void onSubscribe(Disposable d) {
                }

                @Override
                public void onComplete() {
                }

                @Override
                public void onError(Throwable e) {
                }
            });
        }
    }
    return Result.SUCCESS;
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) IOException(java.io.IOException) UserEntity(com.nextcloud.talk.models.database.UserEntity) PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) CompletableObserver(io.reactivex.CompletableObserver) Observer(io.reactivex.Observer) CompletableObserver(io.reactivex.CompletableObserver) NcApi(com.nextcloud.talk.api.NcApi) GenericOverall(com.nextcloud.talk.models.json.generic.GenericOverall) CookieManager(java.net.CookieManager) NonNull(android.support.annotation.NonNull)

Example 3 with PushConfigurationState

use of com.nextcloud.talk.models.json.push.PushConfigurationState in project talk-android by nextcloud.

the class PushUtils method pushRegistrationToServer.

public void pushRegistrationToServer() {
    String token = appPreferences.getPushToken();
    if (!TextUtils.isEmpty(token)) {
        String pushTokenHash = generateSHA512Hash(token).toLowerCase();
        PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
        if (devicePublicKey != null) {
            byte[] publicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
            String publicKey = new String(publicKeyBytes);
            publicKey = publicKey.replaceAll("(.{64})", "$1\n");
            publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----\n";
            if (userUtils.anyUserExists()) {
                String providerValue;
                PushConfigurationState accountPushData = null;
                for (Object userEntityObject : userUtils.getUsers()) {
                    UserEntity userEntity = (UserEntity) userEntityObject;
                    providerValue = userEntity.getPushConfigurationState();
                    if (!TextUtils.isEmpty(providerValue)) {
                        try {
                            accountPushData = LoganSquare.parse(providerValue, PushConfigurationState.class);
                        } catch (IOException e) {
                            Log.d(TAG, "Failed to parse account push data");
                            accountPushData = null;
                        }
                    } else {
                        accountPushData = null;
                    }
                    if (accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion() || TextUtils.isEmpty(providerValue) && !userEntity.getScheduledForDeletion()) {
                        Map<String, String> queryMap = new HashMap<>();
                        queryMap.put("format", "json");
                        queryMap.put("pushTokenHash", pushTokenHash);
                        queryMap.put("devicePublicKey", publicKey);
                        queryMap.put("proxyServer", proxyServer);
                        ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
                        ncApi.registerDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), queryMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<PushRegistrationOverall>() {

                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(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());
                                ncApi.registerDeviceForNotificationsWithProxy(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlPushProxy(), proxyMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<Void>() {

                                    @Override
                                    public void onSubscribe(Disposable d) {
                                    }

                                    @Override
                                    public void onNext(Void aVoid) {
                                        PushConfigurationState pushConfigurationState = new PushConfigurationState();
                                        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 {
                                            userUtils.createOrUpdateUser(null, null, null, userEntity.getDisplayName(), LoganSquare.serialize(pushConfigurationState), null, null, userEntity.getId(), null).subscribe(new Observer<UserEntity>() {

                                                @Override
                                                public void onSubscribe(Disposable d) {
                                                }

                                                @Override
                                                public void onNext(UserEntity userEntity) {
                                                }

                                                @Override
                                                public void onError(Throwable e) {
                                                }

                                                @Override
                                                public void onComplete() {
                                                }
                                            });
                                        } catch (IOException e) {
                                            Log.e(TAG, "IOException while updating user");
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                    }

                                    @Override
                                    public void onComplete() {
                                    }
                                });
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) IOException(java.io.IOException) UserEntity(com.nextcloud.talk.models.database.UserEntity) PushRegistrationOverall(com.nextcloud.talk.models.json.push.PushRegistrationOverall) PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) Observer(io.reactivex.Observer) NcApi(com.nextcloud.talk.api.NcApi) HashMap(java.util.HashMap) Map(java.util.Map) CookieManager(java.net.CookieManager)

Aggregations

UserEntity (com.nextcloud.talk.models.database.UserEntity)3 PushConfigurationState (com.nextcloud.talk.models.json.push.PushConfigurationState)3 IOException (java.io.IOException)3 NcApi (com.nextcloud.talk.api.NcApi)2 Observer (io.reactivex.Observer)2 Disposable (io.reactivex.disposables.Disposable)2 CookieManager (java.net.CookieManager)2 PublicKey (java.security.PublicKey)2 HashMap (java.util.HashMap)2 JavaNetCookieJar (okhttp3.JavaNetCookieJar)2 NonNull (android.support.annotation.NonNull)1 SignatureVerification (com.nextcloud.talk.models.SignatureVerification)1 GenericOverall (com.nextcloud.talk.models.json.generic.GenericOverall)1 PushRegistrationOverall (com.nextcloud.talk.models.json.push.PushRegistrationOverall)1 CompletableObserver (io.reactivex.CompletableObserver)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Signature (java.security.Signature)1 SignatureException (java.security.SignatureException)1 Map (java.util.Map)1