Compare commits
2 Commits
docs/1.0.1
...
8e6bc709f3
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e6bc709f3 | |||
| 9e2246a459 |
@@ -21,23 +21,20 @@
|
||||
|
||||
<PackageReference Include="AutoMapper" Version="16.1.1" />
|
||||
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="5.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.11" />
|
||||
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.18.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="5.3.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.9" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.9.0" />
|
||||
<PackageReference Include="Quartz" Version="3.18.1" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.13.17" />
|
||||
<PackageReference Include="System.Text.Json" Version="10.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.AspNetCore.Http.Features">
|
||||
<HintPath>..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.AspNetCore.App\7.0.10\Microsoft.AspNetCore.Http.Features.dll</HintPath>
|
||||
</Reference>
|
||||
<PackageReference Include="StackExchange.Redis" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -9,12 +9,17 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.10" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.27" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.18.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.18.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.11" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.19.1" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.19.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -10,17 +10,13 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.10" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.27" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.28" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="3.9.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.3" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="SharpCompress" Version="0.49.0" />
|
||||
<PackageReference Include="Snappier" Version="1.3.1" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.18.0" />
|
||||
<PackageReference Include="System.Text.Json" Version="10.0.8" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.19.1" />
|
||||
<PackageReference Include="System.Text.Json" Version="10.0.9" />
|
||||
<PackageReference Include="TagLibSharp" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1296,7 +1296,6 @@ public class MasterListRepository<T> : MongoRepository<T>, IMasterListRepository
|
||||
|
||||
// Normaliza el texto de búsqueda a minúsculas
|
||||
var normalizedTextSearch = textSearch.ToLowerInvariant();
|
||||
// Construye la expresión regular dinámica para tener en cuenta la acentuación en las vocales
|
||||
var regexPattern = BuildRegexPattern(normalizedTextSearch);
|
||||
// foreach (var c in normalizedTextSearch)
|
||||
// {
|
||||
|
||||
@@ -9,11 +9,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="EasyNetQ" Version="8.1.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.8" />
|
||||
<PackageReference Include="EasyNetQ" Version="8.1.5" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.9" />
|
||||
<PackageReference Include="MongoMigrations.Core" Version="4.0.15" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.13.17" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -5,11 +5,12 @@ using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.LdapLogin.Configuration;
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Novell.Directory.Ldap;
|
||||
using System.DirectoryServices.Protocols;
|
||||
using System.Net;
|
||||
using Authorization = adas_core.Domain.Models.MongoModels.Authorization;
|
||||
|
||||
namespace adas_core.LdapLogin;
|
||||
|
||||
@@ -84,24 +85,18 @@ public class LdapLoginService : ILoginService
|
||||
/// <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
|
||||
if (_ldapConfig.Server == null) throw new LoginServicesException("LDAP Config not found");
|
||||
var conn = new LdapConnection();
|
||||
if (_ldapConfig.Server == null)
|
||||
throw new LoginServicesException("LDAP Config not found");
|
||||
|
||||
var identifier = new LdapDirectoryIdentifier(_ldapConfig.Server, _ldapConfig.Port ?? 389);
|
||||
var connection = new LdapConnection(identifier);
|
||||
|
||||
try
|
||||
{
|
||||
await conn.ConnectAsync(_ldapConfig.Server, _ldapConfig.Port ?? 389);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("[LDAP] Error connecting to {server}, port {port}, Exception: {e}", _ldapConfig.Server,
|
||||
_ldapConfig.Port, e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (_ldapConfig.LdapUser != null)
|
||||
{
|
||||
_logger.LogInformation("[LDAP] _ldapConfig.LdapUser is enabled with {LdapUser}", _ldapConfig.LdapUser);
|
||||
await conn.BindAsync(_ldapConfig.LdapUser, _ldapConfig.LdapPassword);
|
||||
_logger.LogInformation("[LDAP] Using configured LDAP user {LdapUser}", _ldapConfig.LdapUser);
|
||||
connection.Credential = new NetworkCredential(_ldapConfig.LdapUser, _ldapConfig.LdapPassword);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -109,56 +104,52 @@ public class LdapLoginService : ILoginService
|
||||
? _ldapConfig.UserDomainName + @"\"
|
||||
: "") + username;
|
||||
|
||||
try
|
||||
{
|
||||
await conn.BindAsync(ldapUser, password);
|
||||
connection.Credential = new NetworkCredential(ldapUser, password);
|
||||
}
|
||||
|
||||
connection.AuthType = AuthType.Basic;
|
||||
connection.Bind();
|
||||
}
|
||||
catch (LdapException e)
|
||||
{
|
||||
_logger.LogError("[LDAP] Error binding ldapUser: {LdapUser} and password", ldapUser);
|
||||
|
||||
_logger.LogError("[LDAP] Error binding user {username}", username);
|
||||
throw new UserNotFoundException(username, e);
|
||||
}
|
||||
}
|
||||
|
||||
var results = await conn.SearchAsync(
|
||||
_ldapConfig.SearchBase,
|
||||
LdapConnection.ScopeSub,
|
||||
$"({_ldapConfig.UserNameProperty}={username})",
|
||||
null,
|
||||
false);
|
||||
|
||||
|
||||
LdapEntry? entry = null;
|
||||
|
||||
while (await results.HasMoreAsync())
|
||||
{
|
||||
LdapEntry? current = null;
|
||||
SearchResultEntry? entry = null;
|
||||
|
||||
try
|
||||
{
|
||||
current = await results.NextAsync();
|
||||
}
|
||||
catch (LdapException ex)
|
||||
{
|
||||
_logger.LogWarning("[LDAP] Skipping invalid entry: {error}", ex.Message);
|
||||
continue;
|
||||
}
|
||||
var request = new SearchRequest(
|
||||
_ldapConfig.SearchBase,
|
||||
$"({_ldapConfig.UserNameProperty}={username})",
|
||||
SearchScope.Subtree
|
||||
);
|
||||
|
||||
if (current != null)
|
||||
var response = (SearchResponse)connection.SendRequest(request);
|
||||
|
||||
foreach (SearchResultEntry current in response.Entries)
|
||||
{
|
||||
entry = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("[LDAP] Search error for user {username}: {error}", username, e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (entry == null)
|
||||
throw new LoginServicesException("LDAP User not found");
|
||||
|
||||
if (entry == null) throw new LoginServicesException("LDAP User not found");
|
||||
var userEntryLdap = GetUser(entry);
|
||||
var user = await GetOrCreateUser(userEntryLdap, entry);
|
||||
_logger.LogInformation("[LDAP] entry is {entry} and user {user}", entry, user);
|
||||
// user.Authorization.AddRange(GetAuthorities(entry));
|
||||
conn.Disconnect();
|
||||
|
||||
_logger.LogInformation("[LDAP] entry found and user {user}", user);
|
||||
|
||||
connection.Dispose();
|
||||
|
||||
return user ?? throw new LoginServicesException("LDAP User not found");
|
||||
}
|
||||
|
||||
@@ -169,9 +160,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -181,9 +170,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -192,9 +179,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -203,9 +188,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -214,9 +197,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -224,9 +205,8 @@ public class LdapLoginService : ILoginService
|
||||
/// <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");
|
||||
}
|
||||
=> 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.
|
||||
@@ -236,25 +216,23 @@ public class LdapLoginService : ILoginService
|
||||
/// <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) ??
|
||||
await _userService.Value.GetUserByName(userEntryLdap.Name)) ??
|
||||
await _userService.Value.CreateUser(userEntryLdap);
|
||||
|
||||
if (userToReturn == null) return userToReturn;
|
||||
|
||||
userToReturn.Authorization = [];
|
||||
|
||||
var authorities = await CheckAuthorities(userToReturn, entry);
|
||||
|
||||
userToReturn.Authorization.AddRange(authorities);
|
||||
//foreach (var authorization in userToReturn.Authorization)
|
||||
//{
|
||||
// Enum.TryParse<RolesType>(authorization.Rol, out var compareRole);
|
||||
// if (compareRole == RolesType.Admin) userToReturn.Rol = RolesType.Admin;
|
||||
//}
|
||||
|
||||
return userToReturn;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This method checks the authorities of a user based on the LDAP entry and the application's configuration.
|
||||
/// </summary>
|
||||
@@ -262,28 +240,26 @@ public class LdapLoginService : ILoginService
|
||||
/// <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
|
||||
{
|
||||
var authorizationMap = GetAuthoritiesMap(entry, user);
|
||||
var authorizationWhiteList = GetAuthoritiesWhiteList(entry, user);
|
||||
_logger.LogInformation(
|
||||
"[LDAP] user {user} authorizationMap count is {authorizationMap}, authorizationWhiteList count is {authorizationWhiteList}",
|
||||
user.UserName, authorizationMap.Count, authorizationWhiteList.Count);
|
||||
|
||||
// Primero, creamos un HashSet con los DisplayID de la lista blanca para búsqueda eficiente
|
||||
var whiteListDisplayIds = new HashSet<string?>(authorizationWhiteList.Select(a => a.DisplayId));
|
||||
|
||||
// Filtramos los elementos de authorizationMap que no están en la lista blanca, basándonos en DisplayID
|
||||
var uniqueMapAuthorizations = authorizationMap.Where(a => !whiteListDisplayIds.Contains(a.DisplayId));
|
||||
var uniqueMapAuthorizations = authorizationMap
|
||||
.Where(a => !whiteListDisplayIds.Contains(a.DisplayId));
|
||||
|
||||
// Finalmente, combinamos los elementos únicos de authorizationMap con los de authorizationWhiteList
|
||||
var combinedList = authorizationWhiteList.Concat(uniqueMapAuthorizations).ToList();
|
||||
|
||||
var userAuthorities = await _authorityService.GetUserAuthorities(user.Id);
|
||||
|
||||
foreach (var auth in combinedList)
|
||||
{
|
||||
var authFound = userAuthorities.Find(c => c.DisplayId == auth.DisplayId);
|
||||
|
||||
if (authFound is { CanUpdate: true })
|
||||
{
|
||||
authFound.Rol = auth.Rol;
|
||||
@@ -299,12 +275,14 @@ public class LdapLoginService : ILoginService
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("[LDAP] CheckAuthorities for user {user} has exception {ex}", user.UserName, e.Message);
|
||||
_logger.LogError("[LDAP] CheckAuthorities error for user {user}: {error}",
|
||||
user.UserName, e.Message);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This method retrieves a list of authorities for a user based on a whitelist defined in the application's configuration.
|
||||
/// </summary>
|
||||
@@ -312,29 +290,25 @@ public class LdapLoginService : ILoginService
|
||||
/// <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
|
||||
{
|
||||
var userWhiteList = new List<Authorization>();
|
||||
var userNameProperty = _ldapConfig.UserNameProperty;
|
||||
var result = new List<Authorization>();
|
||||
|
||||
var whiteList = _ldapConfig.WhiteList.FindAll(u =>
|
||||
(u.Name != null && entry.Dn.Contains(u.Name, StringComparison.CurrentCultureIgnoreCase)) ||
|
||||
(u.Name != null && entry.DistinguishedName.Contains(u.Name, StringComparison.CurrentCultureIgnoreCase)) ||
|
||||
(u.Username != null &&
|
||||
userNameProperty != null &&
|
||||
entry.GetAttributeSet().TryGetValue(userNameProperty, out var attr) &&
|
||||
attr.StringValue != null &&
|
||||
attr.StringValue.Equals(u.Username, StringComparison.CurrentCultureIgnoreCase))
|
||||
entry.Attributes[_ldapConfig.UserNameProperty]?[0]?.ToString()
|
||||
?.Equals(u.Username, StringComparison.CurrentCultureIgnoreCase) == true)
|
||||
);
|
||||
|
||||
if (whiteList.Count == 0) return userWhiteList;
|
||||
|
||||
foreach (var authorityMap in whiteList)
|
||||
{
|
||||
if (!Enum.TryParse<PermissionEnum.RolesType>(authorityMap.Rol, out _))
|
||||
continue;
|
||||
|
||||
userWhiteList.Add(new Authorization
|
||||
result.Add(new Authorization
|
||||
{
|
||||
UserId = user.Id,
|
||||
DisplayId = authorityMap.DisplayId,
|
||||
@@ -342,63 +316,49 @@ public class LdapLoginService : ILoginService
|
||||
});
|
||||
}
|
||||
|
||||
return userWhiteList;
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("[LDAP] GetAuthoritiesWhiteList for user {user} has exception {ex}", user.UserName,
|
||||
e.Message);
|
||||
|
||||
_logger.LogError("[LDAP] GetAuthoritiesWhiteList error: {error}", e.Message);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <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();
|
||||
|
||||
var user = new User();
|
||||
{
|
||||
var user = new User
|
||||
{
|
||||
UserName = entry.Attributes[_ldapConfig.UserNameProperty]?[0]?.ToString() ?? ""
|
||||
};
|
||||
|
||||
// UserName
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.UserNameProperty) &&
|
||||
attributes.TryGetValue(_ldapConfig.UserNameProperty, out var userAttr) &&
|
||||
userAttr?.StringValue != null)
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.FirstNameProperty))
|
||||
{
|
||||
user.UserName = userAttr.StringValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
user.UserName = "";
|
||||
var first = entry.Attributes[_ldapConfig.FirstNameProperty]?[0]?.ToString();
|
||||
if (first != null)
|
||||
user.Name = first;
|
||||
}
|
||||
|
||||
_logger.LogInformation("[LDAP] GetUser UserName is {UserName} ", user.UserName);
|
||||
|
||||
// First name
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.FirstNameProperty) &&
|
||||
attributes.TryGetValue(_ldapConfig.FirstNameProperty, out var firstNameAttr) &&
|
||||
firstNameAttr?.StringValue != null)
|
||||
{
|
||||
user.Name = firstNameAttr.StringValue;
|
||||
}
|
||||
|
||||
// Last name
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.LastNameProperty) &&
|
||||
attributes.TryGetValue(_ldapConfig.LastNameProperty, out var lastNameAttr) &&
|
||||
lastNameAttr?.StringValue != null)
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.LastNameProperty))
|
||||
{
|
||||
var last = entry.Attributes[_ldapConfig.LastNameProperty]?[0]?.ToString();
|
||||
if (last != null)
|
||||
user.Name = string.IsNullOrEmpty(user.Name)
|
||||
? lastNameAttr.StringValue
|
||||
: $"{user.Name} {lastNameAttr.StringValue}";
|
||||
? last
|
||||
: $"{user.Name} {last}";
|
||||
}
|
||||
|
||||
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>
|
||||
@@ -406,6 +366,7 @@ public class LdapLoginService : ILoginService
|
||||
/// <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
|
||||
{
|
||||
@@ -414,16 +375,14 @@ public class LdapLoginService : ILoginService
|
||||
if (!_ldapConfig.AuthoritiesMap.Any())
|
||||
return authorities;
|
||||
|
||||
var attributes = ldapEntry.GetAttributeSet();
|
||||
var groupAttr = entry.Attributes[_ldapConfig.GroupsProperty];
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(_ldapConfig.GroupsProperty) ||
|
||||
!attributes.TryGetValue(_ldapConfig.GroupsProperty!, out var groupsAttr) ||
|
||||
groupsAttr?.StringValueArray == null)
|
||||
{
|
||||
if (groupAttr == null)
|
||||
return authorities;
|
||||
}
|
||||
|
||||
var groups = groupsAttr.StringValueArray.ToList();
|
||||
var groups = groupAttr.GetValues(typeof(string))
|
||||
.Cast<string>()
|
||||
.ToList();
|
||||
|
||||
foreach (var authorityMap in _ldapConfig.AuthoritiesMap)
|
||||
{
|
||||
@@ -446,7 +405,7 @@ public class LdapLoginService : ILoginService
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error getting Authorities Map. Return new empty list. Exception: {e}", e);
|
||||
_logger.LogError("[LDAP] GetAuthoritiesMap error: {error}", e.Message);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,12 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="FluentValidation" Version="12.1.1" />
|
||||
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.11" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.DirectoryServices.Protocols" Version="10.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.11" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -21,14 +21,18 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="8.0.28" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
|
||||
<PackageReference Include="Microsoft.QualityTools.Testing.Fakes" Version="18.1.1" />
|
||||
<PackageReference Include="Mongo2Go" Version="4.1.0" />
|
||||
<PackageReference Include="MongoMigrations.Core" Version="4.0.15" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="4.6.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.13.0">
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.14.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@@ -14,6 +14,10 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@@ -15,11 +15,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.8" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="SharpCompress" Version="0.49.0" />
|
||||
<PackageReference Include="Snappier" Version="1.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -21,7 +21,7 @@ using adas_core.WebSocket;
|
||||
using adas_core.WebSocket.Hubs;
|
||||
using audit_logs.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Microsoft.OpenApi;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Newtonsoft.Json;
|
||||
@@ -120,7 +120,7 @@ builder.Services.AddSwaggerGen(c =>
|
||||
|
||||
c.MapType<ObjectId>(() => new OpenApiSchema
|
||||
{
|
||||
Type = "string",
|
||||
Type = JsonSchemaType.String,
|
||||
Format = "objectid"
|
||||
});
|
||||
});
|
||||
|
||||
+11
-14
@@ -24,15 +24,17 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="EasyNetQ" Version="8.1.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="5.3.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.27" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Protocols" Version="8.18.0" />
|
||||
<PackageReference Include="EasyNetQ" Version="8.1.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.28" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="8.0.28" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Protocols" Version="8.19.1" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.9.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
|
||||
<PackageReference Include="Quartz" Version="3.18.1" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Dynamic" Version="1.0.9" />
|
||||
@@ -42,18 +44,13 @@
|
||||
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
|
||||
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="10.0.8" />
|
||||
<PackageReference Include="System.Management" Version="10.0.8" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
|
||||
<PackageReference Include="WebPush" Version="1.0.13" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.27" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="10.2.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.28" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
//"ConnectionString": "mongodb://smartuci:!H12o2020@localhost:27017/?authSource=smartuci",
|
||||
//"ConnectionString": "mongodb://admin:4q*pHca@10.0.20.78:22007",
|
||||
//"ConnectionString": "mongodb://smacsuci:2(R*aQpu2r@sumo.julianrojas.xyz:27017/?authSource=admin",
|
||||
"DatabaseName": "adasDevNewStandard"
|
||||
"DatabaseName": "adas"
|
||||
},
|
||||
"DatabaseConfigurationAudit": {
|
||||
"ConnectionString": "mongodb://smacsuci:2(R*aQpu2r@julianrojas.xyz:27017/?authSource=admin",
|
||||
|
||||
Reference in New Issue
Block a user