rama creada apartir de master en j
This commit is contained in:
@@ -13,13 +13,43 @@ using Novell.Directory.Ldap;
|
||||
|
||||
namespace adas_core.LdapLogin;
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of ILoginService that authenticates users against an LDAP server.
|
||||
/// It retrieves user information and authorities based on the LDAP entry and maps them to the application's user model.
|
||||
/// The service also handles the creation of new users in the application if they do not already exist, based on the LDAP information.
|
||||
/// It uses configuration settings for connecting to the LDAP server and for mapping LDAP attributes to user properties and authorities.
|
||||
/// </summary>
|
||||
public class LdapLoginService : ILoginService
|
||||
{
|
||||
/// <summary>
|
||||
/// The IAuthorityService is used to manage user authorities in the application.
|
||||
/// </summary>
|
||||
private readonly IAuthorityService _authorityService;
|
||||
|
||||
/// <summary>
|
||||
/// The LdapConfig contains the necessary configuration for connecting to the LDAP server, such as server address, port, search base, and attribute mappings.
|
||||
/// </summary>
|
||||
private readonly LdapConfig _ldapConfig;
|
||||
|
||||
/// <summary>
|
||||
/// The ILogger is used for logging information, warnings, and errors related to LDAP login operations.
|
||||
/// </summary>
|
||||
private readonly ILogger<LdapLoginService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// The IUserService is used to manage user information in the application, such as retrieving existing users or creating new users based on LDAP information.
|
||||
/// </summary>
|
||||
private readonly Lazy<IUserService> _userService;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for LdapLoginService. It initializes the service with the necessary dependencies and validates the LDAP configuration.
|
||||
/// </summary>
|
||||
/// <param name="ldapConfig">The LDAP configuration options.</param>
|
||||
/// <param name="validator">The validator for the LDAP configuration.</param>
|
||||
/// <param name="userService">The user service for managing user information.</param>
|
||||
/// <param name="authorityService">The authority service for managing user authorities.</param>
|
||||
/// <param name="logger">The logger for logging LDAP login operations.</param>
|
||||
public LdapLoginService(
|
||||
IOptions<LdapConfig> ldapConfig,
|
||||
IValidator<LdapConfig> validator,
|
||||
@@ -34,9 +64,24 @@ public class LdapLoginService : ILoginService
|
||||
validator.Validate(_ldapConfig, options => options.ThrowOnFailures());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that this login service allows password authentication, as it connects to an LDAP server which typically requires a username and password for authentication.
|
||||
/// </summary>
|
||||
public bool AllowPassword => true;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that this login service does not allow token-based authentication, as it is designed to authenticate users against an LDAP server using their credentials rather than tokens.
|
||||
/// </summary>
|
||||
public UserEnum.LoginMethod Method => UserEnum.LoginMethod.Ldap;
|
||||
|
||||
/// <summary>
|
||||
/// Authenticates a user against the LDAP server using the provided username and password.
|
||||
/// </summary>
|
||||
/// <param name="username">The username of the user to authenticate.</param>
|
||||
/// <param name="password">The password of the user to authenticate.</param>
|
||||
/// <returns>The authenticated user.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when there is an error during the login process.</exception>
|
||||
/// <exception cref="UserNotFoundException">Thrown when the user is not found in the LDAP directory.</exception>
|
||||
public async Task<User> Login(string username, string password)
|
||||
{
|
||||
// Check for LDAP config
|
||||
@@ -117,36 +162,79 @@ public class LdapLoginService : ILoginService
|
||||
return user ?? throw new LoginServicesException("LDAP User not found");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the login process is handled through the Login(string username, string password) method.
|
||||
/// </summary>
|
||||
/// <param name="context">The HTTP context of the request.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<User> Login(HttpContext context)
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the authentication process is handled through the Login(string username, string password) method.
|
||||
/// </summary>
|
||||
/// <param name="username">The username of the user to authenticate.</param>
|
||||
/// <param name="password">The password of the user to authenticate.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<User> Authenticate(string username, string password)
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the user retrieval process is handled through the Login(string username, string password) method and the GetOrCreateUser(User userEntryLdap, LdapEntry entry) method.
|
||||
/// </summary>
|
||||
/// <param name="id">The ID of the user to retrieve.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<User?> GetById(ObjectId id)
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the user retrieval process is handled through the Login(string username, string password) method and the GetOrCreateUser(User userEntryLdap, LdapEntry entry) method.
|
||||
/// </summary>
|
||||
/// <param name="email">The email of the user to retrieve.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<User?> GetByEmail(string email)
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the user retrieval process is handled through the Login(string username, string password) method and the GetOrCreateUser(User userEntryLdap, LdapEntry entry) method.
|
||||
/// </summary>
|
||||
/// <param name="username">The username of the user to retrieve.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<User?> GetByUsername(string username)
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is not implemented in the LdapLoginService, as the user retrieval process is handled through the Login(string username, string password) method and the GetOrCreateUser(User userEntryLdap, LdapEntry entry) method.
|
||||
/// </summary>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
/// <exception cref="LoginServicesException">Thrown when the method is not implemented.</exception>
|
||||
public Task<List<User>> GetAllUsers()
|
||||
{
|
||||
throw new LoginServicesException("Not implemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method retrieves an existing user from the application based on the information obtained from the LDAP entry, or creates a new user if one does not already exist.
|
||||
/// It also checks and updates the user's authorities based on the LDAP entry and the application's configuration.
|
||||
/// </summary>
|
||||
/// <param name="userEntryLdap">The user information obtained from the LDAP entry.</param>
|
||||
/// <param name="entry">The LDAP entry containing the user's information.</param>
|
||||
/// <returns>The existing or newly created user with updated authorities.</returns>
|
||||
private async Task<User?> GetOrCreateUser(User userEntryLdap, LdapEntry entry)
|
||||
{
|
||||
var userToReturn = (await _userService.Value.GetUserByUserName(userEntryLdap.UserName) ??
|
||||
@@ -167,6 +255,12 @@ public class LdapLoginService : ILoginService
|
||||
return userToReturn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method checks the authorities of a user based on the LDAP entry and the application's configuration.
|
||||
/// </summary>
|
||||
/// <param name="user">The user whose authorities are being checked.</param>
|
||||
/// <param name="entry">The LDAP entry containing the user's information.</param>
|
||||
/// <returns>A list of updated authorities for the user.</returns>
|
||||
private async Task<List<Authorization>> CheckAuthorities(User user, LdapEntry entry)
|
||||
{
|
||||
try
|
||||
@@ -211,6 +305,12 @@ public class LdapLoginService : ILoginService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method retrieves a list of authorities for a user based on a whitelist defined in the application's configuration.
|
||||
/// </summary>
|
||||
/// <param name="entry">The LDAP entry containing the user's information.</param>
|
||||
/// <param name="user">The user whose authorities are being retrieved.</param>
|
||||
/// <returns>A list of authorities for the user based on the whitelist.</returns>
|
||||
private List<Authorization> GetAuthoritiesWhiteList(LdapEntry entry, User user)
|
||||
{
|
||||
try
|
||||
@@ -253,6 +353,11 @@ public class LdapLoginService : ILoginService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method retrieves a list of authorities for a user based on the LDAP entry and the application's configuration for mapping LDAP groups to authorities.
|
||||
/// </summary>
|
||||
/// <param name="ldapEntry">The LDAP entry containing the user's information.</param>
|
||||
/// <returns>The existing or newly created user with updated authorities.</returns>
|
||||
private User GetUser(LdapEntry ldapEntry)
|
||||
{
|
||||
var attributes = ldapEntry.GetAttributeSet();
|
||||
@@ -294,6 +399,12 @@ public class LdapLoginService : ILoginService
|
||||
return user;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method retrieves a list of authorities for a user based on the LDAP entry and the application's configuration for mapping LDAP groups to authorities.
|
||||
/// </summary>
|
||||
/// <param name="ldapEntry">The LDAP entry containing the user's information.</param>
|
||||
/// <param name="user">The user whose authorities are being retrieved.</param>
|
||||
/// <returns>A list of authorities for the user based on the LDAP entry and the application's configuration.</returns>
|
||||
private List<Authorization> GetAuthoritiesMap(LdapEntry ldapEntry, User user)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user