Search in sources :

Example 1 with PushConfigurationState

use of com.owncloud.android.datamodel.PushConfigurationState in project android by nextcloud.

the class PushUtils method deleteRegistrationForAccount.

private static void deleteRegistrationForAccount(Account account) {
    Context context = MainApp.getAppContext();
    OwnCloudAccount ocAccount;
    arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
    try {
        ocAccount = new OwnCloudAccount(account, context);
        OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
        RemoteOperation unregisterAccountDeviceForNotificationsOperation = new UnregisterAccountDeviceForNotificationsOperation();
        RemoteOperationResult remoteOperationResult = unregisterAccountDeviceForNotificationsOperation.execute(mClient);
        if (remoteOperationResult.getHttpCode() == HttpStatus.SC_ACCEPTED) {
            String arbitraryValue;
            if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH))) {
                Gson gson = new Gson();
                PushConfigurationState pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                RemoteOperationResult unregisterResult = new UnregisterAccountDeviceForProxyOperation(context.getResources().getString(R.string.push_server_url), pushArbitraryData.getDeviceIdentifier(), pushArbitraryData.getDeviceIdentifierSignature(), pushArbitraryData.getUserPublicKey()).run();
                if (unregisterResult.isSuccess()) {
                    arbitraryDataProvider.deleteKeyForAccount(account.name, KEY_PUSH);
                }
            }
        }
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        Log_OC.d(TAG, "Failed to find an account");
    } catch (AuthenticatorException e) {
        Log_OC.d(TAG, "Failed via AuthenticatorException");
    } catch (IOException e) {
        Log_OC.d(TAG, "Failed via IOException");
    } catch (OperationCanceledException e) {
        Log_OC.d(TAG, "Failed via OperationCanceledException");
    }
}
Also used : Context(android.content.Context) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UnregisterAccountDeviceForProxyOperation(com.owncloud.android.lib.resources.notifications.UnregisterAccountDeviceForProxyOperation) OperationCanceledException(android.accounts.OperationCanceledException) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) AuthenticatorException(android.accounts.AuthenticatorException) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) UnregisterAccountDeviceForNotificationsOperation(com.owncloud.android.lib.resources.notifications.UnregisterAccountDeviceForNotificationsOperation) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient)

Example 2 with PushConfigurationState

use of com.owncloud.android.datamodel.PushConfigurationState in project android by nextcloud.

the class PushUtils method pushRegistrationToServer.

public static void pushRegistrationToServer() {
    String token = PreferenceManager.getPushToken(MainApp.getAppContext());
    arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
    if (!TextUtils.isEmpty(MainApp.getAppContext().getResources().getString(R.string.push_server_url)) && !TextUtils.isEmpty(token)) {
        PushUtils.generateRsa2048KeyPair();
        String pushTokenHash = PushUtils.generateSHA512Hash(token).toLowerCase(Locale.ROOT);
        PublicKey devicePublicKey = (PublicKey) PushUtils.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";
            Context context = MainApp.getAppContext();
            String providerValue;
            PushConfigurationState accountPushData = null;
            Gson gson = new Gson();
            for (Account account : AccountUtils.getAccounts(context)) {
                providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH);
                if (!TextUtils.isEmpty(providerValue)) {
                    accountPushData = gson.fromJson(providerValue, PushConfigurationState.class);
                } else {
                    accountPushData = null;
                }
                if (accountPushData != null && !accountPushData.getPushToken().equals(token) && !accountPushData.isShouldBeDeleted() || TextUtils.isEmpty(providerValue)) {
                    try {
                        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
                        OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
                        RemoteOperation registerAccountDeviceForNotificationsOperation = new RegisterAccountDeviceForNotificationsOperation(pushTokenHash, publicKey, context.getResources().getString(R.string.push_server_url));
                        RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.execute(mClient);
                        if (remoteOperationResult.isSuccess()) {
                            PushResponse pushResponse = remoteOperationResult.getPushResponseData();
                            RemoteOperation registerAccountDeviceForProxyOperation = new RegisterAccountDeviceForProxyOperation(context.getResources().getString(R.string.push_server_url), token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(), pushResponse.getPublicKey());
                            remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
                            if (remoteOperationResult.isSuccess()) {
                                PushConfigurationState pushArbitraryData = new PushConfigurationState(token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(), pushResponse.getPublicKey(), false);
                                arbitraryDataProvider.storeOrUpdateKeyValue(account.name, KEY_PUSH, gson.toJson(pushArbitraryData));
                            }
                        } else if (remoteOperationResult.getCode() == RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD) {
                            arbitraryDataProvider.storeOrUpdateKeyValue(account.name, AccountUtils.ACCOUNT_USES_STANDARD_PASSWORD, "true");
                        }
                    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
                        Log_OC.d(TAG, "Failed to find an account");
                    } catch (AuthenticatorException e) {
                        Log_OC.d(TAG, "Failed via AuthenticatorException");
                    } catch (IOException e) {
                        Log_OC.d(TAG, "Failed via IOException");
                    } catch (OperationCanceledException e) {
                        Log_OC.d(TAG, "Failed via OperationCanceledException");
                    }
                } else if (accountPushData != null && accountPushData.isShouldBeDeleted()) {
                    deleteRegistrationForAccount(account);
                }
            }
        }
    }
}
Also used : Context(android.content.Context) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) PublicKey(java.security.PublicKey) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCanceledException(android.accounts.OperationCanceledException) RegisterAccountDeviceForNotificationsOperation(com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation) AccountUtils(com.owncloud.android.authentication.AccountUtils) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) AuthenticatorException(android.accounts.AuthenticatorException) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) RegisterAccountDeviceForProxyOperation(com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForProxyOperation) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) PushResponse(com.owncloud.android.lib.resources.notifications.models.PushResponse)

Example 3 with PushConfigurationState

use of com.owncloud.android.datamodel.PushConfigurationState in project android by nextcloud.

the class PushUtils method verifySignature.

public SignatureVerification verifySignature(Context context, byte[] signatureBytes, byte[] subjectBytes) {
    Signature signature = null;
    PublicKey publicKey;
    SignatureVerification signatureVerification = new SignatureVerification();
    signatureVerification.setSignatureValid(false);
    Account[] accounts = AccountUtils.getAccounts(context);
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String arbitraryValue;
    Gson gson = new Gson();
    PushConfigurationState pushArbitraryData;
    try {
        signature = Signature.getInstance("SHA512withRSA");
        if (accounts.length > 0) {
            for (Account account : accounts) {
                if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account, KEY_PUSH))) {
                    pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                    if (!pushArbitraryData.isShouldBeDeleted()) {
                        publicKey = (PublicKey) readKeyFromString(true, pushArbitraryData.getUserPublicKey());
                        signature.initVerify(publicKey);
                        signature.update(subjectBytes);
                        if (signature.verify(signatureBytes)) {
                            signatureVerification.setSignatureValid(true);
                            signatureVerification.setAccount(account);
                            return signatureVerification;
                        }
                    }
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "No such algorithm");
    } 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 : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) PublicKey(java.security.PublicKey) Signature(java.security.Signature) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) SignatureVerification(com.owncloud.android.datamodel.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 4 with PushConfigurationState

use of com.owncloud.android.datamodel.PushConfigurationState in project android by nextcloud.

the class PushUtils method verifySignature.

public static SignatureVerification verifySignature(final Context context, final UserAccountManager accountManager, final byte[] signatureBytes, final byte[] subjectBytes) {
    Signature signature;
    PublicKey publicKey;
    Account[] accounts = accountManager.getAccounts();
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
    String arbitraryValue;
    Gson gson = new Gson();
    PushConfigurationState pushArbitraryData;
    try {
        signature = Signature.getInstance("SHA512withRSA");
        if (accounts.length > 0) {
            for (Account account : accounts) {
                if (!TextUtils.isEmpty(arbitraryValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH))) {
                    pushArbitraryData = gson.fromJson(arbitraryValue, PushConfigurationState.class);
                    if (!pushArbitraryData.isShouldBeDeleted()) {
                        publicKey = (PublicKey) readKeyFromString(true, pushArbitraryData.getUserPublicKey());
                        signature.initVerify(publicKey);
                        signature.update(subjectBytes);
                        if (signature.verify(signatureBytes)) {
                            return new SignatureVerification(true, account);
                        }
                    }
                }
            }
        }
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "No such algorithm");
    } 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 new SignatureVerification(false, null);
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) PublicKey(java.security.PublicKey) Signature(java.security.Signature) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) SignatureVerification(com.owncloud.android.datamodel.SignatureVerification) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with PushConfigurationState

use of com.owncloud.android.datamodel.PushConfigurationState in project android by nextcloud.

the class PushUtils method pushRegistrationToServer.

public static void pushRegistrationToServer(final UserAccountManager accountManager, final String token) {
    arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
    if (!TextUtils.isEmpty(MainApp.getAppContext().getResources().getString(R.string.push_server_url)) && !TextUtils.isEmpty(token)) {
        PushUtils.generateRsa2048KeyPair();
        String pushTokenHash = PushUtils.generateSHA512Hash(token).toLowerCase(Locale.ROOT);
        PublicKey devicePublicKey = (PublicKey) PushUtils.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";
            Context context = MainApp.getAppContext();
            String providerValue;
            PushConfigurationState accountPushData;
            Gson gson = new Gson();
            for (Account account : accountManager.getAccounts()) {
                providerValue = arbitraryDataProvider.getValue(account.name, KEY_PUSH);
                if (!TextUtils.isEmpty(providerValue)) {
                    accountPushData = gson.fromJson(providerValue, PushConfigurationState.class);
                } else {
                    accountPushData = null;
                }
                if (accountPushData != null && !accountPushData.getPushToken().equals(token) && !accountPushData.isShouldBeDeleted() || TextUtils.isEmpty(providerValue)) {
                    try {
                        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
                        OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context);
                        RemoteOperationResult remoteOperationResult = new RegisterAccountDeviceForNotificationsOperation(pushTokenHash, publicKey, context.getResources().getString(R.string.push_server_url)).execute(client);
                        if (remoteOperationResult.isSuccess()) {
                            PushResponse pushResponse = remoteOperationResult.getPushResponseData();
                            RemoteOperationResult resultProxy = new RegisterAccountDeviceForProxyOperation(context.getResources().getString(R.string.push_server_url), token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(), pushResponse.getPublicKey(), MainApp.getUserAgent()).run();
                            if (resultProxy.isSuccess()) {
                                PushConfigurationState pushArbitraryData = new PushConfigurationState(token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(), pushResponse.getPublicKey(), false);
                                arbitraryDataProvider.storeOrUpdateKeyValue(account.name, KEY_PUSH, gson.toJson(pushArbitraryData));
                            }
                        } else if (remoteOperationResult.getCode() == RemoteOperationResult.ResultCode.ACCOUNT_USES_STANDARD_PASSWORD) {
                            arbitraryDataProvider.storeOrUpdateKeyValue(account.name, UserAccountManager.ACCOUNT_USES_STANDARD_PASSWORD, "true");
                        }
                    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
                        Log_OC.d(TAG, "Failed to find an account");
                    } catch (AuthenticatorException e) {
                        Log_OC.d(TAG, "Failed via AuthenticatorException");
                    } catch (IOException e) {
                        Log_OC.d(TAG, "Failed via IOException");
                    } catch (OperationCanceledException e) {
                        Log_OC.d(TAG, "Failed via OperationCanceledException");
                    }
                } else if (accountPushData != null && accountPushData.isShouldBeDeleted()) {
                    deleteRegistrationForAccount(account);
                }
            }
        }
    }
}
Also used : Context(android.content.Context) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) PublicKey(java.security.PublicKey) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCanceledException(android.accounts.OperationCanceledException) RegisterAccountDeviceForNotificationsOperation(com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) Gson(com.google.gson.Gson) AuthenticatorException(android.accounts.AuthenticatorException) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) RegisterAccountDeviceForProxyOperation(com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForProxyOperation) PushConfigurationState(com.owncloud.android.datamodel.PushConfigurationState) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) PushResponse(com.owncloud.android.lib.resources.notifications.models.PushResponse)

Aggregations

Gson (com.google.gson.Gson)5 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)5 PushConfigurationState (com.owncloud.android.datamodel.PushConfigurationState)5 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)5 Account (android.accounts.Account)4 PublicKey (java.security.PublicKey)4 AuthenticatorException (android.accounts.AuthenticatorException)3 OperationCanceledException (android.accounts.OperationCanceledException)3 Context (android.content.Context)3 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)3 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)3 IOException (java.io.IOException)3 SignatureVerification (com.owncloud.android.datamodel.SignatureVerification)2 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)2 RegisterAccountDeviceForNotificationsOperation (com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation)2 RegisterAccountDeviceForProxyOperation (com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForProxyOperation)2 PushResponse (com.owncloud.android.lib.resources.notifications.models.PushResponse)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Signature (java.security.Signature)2