public static AuthenticationFormSur From(IAuthenticationForm form) { if (form == null) { return(null); } var formAsclassic = form as AuthenticationFormClassic; if (formAsclassic != null) { return((AuthenticationFormClassicSur)formAsclassic); } //return new AuthenticationFormClassicSur() //{ // AuthenticationMethod = formAsclassic.AuthenticationMethod, // CryptoProvider = formAsclassic.CryptoProvider, // Login = formAsclassic.Login, // HashOfPassword = formAsclassic.HashOfPassword, // HashAlgotitm = formAsclassic.HashAlgotitm //}; var formAsSign = form as AuthenticationFormSign; if (formAsSign != null) { return((AuthenticationFormSignSur)formAsSign); } //return new AuthenticationFormSignSur() //{ // AuthenticationMethod = formAsSign.AuthenticationMethod, // CryptoProvider = formAsSign.CryptoProvider, // Login = formAsSign.Login, // Sign = formAsSign.Sign, // Hash = formAsSign.Hash, // SignantureAlgoritmName = formAsSign.SignantureAlgoritmName //}; return(new AuthenticationFormSur() { AuthenticationMethod = form.AuthenticationMethod, CryptoProvider = form.CryptoProvider, Login = form.Login }); }
public Presenter(IMainForm v1, IAuthenticationForm v3, IProjectForm v2, IModel s) { _mainView = v1; _projectView = v2; _storage = s; _authenticationForm = v3; _mainView.MainFormInitialized += () => initIssuesIssues(); _mainView.CloseMainView += () => StopThread(); _mainView.ShowProjects += () => ShowProjectsView(); _mainView.NewIssue += () => ShowNewIssueView(); _mainView.showJournals += () => OpenJournalsForm(_mainView.getSelectedIssueID()); _mainView.ApplyFilter += () => SendFilterQuery(_mainView.getFilter()); _mainView.IssueUpdate += () => OpenUpdateIssueView(_mainView.getSelectedIssueID()); _mainView.NewProject += () => ShowNewProjectView(); _projectView.ShowProjects += () => ShowProj(); _projectView.ShowDetailsView += () => OpenProjDetailsForm(_projectView.getSelectedProjID()); _authenticationForm.checkAuthentication += () => checkAuthentication(_authenticationForm.getLogin(), _authenticationForm.getPassword()); _storage.AuthenticationPassed += () => RunMV(); }
//public abstract UserForm GetPublicKey(); /// <exception cref="ArgumentNullException">form == null. -или- resultOfOperation == null.</exception> public abstract Task <Tuple <UserFormSurrogate, OfflineMessagesConcurent> > AuthenticateUserAsync( IAuthenticationForm formAuth, CryptoFactoryBase cryptoFactory, CryptoInfo choosenCrypto, ResultOfOperation resultOfOperation);
//public override UserForm GetPublicKey() //{ // var formNew = new UserForm(); // if (CryptoProvider == CryptoProvider.CngMicrosoft) // { // if (AsymmetricAlgorithm is RSACng) // { // var rsa = (RSACng) AsymmetricAlgorithm; // //var cngKeyBlob = rsa.Key.Export(CngKeyBlobFormat.GenericPublicBlob); // var key = rsa.ExportParameters(false); // var cngKeyBlob = Serializer.Serialize(key, false); // formNew = new UserForm() // { // CryptoProvider = CryptoProvider.CngMicrosoft, // PublicKeyParamsBlob = cngKeyBlob // }; // } // } // return formNew; //} ///// <exception cref="ArgumentNullException">form == null. -или- resultOfOperation == null.</exception> //public override UserForm AuthenticateUser(UserForm form, out IEnumerable<byte[]> messages, // ResultOfOperation resultOfOperation) //{ // if (form == null) // throw new ArgumentNullException(nameof(form)) { Source = GetType().AssemblyQualifiedName }; // if (resultOfOperation == null) // throw new ArgumentNullException(nameof(resultOfOperation)) { Source = GetType().AssemblyQualifiedName }; // messages = new byte[0][]; // var formWasFinded = UsersF.FirstOrDefault((registrationForm => registrationForm.Login.Equals(form.Login))); // var KeyBlob = formWasFinded.SignPublicKeyBlob; // if (KeyBlob == null) // { // resultOfOperation.ErrorMessage = "Пользователь не зарегистрирован."; // resultOfOperation.OperationWasFinishedSuccessful = false; // return null; // } // form.SignPublicKeyBlob = KeyBlob; // if (!form.ValidateSign(false, resultOfOperation)) // return null; // if (OfflineMessagesF.ContainsKey(form.Login)) // messages = OfflineMessagesF[form.Login].MessagesAsBytes.ToArray(); // return form; //} /// <exception cref="ArgumentNullException">form == null. -или- resultOfOperation == null.</exception> public override async Task <Tuple <UserFormSurrogate, OfflineMessagesConcurent> > AuthenticateUserAsync(IAuthenticationForm formAuth, CryptoFactoryBase cryptoFactory, CryptoInfo choosenCrypto, ResultOfOperation resultOfOperation) { if (formAuth == null) { throw new ArgumentNullException(nameof(formAuth)) { Source = GetType().AssemblyQualifiedName } } ; if (resultOfOperation == null) { throw new ArgumentNullException(nameof(resultOfOperation)) { Source = GetType().AssemblyQualifiedName } } ; //messages = null; try { //var formWasFinded = UsersF.First(registrationForm => registrationForm.Login.Equals(formAuth.Login)); var formWasFinded = await UsersF.Users.FirstAsync(registrationForm => registrationForm.Login == formAuth.Login) .ConfigureAwait(false); switch (formAuth.AuthenticationMethod) { case AuthenticationMethod.Classic: var formClassicAuth = formAuth as IAuthenticationFormClassic; if (formClassicAuth == null) { throw new InvalidCastException("Преобразование из типа IAuthenticationForm в тип " + "IAuthenticationFormClassic завершилось с ошибкой.") { Source = GetType().AssemblyQualifiedName } } ; var hashAlg = cryptoFactory.CreateHashAlgorithm(choosenCrypto.Provider, formClassicAuth.HashAlgotitm); var hashOfTruePass = hashAlg.ComputeHash(Encoding.UTF8.GetBytes(formWasFinded.Password)); if (hashOfTruePass.SequenceEqual(formClassicAuth.HashOfPassword)) { resultOfOperation.OperationWasFinishedSuccessful = true; } else { resultOfOperation.ErrorMessage = "Правильность пароля под сомнением?"; resultOfOperation.OperationWasFinishedSuccessful = false; } break; case AuthenticationMethod.Sign: var formSignAuth = formAuth as IAuthenticationFormSign; if (formSignAuth == null) { throw new InvalidCastException("Преобразование из типа IAuthenticationForm в тип " + "IAuthenticationFormSign завершилось с ошибкой.") { Source = GetType().AssemblyQualifiedName } } ; var signAlg = cryptoFactory.CreateSignAlgoritm(formSignAuth.CryptoProvider, formSignAuth.SignantureAlgoritmName); //var signAlg = cryptoFactory.CreateSignAlgoritm(choosenCrypto.Provider, formSignAuth.SignantureAlgoritmName); signAlg.Import(formWasFinded.KeyParametrsBlob.Key); if (signAlg.VerifySign(formSignAuth.Hash, formSignAuth.Sign)) { resultOfOperation.OperationWasFinishedSuccessful = true; } else { resultOfOperation.ErrorMessage = "Достоверность цифровой подписи под сомнением?"; resultOfOperation.OperationWasFinishedSuccessful = false; } break; } OfflineMessagesConcurent messages; OfflineMessagesF.TryRemove(formWasFinded.Login, out messages); if (messages == null) { messages = new OfflineMessagesConcurent(formWasFinded.Login); } return(new Tuple <UserFormSurrogate, OfflineMessagesConcurent>(formWasFinded, messages)); } catch (InvalidOperationException ex) { CreateResultWithError(3, resultOfOperation, formAuth.Login); return(null); } }