Commit 72ea39f9 authored by Geo Perez's avatar Geo Perez

Missing Error message and reduce error model complexity

parent e615485d
......@@ -71,8 +71,8 @@ namespace Unosquare.PassCore.Web.Controllers
{
result.Errors.Add(new ApiErrorItem
{
ErrorType = ApiErrorType.GeneralFailure,
ErrorCode = ApiErrorCode.InvalidCaptcha
ErrorCode = ApiErrorCode.InvalidCaptcha,
Message = _options.ClientSettings.Alerts.ErrorCaptcha
});
}
}
......@@ -80,9 +80,8 @@ namespace Unosquare.PassCore.Web.Controllers
{
result.Errors.Add(new ApiErrorItem
{
ErrorType = ApiErrorType.GeneralFailure,
ErrorCode = ApiErrorCode.Generic,
Message = ex.Message
ErrorCode = ApiErrorCode.Generic,
Message = ex.Message
});
}
......@@ -107,10 +106,10 @@ namespace Unosquare.PassCore.Web.Controllers
private async Task<bool> ValidateRecaptcha(string recaptchaResponse)
{
// skip validation if we don't enable recaptcha
if (string.IsNullOrWhiteSpace(_options.RecaptchaPrivateKey))return true;
if (string.IsNullOrWhiteSpace(_options.RecaptchaPrivateKey)) return true;
// immediately return false because we don't
if (string.IsNullOrEmpty(recaptchaResponse))return false;
if (string.IsNullOrEmpty(recaptchaResponse)) return false;
var requestUrl = $"https://www.google.com/recaptcha/api/siteverify?secret={_options.RecaptchaPrivateKey}&response={recaptchaResponse}";
......
namespace Unosquare.PassCore.Web.Helpers
{
using System.Collections.Generic;
using Unosquare.PassCore.Web.Models;
using System;
using Models;
// Sonar-Codacy thought we needed a static method here; and suggested dual default nulls was pointless.
internal class DebugPasswordChangeProvider : IPasswordChangeProvider
{
ApiErrorItem IPasswordChangeProvider.PerformPasswordChange(ChangePasswordModel model)
{
return DebugPasswordChangeProvider.PerformPasswordChange(model);
}
var username = model.Username.Substring(0, model.Username.IndexOf("@", StringComparison.Ordinal));
protected static ApiErrorItem PerformPasswordChange(ChangePasswordModel model)
{
var username = model.Username.Substring(0, model.Username.IndexOf("@"));
switch (username)
{
case "error":
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.Generic, Message = "Error" };
return new ApiErrorItem { ErrorCode = ApiErrorCode.Generic, Message = "Error" };
case "notfound":
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.UserNotFound, Message = "Invalid Username or Password" };
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = "Invalid Username or Password" };
default:
return null;
}
......
......@@ -22,11 +22,11 @@ namespace Unosquare.PassCore.Web.Helpers
// Check for default domain: if none given, ensure EFLD can be used as an override.
var parts = model.Username.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
var domain = parts.Length > 1 ? parts[1] : _options.ClientSettings.DefaultDomain;
// Domain-determinance
if (string.IsNullOrEmpty(domain))
{
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.InvalidDomain, Message = _options.ClientSettings.Alerts.ErrorInvalidDomain };
return new ApiErrorItem { ErrorCode = ApiErrorCode.InvalidDomain, Message = _options.ClientSettings.Alerts.ErrorInvalidDomain };
}
var username = parts.Length > 1 ? model.Username : $"{model.Username}@{domain}";
......@@ -38,17 +38,17 @@ namespace Unosquare.PassCore.Web.Helpers
// Check if the user principal exists
if (userPrincipal == null)
{
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.UserNotFound, Message = "Invalid Username or Password" };
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = _options.ClientSettings.Alerts.ErrorInvalidUserOrPassword };
}
// Check if password change is allowed
if (userPrincipal.UserCannotChangePassword)
{
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
return new ApiErrorItem { ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
}
// Validate user credentials
if (principalContext.ValidateCredentials(model.Username, model.CurrentPassword)== false)
if (principalContext.ValidateCredentials(model.Username, model.CurrentPassword) == false)
{
if (!LogonUser(username, domain, model.CurrentPassword, LogonTypes.Network, LogonProviders.Default, out _))
{
......@@ -60,7 +60,7 @@ namespace Unosquare.PassCore.Web.Helpers
// Both of these means that the password CAN change and that we got the correct password
break;
default:
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.InvalidCredentials, Message = _options.ClientSettings.Alerts.ErrorInvalidCredentials };
return new ApiErrorItem { ErrorCode = ApiErrorCode.InvalidCredentials, Message = _options.ClientSettings.Alerts.ErrorInvalidCredentials };
}
}
}
......@@ -72,7 +72,7 @@ namespace Unosquare.PassCore.Web.Helpers
{
if (_options.ClientSettings.RestrictedADGroups.Contains(userPrincipalAuthGroup.Name))
{
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
return new ApiErrorItem { ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
}
}
}
......@@ -96,7 +96,7 @@ namespace Unosquare.PassCore.Web.Helpers
}
catch (Exception ex)
{
return new ApiErrorItem { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.Generic, Message = ex.Message };
return new ApiErrorItem { ErrorCode = ApiErrorCode.Generic, Message = ex.Message };
}
return null;
......
namespace Unosquare.PassCore.Web.Models
{
/// <summary>
/// Represent error types
/// </summary>
public enum ApiErrorType
{
Success = 0,
GeneralFailure = 1,
FieldValidation = 2,
}
/// <summary>
/// Represents error codes
/// </summary>
......@@ -30,14 +20,6 @@ namespace Unosquare.PassCore.Web.Models
/// </summary>
public class ApiErrorItem
{
/// <summary>
/// Gets or sets the type of the error.
/// </summary>
/// <value>
/// The type of the error.
/// </value>
public ApiErrorType ErrorType { get; set; }
/// <summary>
/// Gets or sets the error code.
/// </summary>
......
......@@ -27,49 +27,23 @@ namespace Unosquare.PassCore.Web.Models
/// </summary>
public object Payload { get; set; }
/// <summary>
/// Creates a generic success response
/// </summary>
/// <returns>The ApiResult wih OK status</returns>
public static ApiResult Success()
{
return new ApiResult { Payload = "OK" };
}
/// <summary>
/// Creates a generic invalid request response
/// </summary>
/// <returns>The ApiResult wih Invalid request error</returns>
public static ApiResult InvalidRequest()
{
var result = new ApiResult { Payload = "Invalid Request" };
var result = new ApiResult {Payload = "Invalid Request"};
result.Errors.Add(new ApiErrorItem
{
ErrorCode = ApiErrorCode.Generic,
ErrorType = ApiErrorType.GeneralFailure,
FieldName = string.Empty,
Message = "Invalid Request"
FieldName = string.Empty,
Message = "Invalid Request"
});
return result;
}
/// <summary>
/// Adds the validation error.
/// </summary>
/// <param name="fieldName">Name of the field.</param>
/// <param name="errorCode">The error code.</param>
public void AddValidationError(string fieldName, ApiErrorCode errorCode)
{
Errors.Add(new ApiErrorItem
{
ErrorCode = errorCode,
ErrorType = ApiErrorType.FieldValidation,
FieldName = fieldName,
Message = errorCode.ToString()
});
}
/// <summary>
/// Adds the field required validation error.
/// </summary>
......@@ -79,9 +53,8 @@ namespace Unosquare.PassCore.Web.Models
Errors.Add(new ApiErrorItem
{
ErrorCode = ApiErrorCode.FieldRequired,
ErrorType = ApiErrorType.FieldValidation,
FieldName = fieldName,
Message = nameof(ApiErrorCode.FieldRequired)
FieldName = fieldName,
Message = nameof(ApiErrorCode.FieldRequired)
});
}
......@@ -94,9 +67,8 @@ namespace Unosquare.PassCore.Web.Models
Errors.Add(new ApiErrorItem
{
ErrorCode = ApiErrorCode.FieldMismatch,
ErrorType = ApiErrorType.FieldValidation,
FieldName = fieldName,
Message = nameof(ApiErrorCode.FieldMismatch)
FieldName = fieldName,
Message = nameof(ApiErrorCode.FieldMismatch)
});
}
......@@ -110,25 +82,8 @@ namespace Unosquare.PassCore.Web.Models
Errors.Add(new ApiErrorItem
{
ErrorCode = ApiErrorCode.Generic,
ErrorType = ApiErrorType.FieldValidation,
FieldName = fieldName,
Message = message
});
}
/// <summary>
/// Adds the operation error.
/// </summary>
/// <param name="errorCode">The error code.</param>
/// <param name="message">The message.</param>
public void AddOperationError(ApiErrorCode errorCode, string message)
{
Errors.Add(new ApiErrorItem
{
ErrorCode = errorCode,
ErrorType = ApiErrorType.GeneralFailure,
FieldName = string.Empty,
Message = message
FieldName = fieldName,
Message = message
});
}
......
......@@ -62,10 +62,12 @@ namespace Unosquare.PassCore.Web.Models
public class Alerts
{
public string ErrorInvalidCredentials { get; set; }
public string ErrorInvalidDomain {get; set; }
public string ErrorInvalidDomain { get; set; }
public string ErrorPasswordChangeNotAllowed { get; set; }
public string SuccessAlertBody { get; set; }
public string SuccessAlertTitle { get; set; }
public string ErrorInvalidUserOrPassword { get; set; }
public string ErrorCaptcha { get; set; }
}
public class ErrorsPasswordForm
......
......@@ -58,7 +58,9 @@
"SuccessAlertBody": "Please note it may take a few hours for your new password to reach all domain controllers.",
"ErrorPasswordChangeNotAllowed": "You are not allowed to change your password. Please contact your system administrator.",
"ErrorInvalidCredentials": "You need to provide the correct current password.",
"ErrorInvalidDomain": "You have supplied an invalid domain to logon to."
"ErrorInvalidDomain": "You have supplied an invalid domain to logon to.",
"ErrorInvalidUserOrPassword": "Invalid username or password.",
"ErrorCaptcha": "Could not verify you are not a robot"
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment