public async Task <SignInResult> SignIn(string login, string password) { User user; if (login.IsEmailAddress()) { user = await database.UserRepository.FindUserByEmail(login) ?? throw new InvalidCredentialsException("Invalid login or password"); } else { user = await database.UserRepository.FindUserByUsername(login) ?? throw new InvalidCredentialsException("Invalid login or password"); } if (!user.IsActivated) { throw new AccountNotConfirmedException("Account has been not activated"); } if (!hashGenerator.VerifyHash(password, user.PasswordSalt, user.PasswordHash)) { throw new InvalidCredentialsException("Invalid login or password"); } var token = jwtAuthorizationTokenGenerator.GenerateToken(user); return(new SignInResult { JwtToken = token, User = user }); }
public async Task <PurchaseOrderResponse> Handle(PurchaseOrderRequest request, CancellationToken cancellationToken) { if (!await balanceValidationService.HasEnoughFunds(httpContextReader.CurrentUserId, request.TotalAmount)) { throw new PaymentException("Insufficient funds to complete transaction"); } var order = await orderService.PurchaseOrder(request); var orderOfferItems = order.Items.Where(i => i.Type == OrderType.Offer); var offersData = orderOfferItems.Select(i => new { OwnerId = i.OptionalData, Amount = (decimal)i.Amount / Constants.MoneyMultiplier }); foreach (var item in orderOfferItems) { var notification = await notifier.Push(NotificationMessages.OfferBoughtMessage(order.User.UserName, item.ProductName), item.OptionalData); await hubManager.Invoke(SignalrActions.NOTIFICATION_RECEIVED, item.OptionalData, mapper.Map <NotificationDto>(notification)); } var premiumOrder = order.Items.FirstOrDefault(i => i.Type == OrderType.Premium); if (premiumOrder != null && !await rolesManager.AdmitRole(RoleName.Premium, order.User)) { throw new PaymentException("Upgrading account to premium status failed"); } if (order != null) { foreach (var offerData in offersData) { await balanceService.AddBalance(offerData.OwnerId, offerData.Amount); await balanceService.AddBalance(order.UserId, -offerData.Amount); } string token = default(string); if (premiumOrder != null) { await balanceService.AddBalance(order.UserId, -(decimal)premiumOrder.Amount / Constants.MoneyMultiplier); token = await jwtAuthorizationTokenGenerator.GenerateToken(order.User); } return(new PurchaseOrderResponse { Order = mapper.Map <OrderDto>(order), Token = token }); } throw new PaymentException("Purchasing order failed"); }
public async Task <RefreshUserDataResponse> Handle(RefreshUserDataRequest request, CancellationToken cancellationToken) { var currentUser = await profileService.GetCurrentUser(); var token = await jwtAuthorizationTokenGenerator.GenerateToken(currentUser); if (currentUser != null && !string.IsNullOrEmpty(token)) { return new RefreshUserDataResponse { Token = token, User = mapper.Map <UserAuthDto>(currentUser) } } ; throw new AuthException("Some error occurred during signing in"); } }
public async Task <AuthResult> SignIn(string email, string password) { var user = await database.UserRepository.Find(u => u.Email.ToLower() == email.ToLower()) ?? throw new InvalidCredentialsException("Invalid email or password"); if (!UserConfirmedSpecification.Create().IsSatisfied(user)) { throw new AccountNotConfirmedException("Account has not been activated"); } if (UserBlockedSpecification.Create().IsSatisfied(user)) { throw new BlockException("Your account is blocked"); } if (hashGenerator.VerifyHash(password, user.PasswordHash, user.PasswordSalt)) { var token = await jwtAuthorizationTokenGenerator.GenerateToken(user); return(new AuthResult(token, user)); } throw new InvalidCredentialsException("Invalid email or password"); }
public async Task <IdentityResult> SignIn(string email, string password) { var user = await userManager.FindByEmailAsync(email) ?? throw new InvalidCredentialsException("Invalid email address or password"); if (UserIsExternalSpecification.Create().IsSatisfied(user)) { throw new InvalidCredentialsException("Invalid email address or password"); } if (!UserConfirmedSpecification.Create().IsSatisfied(user)) { throw new AccountNotConfirmedException("Account is not confirmed"); } if (UserBlockedSpecification.Create().IsSatisfied(user)) { throw new BlockException(); } return((await signInManager.CheckPasswordSignInAsync(user, password, false)).Succeeded ? new IdentityResult(await jwtAuthorizationTokenGenerator.GenerateToken(user), user) : throw new InvalidCredentialsException("Invalid email address or password")); }
protected async Task <IdentityResult> AddUserLogin(string provider, string providerKey, string email, string username = null, string pictureUrl = null) { var userLoginInfo = new UserLoginInfo(provider, providerKey, provider); var user = await userManager.FindByLoginAsync(userLoginInfo.LoginProvider, userLoginInfo.ProviderKey) ?? await userManager.FindByEmailAsync(email); if (user == null) { user = User.Create(email, username ?? email); await userManager.CreateAsync(user); } await userManager.AddLoginAsync(user, userLoginInfo); if (user == null) { throw new ExternalAuthException("Invalid external authentication"); } await rolesManager.AdmitRole(RoleName.ExternalUser, user); if (!user.IsRegistered()) { user.SetAvatarUrl(pictureUrl); await unitOfWork.Complete(); } if (UserBlockedSpecification.Create().IsSatisfied(user)) { throw new BlockException(); } var token = await jwtAuthorizationTokenGenerator.GenerateToken(user); return(new IdentityResult(token, user)); }