public async Task <IActionResult> SigninConfirmationAsync()
 {
     try {
         return(Ok());
     }
     catch (Exception ex) {
         Log.Error(ex, ex.Source);
         return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
     }
 }
예제 #2
0
        public async Task <IActionResult> ChangePasswordAsync([FromBody] ChangePasswordBindingModel collection)
        {
            Log.Debug($"ChangePassword => {JsonConvert.SerializeObject(collection)}");

            try {
                if (string.IsNullOrEmpty(collection?.Password) || 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]));
                }

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

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

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

                    return(Ok(_localizer[DataTransferer.PasswordChanged().Message]));
                }
                else
                {
                    return(Unauthorized(_localizer[DataTransferer.WrongPassword().Message]));
                }
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }
예제 #3
0
        public async Task <IActionResult> SigninAsync([FromBody] SigninBindingModel collection)
        {
            Log.Debug($"A User is trying to signing in with this data: {JsonConvert.SerializeObject(collection)}");

            if (string.IsNullOrEmpty(collection?.Username) || string.IsNullOrEmpty(collection?.Password))
            {
                return(BadRequest(_localizer[DataTransferer.DefectiveUsernameOrPassword().Message]));
            }

            var deviceHeader = GetDeviceInfosFromHeader();

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

            collection.DeviceId   = deviceHeader.DeviceId;
            collection.DeviceName = deviceHeader.DeviceName;
            collection.DeviceType = deviceHeader.DeviceType;

            try {
                var result = await _accountService.SigninAsync(collection).ConfigureAwait(true);

                switch (result.Code)
                {
                case 200:
                    return(Ok(result.Data));

                case 400:
                    return(BadRequest(result.Message));

                case 500:
                default:
                    return(Problem(result.Message));
                }
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }
        public async Task <IActionResult> SigninCallbackAsync(string userData = null, string remoteError = null)
        {
            try {
                if (string.IsNullOrWhiteSpace(userData))
                {
                    Log.Error("userData is not defined");
                    return(BadRequest(_localizer[DataTransferer.DefectiveEntry().Message]));
                }

                var headerbindingmodel = JsonConvert.DeserializeObject <Device>(Encoding.UTF8.GetString(Convert.FromBase64String(userData)));
                if (headerbindingmodel == null ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceId) ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceName) ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceType))
                {
                    Log.Error("userData is not valid");
                    return(BadRequest(_localizer[DataTransferer.DefectiveEntry().Message]));
                }

                // read external identity from the temporary cookie
                var authenticationResult = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme).ConfigureAwait(true);

                if (!authenticationResult.Succeeded)
                {
                    Log.Error("External authentication failed");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationFailed().Message]));
                }

                // retrieve claims of the external user
                var claimPrincipal = authenticationResult.Principal;
                if (claimPrincipal == null)
                {
                    Log.Error("External authentication user principal error");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationUserError().Message]));
                }

                // transform claims list to model
                var externalUser = GetExternalUser(claimPrincipal.Claims);

                if (string.IsNullOrWhiteSpace(externalUser.Email))
                {
                    Log.Error("External authentication user email not found");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationEmailError().Message]));
                }

                if (externalUser.ProviderId == AccountProvider.Clipboardy)
                {
                    Log.Error("External signup with unknown ProviderId");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationWithUnknownProvider().Message]));
                }

                externalUser.DeviceId   = headerbindingmodel.DeviceId;
                externalUser.DeviceName = headerbindingmodel.DeviceName;
                externalUser.DeviceType = headerbindingmodel.DeviceType;

                var accountprofile = await _accountProfileService.FirstAsync(new AccountProfileGetFirstSchema {
                    TypeId   = AccountProfileType.Email.ToInt(),
                    LinkedId = externalUser.Email
                }).ConfigureAwait(true);

                if (accountprofile == null)
                {
                    Log.Debug($"User {User.Identity.Name} try to sign in for first time at {DateTime.UtcNow}");
                    return(Ok(_accountService.ExternalSignupAsync(externalUser)));
                }
                else
                {
                    Log.Debug($"Account with Id={accountprofile.AccountId} try to sign in at {DateTime.UtcNow}");
                    var result = await _accountService.ExternalSigninAsync(externalUser, accountprofile).ConfigureAwait(false);

                    switch (result.Code)
                    {
                    case 200:
                        return(Ok(result.Data));

                    case 500:
                        return(Problem(_localizer[result.Message]));

                    default:
                        return(BadRequest(_localizer[result.Message]));
                    }
                }
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }
예제 #5
0
        public async Task <IActionResult> SignupAsync([FromBody] SignupBindingModel collection)
        {
            // todo: Captcha

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

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

            Log.Debug($"A User is trying to register with this data: {JsonConvert.SerializeObject(collection)}");
            collection.Username = collection.Username.Trim();

            try {
                if (collection.Username.IsPhoneNumber())
                {
                    collection.Phone = collection.Username;
                    if (await _accountProfileService.FirstAsync(new AccountProfileGetFirstSchema {
                        LinkedId = collection.Phone
                    }).ConfigureAwait(true) != null)
                    {
                        return(BadRequest(_localizer[DataTransferer.CellPhoneAlreadyExists().Message]));
                    }
                }
                else if (new EmailAddressAttribute().IsValid(collection.Username))
                {
                    collection.Email = collection.Username;
                    if (await _accountProfileService.FirstAsync(new AccountProfileGetFirstSchema {
                        LinkedId = collection.Email
                    }).ConfigureAwait(true) != null)
                    {
                        return(BadRequest(_localizer[DataTransferer.EmailAlreadyExists().Message]));
                    }
                }
                else
                {
                    return(BadRequest(_localizer[DataTransferer.InvalidEmailOrCellPhone().Message]));
                }

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

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

                if (string.IsNullOrEmpty(collection.DeviceId) || string.IsNullOrEmpty(collection.DeviceName))
                {
                    return(BadRequest(_localizer[DataTransferer.UnofficialRequest().Message]));
                }

                var result = await _accountService.SignupAsync(collection).ConfigureAwait(true);

                switch (result.Code)
                {
                case 200:
                    return(Ok(result.Data));

                case 400:
                    return(BadRequest(result.Message));

                case 500:
                default:
                    return(Problem(result.Message));
                }
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }
예제 #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]));
            }
        }