Ejemplo n.º 1
0
        private async Task <StringBuilder> GetLitresBookShelfDataAsync()
        {
            try
            {
                if (string.IsNullOrEmpty(CatalogModel.AuthorizationString))
                {
                    throw new CatalogAuthorizationException(CatalogType.Litres, LitresApiConstants.AUTHORIZATION_URL);
                }

                var authorizationString = EncryptService.Decrypt(CatalogModel.AuthorizationString);
                var postParams          = CreatePostParamsForBookShelf(authorizationString);
                var response            = await WebClient.DoPostAsync(LitresApiConstants.BOOK_SHELF_URL, postParams);

                var stream = await response.Content.ReadAsStreamAsync();

                using (var reader = new StreamReader(stream))
                {
                    return(new StringBuilder(reader.ReadToEnd()));
                }
            }
            catch (WebException)
            {
                if (NavigationStack.Any())
                {
                    NavigationStack.Pop();
                }

                throw new ReadCatalogException("Unable read Litres catalog");
            }
        }
Ejemplo n.º 2
0
        public void Encrypt_Fail()
        {
            IEncryptService encryptService = new EncryptService();
            var             result         = encryptService.Encrypt("test");
            var             dec            = encryptService.Decrypt("WrongPassword", result);

            Assert.Equal("Password incorrect", dec);
        }
Ejemplo n.º 3
0
        public void Encrypt_Success()
        {
            IEncryptService encryptService = new EncryptService();
            var             result         = encryptService.Encrypt("test");
            var             dec            = encryptService.Decrypt("test", result);

            Assert.Equal("Password correct", dec);
        }
Ejemplo n.º 4
0
        public void TestDecryptText()
        {
            var encryptService = new EncryptService();
            var encryptedText  = File.ReadAllText(@"../../../TestFiles/Encrypted.txt");
            var decryptedText  = encryptService.Decrypt("скорпион", encryptedText);

            Assert.AreEqual(decryptedText, File.ReadAllText(@"../../../TestFiles/Decrypted.txt"));
        }
Ejemplo n.º 5
0
        public void ResetPassword([FromBody] Object password)
        {
            var            jsonString = password.ToString();
            PasswordString result     = JsonConvert.DeserializeObject <PasswordString>(jsonString);
            HashSalt       hashed     = LoginService.GenerateSaltedHash(10, result.Password);
            string         email      = EncryptService.Decrypt(result.Encrypt, "astrophile");

            UserRepo.UpdatePassword(hashed.Hash, email);
            UserRepo.UpdateSalt(hashed.Salt, email);
        }
Ejemplo n.º 6
0
        private async Task <StringBuilder> GetOpdsDataAsync(string url, IWebClient webClient)
        {
            try
            {
                var    baseUri       = new UriBuilder(url);
                string queryToAppend = "rand=" + Guid.NewGuid();
                baseUri.Query = baseUri.Query.Length > 1 ? baseUri.Query.Substring(1) + "&" + queryToAppend : queryToAppend;

                string authorizationString = null;
                if (!string.IsNullOrEmpty(CatalogModel.AuthorizationString))
                {
                    authorizationString = EncryptService.Decrypt(CatalogModel.AuthorizationString);
                }

                var response = await webClient.DoGetAsync(baseUri.ToString(), authorizationString);

                if (response.StatusCode == HttpStatusCode.Unauthorized ||
                    response.StatusCode == HttpStatusCode.Forbidden)
                {
                    throw new CatalogAuthorizationException(CatalogType.OPDS, url);
                }

                var stream = await response.Content.ReadAsStreamAsync();

                using (var reader = new StreamReader(stream))
                {
                    return(new StringBuilder(reader.ReadToEnd()));
                }
            }
            catch (WebException exception)
            {
                if (exception.Status != WebExceptionStatus.RequestCanceled)
                {
                    if (NavigationStack.Any())
                    {
                        NavigationStack.Pop();
                    }

                    var statusCode = ((HttpWebResponse)exception.Response).StatusCode;
                    if (statusCode == HttpStatusCode.Unauthorized || statusCode == HttpStatusCode.Forbidden)
                    {
                        throw new CatalogAuthorizationException(CatalogModel.Type, url);
                    }

                    throw new ReadCatalogException(string.Format("Unable read catalog {0}", url));
                }
            }
            catch (DataCorruptedException)
            {
                //skip DataCorruptedException
            }

            return(await GetOpdsDataAsync(url, webClient));
        }
Ejemplo n.º 7
0
        public void Decrypt_Text_ShouldReturnSameText()
        {
            Account account = new Account()
            {
                Password = "******",
                Username = "******"
            };
            string encryptedText = EncryptService.Encrypt(account.Username);
            string decryptedText = EncryptService.Decrypt(encryptedText);

            Assert.AreEqual(account.Username, decryptedText);
        }
Ejemplo n.º 8
0
        private void NavigateToWebBrowser(CatalogItemModel model)
        {
            // another hack for improving performance.
            if (model is LitresTopupCatalogItemModel)
            {
                var catalog = CatalogRepository.Get(CatalogId);
                model.HtmlUrl = string.Format(model.HtmlUrl, EncryptService.Decrypt(catalog.AuthorizationString));
            }

            _navigationService.UriFor <WebBrowserPageViewModel>()
            .WithParam(vm => vm.WebBrowserUrl, model.HtmlUrl)
            .WithParam(vm => vm.CatalogId, CatalogId)
            .WithParam(vm => vm.Title, model.Title)
            .Navigate();
        }
Ejemplo n.º 9
0
        private CatalogFolderModel ConvertToLitresFolder(string litresBookShelfData)
        {
            try
            {
                CatalitFb2BooksDto dto;
                using (var stringReader = new StringReader(litresBookShelfData))
                {
                    var xmlSerializer = new XmlSerializer(typeof(CatalitFb2BooksDto));
                    dto = (CatalitFb2BooksDto)xmlSerializer.Deserialize(stringReader);
                }

                return(dto.ToFolder(EncryptService.Decrypt(CatalogModel.AuthorizationString)));
            }
            catch (InvalidOperationException exp)
            {
                throw new ReadCatalogException("Unable convert OPDS data to folder", exp);
            }
        }
Ejemplo n.º 10
0
        public void ForgotPassword(DataInput <ResetPasswordInput> requestDto)
        {
            User user = GetUserContact(requestDto.CurrentUser);

            if (user != null)
            {
                if (requestDto.Dto.Email.Trim().Equals(user.Email.Trim()))
                {
                    var diffInSeconds = Math.Round(user.PasswordLastUdt.HasValue ? (Clock.Now - user.PasswordLastUdt.Value).TotalSeconds : 300);
                    if (diffInSeconds >= 300)
                    {
                        user.PasswordLastUdt = Clock.Now;
                        user.LastUpdateDate  = Clock.Now;
                        user.Password        = GeneratePassword();
                        try
                        {
                            _emailService.SendForgotPassword(user.Email, EncryptService.Decrypt(user.Password), user.FullName, user.UserType.Equals(UserTypeEnum.Employee));
                            Log.Information("Reset Password For User: {Username} Successfully.", user.Username);
                            _unitOfWork.Update(user);
                            _unitOfWork.Commit();
                        }
                        catch (Exception e)
                        {
                            Log.Error(user.FullName + "reset password error {e}", e);
                            throw new DefinedException(ErrorCodeEnum.CannotSendEmailToResetPassword);
                        }
                    }
                    else
                    {
                        throw new DefinedException(ErrorCodeEnum.MultiplePasswordResetting, 300 - diffInSeconds);
                    }
                }
                else
                {
                    Log.Information("Email is incorrect!", ErrorCodeEnum.IncorrectEmail);
                    throw new DefinedException(ErrorCodeEnum.IncorrectEmail);
                }
            }
            else
            {
                Log.Information("User is incorrect!", ErrorCodeEnum.IncorrectUser);
                throw new DefinedException(ErrorCodeEnum.IncorrectUser);
            }
        }
Ejemplo n.º 11
0
 private static Account DecryptAccount(Account account)
 {
     account.Username = EncryptService.Decrypt(account.Username);
     account.Password = EncryptService.Decrypt(account.Password);
     return(account);
 }
Ejemplo n.º 12
0
 private byte[] Decrypt(byte[] source)
 {
     return(EncryptService.Decrypt(source));
 }
        public async Task <string> BuyBook(CatalogBookItemModel book, string authorizationString)
        {
            if (string.IsNullOrEmpty(authorizationString))
            {
                throw new CatalogAuthorizationException(CatalogType.Litres, AUTHORIZATION_URL);
            }

            try
            {
                var art  = string.Empty;
                var uuid = string.Empty;

                if (book.Id.Contains("|"))
                {
                    var idParts = book.Id.Split('|');
                    art  = idParts[0];
                    uuid = idParts[1];
                }
                else
                {
                    art = book.Id;
                }

                var response = await _webClient.DoPostAsync(BUY_BOOK_URL, CreateAcquisitionParams(art, uuid, EncryptService.Decrypt(authorizationString)));

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new ReadCatalogException("Unable to read catalog");
                }

                var responseStream = await response.Content.ReadAsStreamAsync();

                var xDoc = XDocument.Load(responseStream);
                if (xDoc.Root.Name == "catalit-purchase-ok")
                {
                    var format = string.Format("sid={0}&art={1}", EncryptService.Decrypt(authorizationString), art);
                    if (!string.IsNullOrEmpty(uuid))
                    {
                        format = string.Concat(format, string.Format("uuid={0}", uuid));
                    }

                    return(string.Concat(DOWNLOAD_BOOK_URL, format));
                }

                if (xDoc.Root.Name == "catalit-purchase-failed")
                {
                    if (xDoc.Root.Attribute("error").Value == "1")
                    {
                        throw new CatalogNotEnoughMoneyException(CatalogType.Litres, string.Concat(NOT_ENOUGH_MONEY_URL, string.Format("?sid={0}", EncryptService.Decrypt(authorizationString))));
                    }
                    if (xDoc.Root.Attribute("error").Value == "3")
                    {
                        //throw new CatalogBookAlreadyBoughtException(CatalogType.Litres, book.Id);
                        var format = string.Format("sid={0}&art={1}", EncryptService.Decrypt(authorizationString), art);
                        if (!string.IsNullOrEmpty(uuid))
                        {
                            format = string.Concat(format, string.Format("uuid={0}", uuid));
                        }

                        return(string.Concat(DOWNLOAD_BOOK_URL, format));
                    }
                }

                if (xDoc.Root.Name == "catalit-authorization-failed")
                {
                    throw new CatalogAuthorizationException(CatalogType.Litres, AUTHORIZATION_URL);
                }
            }
            catch (WebException)
            {
                throw new ReadCatalogException("Unable to read catalog");
            }

            throw new ReadCatalogException("Unable to read catalog");
        }
Ejemplo n.º 14
0
        public void Encrypt_ThrowException()
        {
            IEncryptService encryptService = new EncryptService();

            Assert.Throws <ArgumentNullException>(() => encryptService.Decrypt(null, "test"));
        }