Skip to content

Commit

Permalink
Add user claim creator (#6852)
Browse files Browse the repository at this point in the history
* Add user claim creator

* move IUserPasswordValidator to serenity
  • Loading branch information
osmanaslancan authored Jul 25, 2023
1 parent 0ef9410 commit 1e47110
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Serenity.Net.Core/Authorization/DefaultUserClaimCreator.cs
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);

}
}
15 changes: 15 additions & 0 deletions src/Serenity.Net.Core/Authorization/IUserClaimCreator.cs
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 src/Serenity.Net.Core/Authorization/IUserPasswordValidator.cs
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 src/Serenity.Net.Core/Authorization/PasswordValidationResult.cs
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
}

0 comments on commit 1e47110

Please sign in to comment.