public override async Task Run() { try { if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.CloudStorageCredentials, out SerializeableCloudStorageCredentials credentials)) { throw new ArgumentNullException(nameof(credentials)); } if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthState, out string oauthState)) { throw new ArgumentNullException(nameof(oauthState)); } if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthCodeVerifier, out string oauthCodeVerifier)) { throw new ArgumentNullException(nameof(oauthState)); } if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthRedirectUrl, out string redirectUrl)) { throw new ArgumentNullException(nameof(redirectUrl)); } StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthState); StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthCodeVerifier); StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthRedirectUrl); ICloudStorageClient cloudStorageClient = _cloudStorageClientFactory.GetOrCreate(credentials.CloudStorageId); if (cloudStorageClient is IOAuth2CloudStorageClient oauthStorageClient) { CloudStorageToken token = await oauthStorageClient.FetchTokenAsync(redirectUrl, oauthState, oauthCodeVerifier); if (token != null) { // User has granted access. credentials.Token = token; await StoryBoard.ContinueWith(SynchronizationStoryStepId.ExistsCloudRepository); } else { // User has rejected access. _feedbackService.ShowToast(_languageService.LoadText("sync_reject")); await StoryBoard.ContinueWith(SynchronizationStoryStepId.StopAndShowRepository); } } } catch (Exception ex) { // Keep the current page open and show the error message ShowExceptionMessage(ex, _feedbackService, _languageService); } }
/// <inheritdoc/> public override async Task Run() { try { SettingsModel settings = _settingsService.LoadSettingsOrDefault(); byte[] binaryCloudRepository = StoryBoard.LoadFromSession <byte[]>(SynchronizationStorySessionKey.BinaryCloudRepository); // Try to decode with all possible transfer codes bool successfullyDecryptedRepository = TryDecryptWithAllTransferCodes( settings, binaryCloudRepository, out byte[] decryptedRepository); if (successfullyDecryptedRepository) { // Deserialize and update repository XDocument cloudRepositoryXml = XmlUtils.LoadFromXmlBytes(decryptedRepository); if (_noteRepositoryUpdater.IsTooNewForThisApp(cloudRepositoryXml)) { throw new SynchronizationStoryBoard.UnsuportedRepositoryRevisionException(); } _noteRepositoryUpdater.Update(cloudRepositoryXml); NoteRepositoryModel cloudRepository = XmlUtils.DeserializeFromXmlDocument <NoteRepositoryModel>(cloudRepositoryXml); // Continue with next step StoryBoard.StoreToSession(SynchronizationStorySessionKey.CloudRepository, cloudRepository); await StoryBoard.ContinueWith(SynchronizationStoryStepId.IsSameRepository); } else { bool existsUserEnteredTransferCode = StoryBoard.TryLoadFromSession <string>(SynchronizationStorySessionKey.UserEnteredTransferCode, out _); if (existsUserEnteredTransferCode) { // Keep transfercode page open and show message _feedbackService.ShowToast(_languageService["sync_error_transfercode"]); } else { // Open transfercode page await StoryBoard.ContinueWith(SynchronizationStoryStepId.ShowTransferCode); } } } catch (Exception ex) { // Keep the current page open and show the error message ShowExceptionMessage(ex, _feedbackService, _languageService); } }
private List <string> ListTransferCodesToTry(SettingsModel settings) { var result = new List <string>(); bool existsUserEnteredTransferCode = StoryBoard.TryLoadFromSession <string>(SynchronizationStorySessionKey.UserEnteredTransferCode, out string userEnteredTransferCode); if (existsUserEnteredTransferCode) { result.Add(userEnteredTransferCode); } else { if (!string.IsNullOrEmpty(settings.TransferCode)) { result.Add(settings.TransferCode); } result.AddRange(settings.TransferCodeHistory); } return(result); }
/// <inheritdoc/> public override async Task Run() { SerializeableCloudStorageCredentials credentials = StoryBoard.LoadFromSession <SerializeableCloudStorageCredentials>(SynchronizationStorySessionKey.CloudStorageCredentials.ToInt()); ICloudStorageClient cloudStorageClient = _cloudStorageClientFactory.GetOrCreate(credentials.CloudStorageId); try { // The repository can be cached for this story, download the repository only once. byte[] binaryCloudRepository; if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.BinaryCloudRepository.ToInt(), out binaryCloudRepository)) { binaryCloudRepository = await cloudStorageClient.DownloadFileAsync(Config.RepositoryFileName, credentials); StoryBoard.StoreToSession(SynchronizationStorySessionKey.BinaryCloudRepository.ToInt(), binaryCloudRepository); } await StoryBoard.ContinueWith(SynchronizationStoryStepId.ExistsTransferCode.ToInt()); } catch (Exception ex) { // Keep the current page open and show the error message ShowExceptionMessage(ex, _feedbackService, _languageService); } }
/// <inheritdoc/> public override async Task Run() { try { SettingsModel settings = _settingsService.LoadSettingsOrDefault(); byte[] binaryCloudRepository = StoryBoard.LoadFromSession <byte[]>(SynchronizationStorySessionKey.BinaryCloudRepository.ToInt()); List <string> transferCodesToTry = ListTransferCodesToTry(settings); // Try to decode with all possible transfer codes EncryptorDecryptor encryptor = new EncryptorDecryptor("SilentNotes"); byte[] decryptedRepository = null; bool successfullyDecryptedRepository = false; int index = 0; while (!successfullyDecryptedRepository && index < transferCodesToTry.Count) { string transferCodeCandidate = transferCodesToTry[index]; successfullyDecryptedRepository = TryDecryptRepositoryWithTransfercode(encryptor, binaryCloudRepository, transferCodeCandidate, out decryptedRepository); if (successfullyDecryptedRepository) { // Store transfercode and encryption mode if necessary if (AdoptTransferCode(settings, transferCodeCandidate) || AdoptEncryptionMode(settings, encryptor, binaryCloudRepository)) { _settingsService.TrySaveSettingsToLocalDevice(settings); } } index++; } if (successfullyDecryptedRepository) { // Deserialize and update repository XDocument cloudRepositoryXml = XmlUtils.LoadFromXmlBytes(decryptedRepository); if (_noteRepositoryUpdater.IsTooNewForThisApp(cloudRepositoryXml)) { throw new SynchronizationStoryBoard.UnsuportedRepositoryRevisionException(); } _noteRepositoryUpdater.Update(cloudRepositoryXml); NoteRepositoryModel cloudRepository = XmlUtils.DeserializeFromXmlDocument <NoteRepositoryModel>(cloudRepositoryXml); // Continue with next step StoryBoard.StoreToSession(SynchronizationStorySessionKey.CloudRepository.ToInt(), cloudRepository); await StoryBoard.ContinueWith(SynchronizationStoryStepId.IsSameRepository.ToInt()); } else { bool existsUserEnteredTransferCode = StoryBoard.TryLoadFromSession <string>(SynchronizationStorySessionKey.UserEnteredTransferCode.ToInt(), out _); if (existsUserEnteredTransferCode) { // Keep transfercode page open and show message _feedbackService.ShowToast(_languageService["sync_error_transfercode"]); } else { // Open transfercode page await StoryBoard.ContinueWith(SynchronizationStoryStepId.ShowTransferCode.ToInt()); } } } catch (Exception ex) { // Keep the current page open and show the error message ShowExceptionMessage(ex, _feedbackService, _languageService); } }