public void First()
        {
            var query = new AccountProfileGetFirstSchema {
                Id = 1
            };
            var account = _accountProfileService.FirstAsync(query).GetAwaiter().GetResult();

            Assert.IsTrue(query.StatusCode == 200);
            Assert.IsTrue(account != null);
        }
예제 #2
0
        public async Task <AccountProfileResult> FirstAsync(AccountProfileGetFirstSchema accountProfile)
        {
            var result = await _storedProcedure.QueryFirstAsync <AccountProfileGetFirstSchema, AccountProfileResult>(accountProfile);

            return(result);
        }
예제 #3
0
        public async Task <IServiceResult> SigninAsync(SigninBindingModel signinModel)
        {
            var now = DateTime.UtcNow;

            using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                try {
                    var query = new AccountProfileGetFirstSchema {
                        LinkedId = signinModel.Username
                                   //StatusId = Status.Active
                    };
                    if (signinModel.Username.IsPhoneNumber())
                    {
                        query.TypeId = AccountProfileType.Phone.ToInt();
                    }
                    else if (new EmailAddressAttribute().IsValid(signinModel.Username))
                    {
                        query.TypeId = AccountProfileType.Email.ToInt();
                    }
                    else
                    {
                        return(DataTransferer.InvalidEmailOrCellPhone());
                    }

                    var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                    if (accountProfile == null)
                    {
                        if (query.TypeId == AccountProfileType.Phone.ToInt())
                        {
                            return(DataTransferer.PhoneNotFound());
                        }
                        else
                        {
                            return(DataTransferer.EmailNotFound());
                        }
                    }

                    var account = await FirstAsync(new AccountGetFirstSchema {
                        Id       = accountProfile.AccountId,
                        StatusId = Status.Active.ToInt()
                    }).ConfigureAwait(true);

                    if (account == null)
                    {
                        return(DataTransferer.UserIsNotActive());
                    }

                    var deviceId = 0;
                    // check password
                    if (_cryptograph.IsEqual(signinModel.Password, account.Password))
                    {
                        var accountDeviceQuery = new AccountDeviceGetFirstSchema {
                            AccountId = account.Id,
                            DeviceId  = signinModel.DeviceId
                        };
                        var accountDevice = await _accountDeviceService.FirstAsync(accountDeviceQuery);

                        // todo: check the accountDevice

                        if (accountDevice != null)
                        {
                            deviceId = accountDevice.Id.Value;
                            // set new token
                            await _accountDeviceService.UpdateAsync(new AccountDeviceUpdateSchema {
                                Id = accountDevice.Id.Value,
                            });
                        }
                        else
                        {
                            // create new device for account
                            deviceId = await _accountDeviceService.AddAsync(new AccountDeviceAddSchema {
                                AccountId  = account.Id,
                                DeviceId   = signinModel.DeviceId,
                                DeviceName = signinModel.DeviceName,
                                DeviceType = signinModel.DeviceType,
                                CreatedAt  = now,
                                StatusId   = Status.Active.ToInt()
                            });
                        }
                    }
                    else
                    {
                        return(DataTransferer.WrongPassword());
                    }

                    // clean forgot password tokens
                    await _accountProfileService.CleanForgotPasswordTokensAsync(account.Id.Value);

                    //if(accountProfilesQuery.StatusCode != 200) {
                    //    Log.Error($"Can't update 'ForgotPasswordTokens' to NULL for AccountId={account.Id}");
                    //    return DataTransferer.SomethingWentWrong();
                    //}

                    // set last signed in at
                    var changedAccount = UpdateAsync(new AccountUpdateSchema {
                        LastSignedinAt = now
                    });

                    transaction.Complete();
                    var token = _jwtHandler.Bearer(new Account(account.Id.Value, deviceId, signinModel.Username, now).ToClaimsIdentity());
                    return(DataTransferer.Ok(token));
                }
                catch (Exception ex) {
                    Log.Error(ex, ex.Source);
                    return(DataTransferer.InternalServerError());
                }
            }
        }
예제 #4
0
        public async Task <ActionResult> ActivateAccountAsync([FromBody] ActivateAccountBindingModel collection)
        {
            if (collection == null ||
                string.IsNullOrWhiteSpace(collection.Username) ||
                string.IsNullOrWhiteSpace(collection.Code))
            {
                return(BadRequest(_localizer[DataTransferer.DefectiveEntry().Message]));
            }

            var query = new AccountProfileGetFirstSchema {
                LinkedId = collection.Username
            };

            if (collection.Username.IsPhoneNumber())
            {
                query.TypeId = AccountProfileType.Phone.ToInt();
            }
            else if (new EmailAddressAttribute().IsValid(collection.Username))
            {
                query.TypeId = AccountProfileType.Email.ToInt();
            }
            else
            {
                return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
            }

            try {
                var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                if (accountProfile == null)
                {
                    if (query.TypeId == AccountProfileType.Phone.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.PhoneNotFound().Message]));
                    }

                    if (query.TypeId == AccountProfileType.Email.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailNotFound().Message]));
                    }
                }

                var activationCode = _memoryCache.Get(collection.Username);
                if (activationCode == null)
                {
                    return(BadRequest(_localizer[DataTransferer.ActivationCodeRequestedNotFound().Message]));
                }

                if (collection.Code != activationCode.ToString())
                {
                    return(BadRequest(_localizer[DataTransferer.ActivationCodeRequestedNotFound().Message]));
                }

                _memoryCache.Remove(collection.Username);
                var accountProfileQuery = new AccountProfileUpdateSchema {
                    Id       = accountProfile.Id.Value,
                    StatusId = Status.Active.ToInt()
                };
                await _accountProfileService.UpdateAsync(accountProfileQuery).ConfigureAwait(true);

                return(Ok(_localizer[DataTransferer.AccountActivated().Message]));
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem());
            }
        }
예제 #5
0
        public async Task <ActionResult> ActivationRequestAsync()
        {
            var query = new AccountProfileGetFirstSchema {
                LinkedId = CurrentAccount.Username
            };

            if (CurrentAccount.Username.IsPhoneNumber())
            {
                query.TypeId = AccountProfileType.Phone.ToInt();
            }
            else if (new EmailAddressAttribute().IsValid(CurrentAccount.Username))
            {
                query.TypeId = AccountProfileType.Email.ToInt();
            }
            else
            {
                return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
            }

            try {
                var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                if (accountProfile == null)
                {
                    if (query.TypeId == AccountProfileType.Phone.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.PhoneNotFound().Message]));
                    }

                    if (query.TypeId == AccountProfileType.Email.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailNotFound().Message]));
                    }
                }

                var activationCode = _randomMaker.NewNumber(10000, 99999);
                _memoryCache.Set(CurrentAccount.Username, activationCode, DateTime.Now.AddMinutes(10));

                if (query.TypeId == AccountProfileType.Phone.ToInt())
                {
                    await _smsService.SendAsync(new SMSModel {
                        PhoneNo  = accountProfile.LinkedId,
                        TextBody = $"{DataTransferer.ActivationCodeSMSBody().Message} \r\n {activationCode}"
                    }).ConfigureAwait(false);
                }
                else
                {
                    await _emailService.SendAsync(new EmailModel {
                        Address    = accountProfile.LinkedId,
                        Subject    = _localizer[DataTransferer.ActivationCodeEmailSubject().Message],
                        IsBodyHtml = true,
                        Body       = $"<p>{DataTransferer.ActivationCodeEmailBody().Message}</p>" +
                                     $"<p>{activationCode}</p>"
                    }).ConfigureAwait(false);
                }

                return(Ok(_localizer[DataTransferer.ActivationCodeRequested().Message]));
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem());
            }
        }
예제 #6
0
        public async Task <IActionResult> ForgotPasswordAsync([FromBody] ForgotPasswordBindingModel collection)
        {
            Log.Debug($"ForgotPassword => {JsonConvert.SerializeObject(collection)}");

            if (string.IsNullOrEmpty(collection?.Username))
            {
                return(BadRequest(_localizer[DataTransferer.DefectiveEmailOrCellPhone().Message]));
            }
            collection.Username = collection.Username.Trim();

            try {
                var query = new AccountProfileGetFirstSchema {
                    LinkedId = collection.Username
                };
                if (collection.Username.IsPhoneNumber())
                {
                    query.TypeId = AccountProfileType.Phone.ToInt();
                }
                else if (new EmailAddressAttribute().IsValid(collection.Username))
                {
                    query.TypeId = AccountProfileType.Email.ToInt();
                }
                else
                {
                    return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
                }

                var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                if (accountProfile == null)
                {
                    if (query.TypeId == AccountProfileType.Phone.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.PhoneNotFound().Message]));
                    }

                    if (query.TypeId == AccountProfileType.Email.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailNotFound().Message]));
                    }
                }

                var token         = _randomMaker.NewToken();
                var username      = Convert.ToBase64String(Encoding.UTF8.GetBytes(collection.Username));
                var changepassurl = $"clipboardy.com/api/account/changepasswordrequested?username={username}&token={token}";
                _memoryCache.Set(username, token, DateTime.Now.AddMinutes(10));

                if (query.TypeId == AccountProfileType.Phone.ToInt())
                {
                    await _smsService.SendAsync(new SMSModel {
                        PhoneNo  = accountProfile.LinkedId,
                        TextBody = $"{DataTransferer.ForgotPasswordSMSBody().Message} \r\n {changepassurl}"
                    }).ConfigureAwait(false);
                }
                else
                {
                    await _emailService.SendAsync(new EmailModel {
                        Address    = accountProfile.LinkedId,
                        Subject    = _localizer[DataTransferer.ForgotPasswordEmailSubject().Message],
                        IsBodyHtml = true,
                        Body       = $"<p>{DataTransferer.ForgotPasswordEmailBody().Message}</p>" +
                                     $"<p>{changepassurl}</p>"
                    }).ConfigureAwait(false);
                }

                return(Ok(changepassurl));
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }
예제 #7
0
        public async Task <IActionResult> ChangeForgotenPasswordAsync([FromBody] ChangeForgotenPasswordBindingModel collection)
        {
            Log.Debug($"ChangeForgotenPassword => {JsonConvert.SerializeObject(collection)}");

            try {
                if (string.IsNullOrEmpty(collection?.Username))
                {
                    return(BadRequest(_localizer[DataTransferer.DefectiveEmailOrCellPhone().Message]));
                }
                collection.Username = collection.Username.Trim();

                if (string.IsNullOrEmpty(collection.NewPassword) && string.IsNullOrEmpty(collection.ConfirmPassword))
                {
                    return(BadRequest(_localizer[DataTransferer.DefectivePassword().Message]));
                }

                if (collection.NewPassword != collection.ConfirmPassword)
                {
                    return(BadRequest(_localizer[DataTransferer.PasswordsMissmatch().Message]));
                }

                if (string.IsNullOrWhiteSpace(collection.Token))
                {
                    return(BadRequest(_localizer[DataTransferer.UnofficialRequest().Message]));
                }

                var query = new AccountProfileGetFirstSchema {
                    LinkedId = collection.Username
                };
                if (collection.Username.IsPhoneNumber())
                {
                    query.TypeId = AccountProfileType.Phone.ToInt();
                }
                else if (new EmailAddressAttribute().IsValid(collection.Username))
                {
                    query.TypeId = AccountProfileType.Email.ToInt();
                }
                else
                {
                    return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
                }

                var accountProfile = await _accountProfileService.FirstAsync(query).ConfigureAwait(true);

                if (accountProfile == null)
                {
                    if (query.TypeId == AccountProfileType.Phone.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.PhoneNotFound().Message]));
                    }

                    if (query.TypeId == AccountProfileType.Email.ToInt())
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailNotFound().Message]));
                    }
                }

                var cachedToken = _memoryCache.Get(collection.Username);
                if (cachedToken == null)
                {
                    return(BadRequest(_localizer[DataTransferer.ChangingPasswordWithoutToken().Message]));
                }

                var account = await _accountService.FirstAsync(new AccountGetFirstSchema {
                    Id = accountProfile.AccountId.Value
                }).ConfigureAwait(true);

                if (account == null)
                {
                    return(BadRequest(_localizer[DataTransferer.UserNotFound().Message]));
                }

                if (collection.Token != cachedToken.ToString())
                {
                    Log.Warning($"Account => {account}, AccountProfile => {accountProfile}, It tried to change its password with a wrong 'ForgotPasswordToken'");
                    return(BadRequest(_localizer[DataTransferer.ChangingPasswordWithWrongToken().Message]));
                }

                _memoryCache.Remove(collection.Username);

                await _accountService.UpdateAsync(new AccountUpdateSchema {
                    Id       = account.Id.Value,
                    Password = _cryptograph.RNG(collection.NewPassword)
                }).ConfigureAwait(false);

                return(Ok(_localizer[DataTransferer.PasswordChanged().Message]));
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }