-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-114: decoupled login error logic between modules (#2746)
Co-authored-by: Oleg Zhuk <[email protected]>
- Loading branch information
1 parent
70bf9bf
commit 06cf1fb
Showing
9 changed files
with
258 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/VirtoCommerce.Platform.Security/Model/SignInValidatorContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using VirtoCommerce.Platform.Core.Security; | ||
|
||
namespace VirtoCommerce.Platform.Security.Model | ||
{ | ||
public class SignInValidatorContext | ||
{ | ||
public ApplicationUser User { get; set; } | ||
|
||
public string StoreId { get; set; } | ||
|
||
public bool DetailedErrors { get; set; } | ||
|
||
public bool IsSucceeded { get; set; } | ||
|
||
public bool IsLockedOut { get; set; } | ||
|
||
public IDictionary<string, object> AdditionalParameters { get; set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/VirtoCommerce.Platform.Security/Model/TokenLoginResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Identity; | ||
using OpenIddict.Abstractions; | ||
|
||
namespace VirtoCommerce.Platform.Security.Model | ||
{ | ||
public class TokenLoginResponse : OpenIddictResponse | ||
{ | ||
public string UserId { get; set; } | ||
|
||
public IList<IdentityError> Errors | ||
{ | ||
get | ||
{ | ||
var errors = new List<IdentityError>(); | ||
if (Code != null) | ||
{ | ||
errors.Add(new IdentityError | ||
{ | ||
Code = Code, | ||
Description = ErrorDescription | ||
}); | ||
} | ||
return errors; | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/VirtoCommerce.Platform.Security/SecurityErrorDescriber.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using VirtoCommerce.Platform.Core.Common; | ||
using VirtoCommerce.Platform.Security.Model; | ||
using static OpenIddict.Abstractions.OpenIddictConstants; | ||
|
||
namespace VirtoCommerce.Platform.Security | ||
{ | ||
public static class SecurityErrorDescriber | ||
{ | ||
public static TokenLoginResponse LoginFailed() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(LoginFailed).ToSnakeCase(), | ||
ErrorDescription = "Login attempt failed. Please check your credentials." | ||
}; | ||
|
||
public static TokenLoginResponse UserIsLockedOut() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(UserIsLockedOut).ToSnakeCase(), | ||
ErrorDescription = "Your account has been locked. Please contact support for assistance." | ||
}; | ||
|
||
public static TokenLoginResponse UserIsTemporaryLockedOut() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(UserIsLockedOut).ToSnakeCase(), | ||
ErrorDescription = "Your account has been temporarily locked. Please try again after some time." | ||
}; | ||
|
||
public static TokenLoginResponse PasswordExpired() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(PasswordExpired).ToSnakeCase(), | ||
ErrorDescription = "Your password has been expired and must be changed.", | ||
}; | ||
|
||
public static TokenLoginResponse PasswordLoginDisabled() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(PasswordLoginDisabled).ToSnakeCase(), | ||
ErrorDescription = "The username/password login is disabled." | ||
}; | ||
|
||
public static TokenLoginResponse TokenInvalid() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(TokenInvalid).ToSnakeCase(), | ||
ErrorDescription = "The token is no longer valid." | ||
}; | ||
|
||
public static TokenLoginResponse SignInNotAllowed() => new() | ||
{ | ||
Error = Errors.InvalidGrant, | ||
Code = nameof(SignInNotAllowed).ToSnakeCase(), | ||
ErrorDescription = "The user is no longer allowed to sign in." | ||
}; | ||
|
||
public static TokenLoginResponse InvalidClient() => new() | ||
{ | ||
Error = Errors.InvalidClient, | ||
Code = nameof(InvalidClient).ToSnakeCase(), | ||
ErrorDescription = "The client application was not found in the database." | ||
}; | ||
|
||
public static TokenLoginResponse UnsupportedGrantType() => new() | ||
{ | ||
Error = Errors.UnsupportedGrantType, | ||
Code = nameof(UnsupportedGrantType).ToSnakeCase(), | ||
ErrorDescription = "The specified grant type is not supported." | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/VirtoCommerce.Platform.Security/Services/BaseUserSignInValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using VirtoCommerce.Platform.Security.Model; | ||
|
||
namespace VirtoCommerce.Platform.Security.Services | ||
{ | ||
public class BaseUserSignInValidator : IUserSignInValidator | ||
{ | ||
public int Priority { get; set; } | ||
|
||
public Task<IList<TokenLoginResponse>> ValidateUserAsync(SignInValidatorContext context) | ||
{ | ||
var result = new List<TokenLoginResponse>(); | ||
|
||
if (!context.IsSucceeded) | ||
{ | ||
var error = SecurityErrorDescriber.LoginFailed(); | ||
|
||
if (context.DetailedErrors && context.IsLockedOut) | ||
{ | ||
var permanentLockOut = context.User.LockoutEnd == DateTime.MaxValue.ToUniversalTime(); | ||
error = permanentLockOut ? SecurityErrorDescriber.UserIsLockedOut() : SecurityErrorDescriber.UserIsTemporaryLockedOut(); | ||
} | ||
|
||
result.Add(error); | ||
} | ||
|
||
return Task.FromResult<IList<TokenLoginResponse>>(result); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/VirtoCommerce.Platform.Security/Services/IUserSignInValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using VirtoCommerce.Platform.Security.Model; | ||
|
||
namespace VirtoCommerce.Platform.Security.Services | ||
{ | ||
public interface IUserSignInValidator | ||
{ | ||
public int Priority { get; set; } | ||
|
||
Task<IList<TokenLoginResponse>> ValidateUserAsync(SignInValidatorContext context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.