Exemplo n.º 1
0
        public override async Task <ConfirmEmailReply> HandleRequest(
            AuthorizedRequestObject <ConfirmEmailRequest, ConfirmEmailReply> request,
            CancellationToken cancellationToken = default)
        {
            if (request == null)
            {
                throw new ArgumentNullException($"{nameof(request)} is null");
            }

            var requestData = request.Value;
            var userId      = request.UserId;

            // throw exception if not found
            Account account = await Context.Accounts.FindAsync(userId).ConfigureAwait(false);

            // throw exception if invalid
            EmailVerifies codeValidation = GetValidCode(requestData, userId);

            // this code executed only in valid state
            account.Email = codeValidation.Email;

            var emails = Context.EmailVerifies.OutOfDated(requestData.Code, requestData.Email, userId);

            Context.EmailVerifies.RemoveRange(emails);

            await Context.SaveChangesAsync().ConfigureAwait(false);

            ConfirmEmailReply confirmEmailReply = new ConfirmEmailReply
            {
                Email = codeValidation.Email
            };

            return(confirmEmailReply);
        }
Exemplo n.º 2
0
        public override async Task <EmailChangeReply> HandleRequest(
            AuthorizedRequestObject <EmailChangeRequest, EmailChangeReply> request,
            CancellationToken cancellationToken = default)
        {
            // TODO: add time change email timelock
            EmailChangeRequest requestValue = request.Value;
            string             email        = requestValue.Email;

            // check if email aldeary linked
            var linkedEmail = Context.Accounts.Any(a => a.Email == email && a.Role > RoleType.Unverified);

            if (linkedEmail)
            {
                request.StatusCode = StatusCode.AlreadyExists;
                request.Detail     = $"Email address '{ email }' is already linked to another account";
                return(new EmailChangeReply());
            }

            ApproveEmailTemplate approveEmailTemplate = new ApproveEmailTemplate();

            try
            {
                await emailTemplateSender.SendMailAsync(email, approveEmailTemplate).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                request.StatusCode = StatusCode.InvalidArgument;
                request.Detail     = $"The mail server could not send the email {email} to the address and threw an error {e.Message}";
                return(new EmailChangeReply());
            }

            // add code
            EmailVerifies emailVerifies = new EmailVerifies()
            {
                Code     = approveEmailTemplate.Code,
                SentTime = DateTime.UtcNow,
                Attempts = 0,
                UserId   = request.UserId,
                Guid     = Guid.Parse(approveEmailTemplate.Link),
                Email    = email
            };

            Context.EmailVerifies.Add(emailVerifies);
            await Context.SaveChangesAsync().ConfigureAwait(false);


            EmailChangeReply confirmReply = new EmailChangeReply
            {
                Email = email
            };

            return(confirmReply);
        }
        private bool HasCode(int userId, string email, int code)
        {
            EmailVerifies emailVerifier = emailVerifierRepository
                                          .GetWhere(e => IsRequestedEmailVerifier(e, userId, email)).FirstOrDefault();

            if (emailVerifier == null)
            {
                throw new ArgumentNullException();
            }

            return(emailVerifier.Equals(userId, email, code));
        }
 private async Task ProccedMessageVerification(int userId, string email, bool isCorrect)
 {
     if (isCorrect)
     {
         await emailVerifierRepository.Delete(e => IsRequestedEmailVerifier(e, userId, email));
     }
     else
     {
         EmailVerifies emailVerifier = emailVerifierRepository
                                       .GetWhere(e => IsRequestedEmailVerifier(e, userId, email)).FirstOrDefault();
         emailVerifier.Attempts++;
         await emailVerifierRepository.Update(emailVerifier);
     }
 }
Exemplo n.º 5
0
        public int GetTimeout(int userId)
        {
            EmailVerifies emailVerifier = emailVerifierRepository.GetWhere(e => e.UserId.Equals(userId))
                                          .OrderByDescending(x => x.SentTime)
                                          .FirstOrDefault();

            if (emailVerifier == null)
            {
                return(0);
            }

            TimeSpan currentSpan = emailVerifier.SentTime + TimeSpan.FromMinutes(EMAIL_TIMEOUT) - DateTime.UtcNow;
            int      seconds     = Convert.ToInt32(currentSpan.TotalSeconds);

            return(seconds);
        }
Exemplo n.º 6
0
        public async Task <bool> HasCorrectLink(string link)
        {
            // if link is not guid this is broken link so return
            if (!Guid.TryParse(link, out Guid guid))
            {
                return(false);
            }

            EmailVerifies emailVerifier = await emailVerifierRepository.FindById(guid);

            bool isCorrect = emailVerifier != null;

            if (isCorrect)
            {
                await RemoveStorageVerification(emailVerifier);

                await accountRepository.SetEmail(emailVerifier.UserId, emailVerifier.Email);
            }

            return(isCorrect);
        }
Exemplo n.º 7
0
        public override async Task <ConfirmEmailReply> HandleRequest(
            RequestObject <ConfirmEmailLinkRequest, ConfirmEmailReply> request,
            CancellationToken cancellationToken = default)
        {
            var link = request.Value.ActivationGuid;

            if (!Guid.TryParse(link, out Guid guid))
            {
                request.StatusCode = StatusCode.InvalidArgument;
                request.Detail     = Strings.GetDetailMessage(Locale.RuRu, DetailMessage.LinkInvalid);
                return(new ConfirmEmailReply());
            }

            EmailVerifies linkedEmail = await Context.EmailVerifies.FindAsync(guid);

            if (linkedEmail == null)
            {
                request.StatusCode = StatusCode.NotFound;
                request.Detail     = Strings.GetDetailMessage(Locale.RuRu, DetailMessage.EmailconfirmationNotfound);
                return(new ConfirmEmailReply());
            }

            // set new email to account
            Account account = await Context.Accounts.FindAsync(linkedEmail.UserId).ConfigureAwait(false);

            account.Email = linkedEmail.Email;

            // delete out of dated
            var outdatedVerifies = Context.EmailVerifies.OutOfDated(guid);

            Context.RemoveRange(outdatedVerifies);

            // save changes
            await Context.SaveChangesAsync().ConfigureAwait(false);

            return(new ConfirmEmailReply()
            {
                Email = account.Email
            });
        }
Exemplo n.º 8
0
        public override async Task <ConfirmEmailReply> HandleRequest(
            RequestObject <ConfirmEmailLinkRequest, ConfirmEmailReply> request,
            CancellationToken cancellationToken = default)
        {
            var link = request.Value.ActivationGuid;

            if (!Guid.TryParse(link, out Guid guid))
            {
                throw new ArgumentException($"{nameof(link)} is invalid");
            }

            EmailVerifies linkedEmail = await Context.EmailVerifies.FindAsync(guid);

            if (linkedEmail == null)
            {
                throw new ArgumentNullException($"{nameof(linkedEmail)} not found");
            }

            // set new email to account
            Account account = await Context.Accounts.FindAsync(linkedEmail.UserId).ConfigureAwait(false);

            account.Email = linkedEmail.Email;

            // delete out of dated
            var outdatedVerifies = Context.EmailVerifies.OutOfDated(guid);

            Context.RemoveRange(outdatedVerifies);

            // save changes
            await Context.SaveChangesAsync().ConfigureAwait(false);

            return(new ConfirmEmailReply()
            {
                Email = account.Email
            });
        }
Exemplo n.º 9
0
 private async Task RemoveStorageVerification(EmailVerifies emailVerifier)
 {
     await emailVerifierRepository.Delete(e => e.UserId.Equals(emailVerifier.UserId));
 }
Exemplo n.º 10
0
 public bool IsRequestedEmailVerifier(EmailVerifies e, int userId, string email)
 {
     return(e.UserId.Equals(userId) && e.Email.Equals(email));
 }