/// <summary> /// Writes the current accounts into the datafile /// </summary> public async void WriteLocalDatafile() { var dbHash = await App.Repository.Password.GetAsync(); var datafileDB = await App.Repository.Datafile.GetAsync(); var iv = new AesManaged().IV; await CollectionAccessSemaphore.WaitAsync(); DatafileModel file = new DatafileModel() { IV = iv, Collection = Collection }; var folder = await StorageFolder.GetFolderFromPathAsync(datafileDB.Path); await FileService.WriteStringAsync( datafileDB.Name, NewtonsoftJSONService.SerializeEncrypt(SecretService.Helper.ReadSecret(Constants.ContainerName, dbHash.Hash), iv, file), folder); CollectionAccessSemaphore.Release(); }
/// <summary> /// Checks if the password is correct or not and displays an error message /// </summary> /// <returns>boolean</returns> public async Task <bool> TestPassword() { Windows.Storage.StorageFile file = await LocalStorageFolder.GetFileAsync(DateFileName); if (file != null) { string datafileStr = await FileService.ReadStringAsync(DateFileName, LocalStorageFolder); //read the iv for AES DatafileModel datafile = NewtonsoftJSONService.Deserialize <DatafileModel>(datafileStr); var iv = datafile.IV; try { var deserializeCollection = NewtonsoftJSONService.DeserializeDecrypt <DatafileModel> (Password, iv, datafileStr); return(true); } catch (Exception) { ShowError = true; Password = string.Empty; return(false); } } else { //TODO add error, no file found? } return(false); }
/// <summary> /// Constructor /// </summary> public NewDatafileContentDialogViewModel() { SerializationService = App.Current.Container.Resolve <ISerializationService>(); FileService = App.Current.Container.Resolve <IFileService>(); #pragma warning disable AsyncFixer03 // Fire-and-forget async-void methods or delegates PrimaryButtonCommand = new DelegateCommand(async() => { Password = Password.Replace(" ", string.Empty); await CreateLocalFileDB(false); byte[] iv = new AesManaged().IV; DatafileModel file = new DatafileModel() { IV = iv, Collection = new System.Collections.ObjectModel.ObservableCollection <TwoFACodeModel>() }; await FileService.WriteStringAsync(DateFileName, SerializationService.Serialize(file), await StorageFolder.GetFolderFromPathAsync(LocalStorageFolder.Path)); }); #pragma warning restore AsyncFixer03 // Fire-and-forget async-void methods or delegates CheckServerAddressCommand = new DelegateCommand(() => { CheckServerStatus(); }); ConfirmErrorCommand = new DelegateCommand(() => { ShowError = false; }); LoginCommand = new DelegateCommand(async() => { CheckLoginAsync(); }); ChangePathCommand = new DelegateCommand(async() => { SetLocalPath(true); //change path is true }); ChooseWebDAVCommand = new DelegateCommand(() => { }); }
/// <summary> /// Checks and reads the current local datafile /// </summary> /// <param name="dbDatafile"></param> private async void CheckLocalDatafile(DBDatafileModel dbDatafile) { try { ObservableCollection <TwoFACodeModel> deserializeCollection = new ObservableCollection <TwoFACodeModel>(); StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(dbDatafile.Path); if (await FileService.FileExistsAsync(dbDatafile.Name, folder)) { var dbHash = await App.Repository.Password.GetAsync(); // prevent write of the datafile _initialization = true; try { string datafileStr = await FileService.ReadStringAsync(dbDatafile.Name, folder); if (!string.IsNullOrEmpty(datafileStr)) { // read the iv for AES DatafileModel datafile = NewtonsoftJSONService.Deserialize <DatafileModel>(datafileStr); var iv = datafile.IV; datafile = NewtonsoftJSONService.DeserializeDecrypt <DatafileModel>( SecretService.Helper.ReadSecret(Constants.ContainerName, dbHash.Hash), iv, datafileStr); deserializeCollection = datafile.Collection; } } catch (Exception) { ShowPasswordError(); } } // file not found case else { ShowFileOrFolderNotFoundError(); } if (deserializeCollection != null) { Collection.AddRange(deserializeCollection); if (Collection.Count == 0) { // if no error has occured if (!_errorOccurred) { EmptyAccountCollectionTipIsOpen = true; } } else { if (EmptyAccountCollectionTipIsOpen) { EmptyAccountCollectionTipIsOpen = false; } } } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception exc) #pragma warning restore CA1031 // Do not catch general exception types { if (exc is UnauthorizedAccessException) { ShowUnauthorizedAccessError(); } else if (exc is FileNotFoundException) { ShowFileOrFolderNotFoundError(); } else { _errorOccurred = true; TrackingManager.TrackException(exc); ErrorDialogs.ShowUnexpectedError(exc); } } // writing the data file is activated again _initialization = false; }