-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add user claim creator * move IUserPasswordValidator to serenity
- Loading branch information
1 parent
0ef9410
commit 1e47110
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Serenity.Net.Core/Authorization/DefaultUserClaimCreator.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,58 @@ | ||
using System.Security.Principal; | ||
|
||
namespace Serenity.Services; | ||
|
||
/// <summary> | ||
/// Default implementation for IUserClaimCreator | ||
/// </summary> | ||
public class DefaultUserClaimCreator : IUserClaimCreator | ||
{ | ||
private readonly IUserRetrieveService userRetrieveService; | ||
|
||
/// <summary> | ||
/// Creates an instance of the class | ||
/// </summary> | ||
/// <param name="userRetrieveService"></param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public DefaultUserClaimCreator(IUserRetrieveService userRetrieveService) | ||
{ | ||
this.userRetrieveService = userRetrieveService ?? throw new ArgumentNullException(nameof(userRetrieveService)); | ||
} | ||
|
||
/// <summary> | ||
/// Add User Claims To Identity | ||
/// </summary> | ||
/// <param name="identity"></param> | ||
/// <param name="userDefinition"></param> | ||
protected virtual void AddClaims(ClaimsIdentity identity, IUserDefinition userDefinition) | ||
{ | ||
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userDefinition.Id)); | ||
} | ||
|
||
/// <summary> | ||
/// Create user Principal | ||
/// </summary> | ||
/// <param name="username"></param> | ||
/// <param name="authType"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public virtual ClaimsPrincipal CreatePrincipal(string username, string authType) | ||
{ | ||
if (username is null) | ||
throw new ArgumentNullException(nameof(username)); | ||
|
||
var user = userRetrieveService.ByUsername(username); | ||
if (user == null) | ||
throw new ArgumentOutOfRangeException(nameof(username)); | ||
|
||
if (authType == null) | ||
throw new ArgumentNullException(nameof(authType)); | ||
|
||
var identity = new GenericIdentity(username, authType); | ||
AddClaims(identity, user); | ||
|
||
return new ClaimsPrincipal(identity); | ||
|
||
} | ||
} |
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,15 @@ | ||
namespace Serenity.Abstractions; | ||
|
||
/// <summary> | ||
/// Interface for getting user ClaimsPrincipal | ||
/// </summary> | ||
public interface IUserClaimCreator | ||
{ | ||
/// <summary> | ||
/// Gets the ClaimsPrincipal for user with given username | ||
/// </summary> | ||
/// <param name="username"></param> | ||
/// <param name="authType"></param> | ||
/// <returns></returns> | ||
ClaimsPrincipal CreatePrincipal(string username, string authType); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Serenity.Net.Core/Authorization/IUserPasswordValidator.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,15 @@ | ||
namespace Serenity.Abstractions; | ||
|
||
/// <summary> | ||
/// Abstraction to validate a user password | ||
/// </summary> | ||
public interface IUserPasswordValidator | ||
{ | ||
/// <summary> | ||
/// Validates a user password | ||
/// </summary> | ||
/// <param name="username"></param> | ||
/// <param name="password"></param> | ||
/// <returns><see cref="PasswordValidationResult.Valid"/> if given username and password is true</returns> | ||
PasswordValidationResult Validate(ref string username, string password); | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Serenity.Net.Core/Authorization/PasswordValidationResult.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,40 @@ | ||
namespace Serenity.ComponentModel; | ||
|
||
/// <summary> | ||
/// Password validation result | ||
/// </summary> | ||
public enum PasswordValidationResult | ||
{ | ||
/// <summary> | ||
/// Username is empty | ||
/// </summary> | ||
EmptyUsername, | ||
/// <summary> | ||
/// Password is empty | ||
/// </summary> | ||
EmptyPassword, | ||
/// <summary> | ||
/// User is not active | ||
/// </summary> | ||
InactiveUser, | ||
/// <summary> | ||
/// User source is not found | ||
/// </summary> | ||
UnknownSource, | ||
/// <summary> | ||
/// To many retries | ||
/// </summary> | ||
Throttle, | ||
/// <summary> | ||
/// Directory error | ||
/// </summary> | ||
DirectoryError, | ||
/// <summary> | ||
/// Invalid | ||
/// </summary> | ||
Invalid, | ||
/// <summary> | ||
/// Valid | ||
/// </summary> | ||
Valid | ||
} |