Esempio n. 1
0
        public async Task <IActionResult> GoogleLogin([FromBody] GoogleLoginRequestModel model)
        {
            var result = await _authService.GoogleAuthenticateAsync(model.AccessToken);

            switch (result.Error)
            {
            case CustomerError.None:
                return(Ok(_mapper.Map <LoginResponseModel>(result)));

            case CustomerError.LoginNotFound:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidCredentials);

            case CustomerError.LoginExistsWithDifferentProvider:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.LoginExistsWithDifferentProvider);

            case CustomerError.InvalidOrExpiredGoogleAccessToken:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidOrExpiredGoogleAccessToken);

            case CustomerError.CustomerBlocked:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.CustomerBlocked);

            case CustomerError.CustomerProfileDeactivated:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.CustomerIsNotActive);

            default:
                throw new InvalidOperationException(
                          $"Unexpected error during Authenticate with access token {model.AccessToken} - {result.Error}");
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> ChangePasswordAsync([FromBody] ChangePasswordRequestModel model)
        {
            var customerId = _requestContext.UserId;

            var changePasswordResult = await _customerService.ChangePasswordAsync(customerId, model.Password);

            switch (changePasswordResult.Error)
            {
            case CustomerError.None:
                var customerInfo = await _customerService.GetCustomerInfoAsync(customerId, true);

                if (customerInfo == null)
                {
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidCredentials);
                }

                var reLoginResult = await _customerManagementServiceClient.AuthApi.AuthenticateAsync(
                    new AuthenticateRequestModel
                {
                    Email         = customerInfo.Email,
                    Password      = model.Password,
                    LoginProvider = LoginProvider.Standard
                });

                switch (reLoginResult.Error)
                {
                case CustomerManagementError.None:
                    return(Ok(_mapper.Map <LoginResponseModel>(reLoginResult)));

                case CustomerManagementError.LoginNotFound:
                case CustomerManagementError.PasswordMismatch:
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidCredentials);

                case CustomerManagementError.InvalidLoginFormat:
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidEmailFormat);

                case CustomerManagementError.InvalidPasswordFormat:
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidPasswordFormat);

                case CustomerManagementError.LoginExistsWithDifferentProvider:
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.LoginExistsWithDifferentProvider);

                case CustomerManagementError.CustomerBlocked:
                    throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.CustomerBlocked);

                default:
                    throw new InvalidOperationException($"Unexpected error during Authenticate for {customerInfo.Email.SanitizeEmail()} - {reLoginResult.Error}");
                }

            case CustomerError.InvalidPasswordFormat:
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.InvalidPasswordFormat);

            case CustomerError.CustomerBlocked:
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.CustomerBlocked);

            default:
                throw new InvalidOperationException($"Unexpected error during password change for {customerId} - {changePasswordResult}");
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Login([FromBody] LoginRequestModel model)
        {
            var authModel = await _authService.AuthAsync(model.ClientId, model.ClientSecret, model.UserInfo);

            switch (authModel.Error)
            {
            case ServicesError.None:
                return(Ok(new LoginResponseModel {
                    Token = authModel.Token
                }));

            case ServicesError.LoginNotFound:
            case ServicesError.PasswordMismatch:
            case ServicesError.InvalidCredentials:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidCredentials);

            default:
                throw new InvalidOperationException(
                          $"Unexpected error during Authenticate for {model.ClientId} - {authModel.Error}");
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Login([FromBody] LoginRequestModel model)
        {
            if (!model.Email.IsValidEmailAndRowKey())
            {
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.InvalidEmailFormat);
            }

            var signinLockStatus = await _signinThrottlingService.IsSigninLockedAsync(model.Email);

            if (signinLockStatus.IsLocked)
            {
                return(signinLockStatus.GetSigninLockedResponse());
            }

            var result = await _customerManagementServiceClient.AuthApi.AuthenticateAsync(
                _mapper.Map <AuthenticateRequestModel>(model));

            if (result.Error.IsPasswordWrong())
            {
                var failedSigninResult = await _signinThrottlingService.RegisterFailedSigninAsync(model.Email);

                if (failedSigninResult.Effect != FailedSigninEffect.None)
                {
                    if (failedSigninResult.Effect == FailedSigninEffect.Warning)
                    {
                        return(failedSigninResult.GetWarningResponse());
                    }

                    if (failedSigninResult.Effect == FailedSigninEffect.SigninLocked)
                    {
                        return(failedSigninResult.GetSigninLockedResponse());
                    }

                    throw new InvalidOperationException($"Unexpected throttling effect during authentication for {model.Email.SanitizeEmail()} - {failedSigninResult.Effect.ToString()}");
                }
            }

            switch (result.Error)
            {
            case CustomerManagementError.None:
                await _signinThrottlingService.FlushFailuresAsync(model.Email);

                return(Ok(_mapper.Map <LoginResponseModel>(result)));

            case CustomerManagementError.LoginNotFound:
            case CustomerManagementError.PasswordMismatch:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidCredentials);

            case CustomerManagementError.InvalidLoginFormat:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidEmailFormat);

            case CustomerManagementError.InvalidPasswordFormat:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.InvalidPasswordFormat);

            case CustomerManagementError.LoginExistsWithDifferentProvider:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.LoginExistsWithDifferentProvider);

            case CustomerManagementError.CustomerBlocked:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.CustomerBlocked);

            case CustomerManagementError.CustomerProfileDeactivated:
                throw LykkeApiErrorException.Unauthorized(ApiErrorCodes.Service.CustomerIsNotActive);

            default:
                throw new InvalidOperationException($"Unexpected error during Authenticate for {model.Email.SanitizeEmail()} - {result.Error.ToString()}");
            }
        }