Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user claim creator #6852

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}