Compare commits
3 Commits
59e8c6c0c6
...
b88fd78367
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b88fd78367 | ||
|
|
7670f2119e | ||
|
|
a142196d87 |
@@ -1,158 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using DigitalData.UserManager.Application.Contracts;
|
|
||||||
using DigitalData.UserManager.Application.DTOs.User;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using DigitalData.UserManager.Application;
|
|
||||||
using DigitalData.UserManager.Application.DTOs.Auth;
|
|
||||||
using DigitalData.Core.Abstractions.Application;
|
|
||||||
using Microsoft.Extensions.Localization;
|
|
||||||
using DigitalData.Core.DTO;
|
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
|
||||||
{
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
public class AuthController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly IUserService _userService;
|
|
||||||
private readonly IGroupOfUserService _gouService;
|
|
||||||
private readonly IDirectorySearchService _dirSearchService;
|
|
||||||
private readonly IStringLocalizer<Resource> _localizer;
|
|
||||||
private readonly ILogger<AuthController> _logger;
|
|
||||||
private readonly IConfiguration _config;
|
|
||||||
public AuthController(IUserService userService, IGroupOfUserService gouService, IDirectorySearchService directorySearchService, IStringLocalizer<Resource> localizer, ILogger<AuthController> logger, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
_userService = userService;
|
|
||||||
_gouService = gouService;
|
|
||||||
_dirSearchService = directorySearchService;
|
|
||||||
_localizer = localizer;
|
|
||||||
_logger = logger;
|
|
||||||
_config = configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
[AllowAnonymous]
|
|
||||||
[HttpGet("check")]
|
|
||||||
public IActionResult CheckAuthentication()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Ok(User.Identity?.IsAuthenticated ?? false);
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[AllowAnonymous]
|
|
||||||
[HttpPost("login")]
|
|
||||||
public async Task<IActionResult> Login([FromBody] LogInDto login)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
bool isValid = _dirSearchService.ValidateCredentials(login.Username, login.Password);
|
|
||||||
|
|
||||||
if (!isValid)
|
|
||||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UserNotFound]));
|
|
||||||
|
|
||||||
var allowedGroupName = _config.GetSection("AllowedGroupName").Get<string>()
|
|
||||||
?? throw new InvalidOperationException("Allowed group names configuration is missing.");
|
|
||||||
|
|
||||||
var gouMsg = await _gouService.HasGroup(login.Username, allowedGroupName, caseSensitive: false);
|
|
||||||
if (!gouMsg.IsSuccess)
|
|
||||||
return Unauthorized(Result.Fail().Message(_localizer[Key.UnauthorizedUser]));
|
|
||||||
|
|
||||||
//find the user
|
|
||||||
var uRes = await _userService.ReadByUsernameAsync(login.Username);
|
|
||||||
if (!uRes.IsSuccess || uRes.Data is null)
|
|
||||||
{
|
|
||||||
return Unauthorized(uRes);
|
|
||||||
}
|
|
||||||
|
|
||||||
UserReadDto user = uRes.Data;
|
|
||||||
|
|
||||||
// Create claims
|
|
||||||
var claims = new List<Claim>
|
|
||||||
{
|
|
||||||
new (ClaimTypes.NameIdentifier, user.Id.ToString()),
|
|
||||||
new (ClaimTypes.Name, user.Username),
|
|
||||||
new (ClaimTypes.Surname, user.Name ?? ""),
|
|
||||||
new (ClaimTypes.GivenName, user.Prename ?? ""),
|
|
||||||
new (ClaimTypes.Email, user.Email ?? ""),
|
|
||||||
new (ClaimTypes.Role, "PM_USER")
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create claimsIdentity
|
|
||||||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
|
|
||||||
// Create authProperties
|
|
||||||
var authProperties = new AuthenticationProperties
|
|
||||||
{
|
|
||||||
IsPersistent = true,
|
|
||||||
AllowRefresh = true,
|
|
||||||
ExpiresUtc = DateTime.UtcNow.AddMinutes(60)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Sign in
|
|
||||||
await HttpContext.SignInAsync(
|
|
||||||
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
||||||
new ClaimsPrincipal(claimsIdentity),
|
|
||||||
authProperties);
|
|
||||||
|
|
||||||
_dirSearchService.SetSearchRootCache(user.Username, login.Password);
|
|
||||||
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize]
|
|
||||||
[HttpGet("user")]
|
|
||||||
public async Task<IActionResult> GetUserWithClaims()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Extract the username from the Name claim.
|
|
||||||
string? username = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(username))
|
|
||||||
return Unauthorized();
|
|
||||||
|
|
||||||
return await _userService.ReadByUsernameAsync(username)
|
|
||||||
.ThenAsync(Ok, IActionResult (m, n) =>
|
|
||||||
{
|
|
||||||
_logger.LogNotice(n);
|
|
||||||
return NotFound(Result.Fail().Message(_localizer[Key.UserNotFound]));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize]
|
|
||||||
[HttpPost("logout")]
|
|
||||||
public async Task<IActionResult> Logout()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
catch(Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,11 +7,11 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class BaseAuthController<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TBaseEntity> : CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TBaseEntity, int>
|
public class BaseAuthController<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TBaseEntity> : CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TBaseEntity, int>
|
||||||
where TCRUDService : IBaseService<TCreateDto, TReadDto, TUpdateDto, TBaseEntity>
|
where TCRUDService : IBaseService<TCreateDto, TReadDto, TBaseEntity>
|
||||||
where TCreateDto : BaseCreateDto
|
where TCreateDto : BaseCreateDto
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TUpdateDto : BaseUpdateDto
|
where TUpdateDto : BaseUpdateDto
|
||||||
@@ -44,4 +44,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -10,8 +10,8 @@ using Microsoft.Extensions.Localization;
|
|||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
@@ -232,4 +232,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
get => (HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value);
|
get => (HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
using DigitalData.UserManager.Application.Services;
|
using DigitalData.UserManager.Application.Services;
|
||||||
using DigitalData.UserManager.Application.Services.Options;
|
using DigitalData.UserManager.Application.Services.Options;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class EncryptionController : ControllerBase
|
public class EncryptionController : ControllerBase
|
||||||
@@ -43,4 +42,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
return Ok(param);
|
return Ok(param);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -5,8 +5,8 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class GroupController : BaseAuthController<IGroupService, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group>
|
public class GroupController : BaseAuthController<IGroupService, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group>
|
||||||
{
|
{
|
||||||
@@ -40,4 +40,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -5,8 +5,8 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class GroupOfUserController : BaseAuthController<IGroupOfUserService, GroupOfUserCreateDto, GroupOfUserReadDto, GroupOfUserUpdateDto, GroupOfUser>
|
public class GroupOfUserController : BaseAuthController<IGroupOfUserService, GroupOfUserCreateDto, GroupOfUserReadDto, GroupOfUserUpdateDto, GroupOfUser>
|
||||||
{
|
{
|
||||||
@@ -78,4 +78,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -4,8 +4,8 @@ using DigitalData.UserManager.Application.DTOs.Module;
|
|||||||
using DigitalData.UserManager.Domain.Entities;
|
using DigitalData.UserManager.Domain.Entities;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class ModuleController : ReadControllerBaseWithErrorHandling<IModuleService, ModuleDto, Module, int>
|
public class ModuleController : ReadControllerBaseWithErrorHandling<IModuleService, ModuleDto, Module, int>
|
||||||
{
|
{
|
||||||
@@ -13,4 +13,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -6,8 +6,8 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class ModuleOfUserController : CRUDControllerBaseWithErrorHandling<IModuleOfUserService, ModuleOfUserCreateDto, ModuleOfUserReadDto, ModuleOfUserUpdateDto, ModuleOfUser, int>
|
public class ModuleOfUserController : CRUDControllerBaseWithErrorHandling<IModuleOfUserService, ModuleOfUserCreateDto, ModuleOfUserReadDto, ModuleOfUserUpdateDto, ModuleOfUser, int>
|
||||||
{
|
{
|
||||||
@@ -50,4 +50,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using DigitalData.UserManager.Application.DTOs.Auth;
|
||||||
|
|
||||||
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
|
|
||||||
|
[Route("api/Auth")]
|
||||||
|
[ApiController]
|
||||||
|
[Tags("Auth")]
|
||||||
|
public class PlaceholderAuthController : ControllerBase
|
||||||
|
{
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpGet("check")]
|
||||||
|
public IActionResult CheckAuthentication() => throw new NotImplementedException();
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpPost("login")]
|
||||||
|
public Task<IActionResult> Login([FromBody] LogInDto login) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("user")]
|
||||||
|
public Task<IActionResult> GetUserWithClaims() => throw new NotImplementedException();
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPost("logout")]
|
||||||
|
public Task<IActionResult> Logout() => throw new NotImplementedException();
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using DigitalData.Core.API;
|
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using DigitalData.UserManager.Application.Contracts;
|
using DigitalData.UserManager.Application.Contracts;
|
||||||
using DigitalData.UserManager.Application.DTOs.User;
|
using DigitalData.UserManager.Application.DTOs.User;
|
||||||
@@ -6,8 +5,8 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class UserController : BaseAuthController<IUserService, UserCreateDto, UserReadDto, UserUpdateDto, User>
|
public class UserController : BaseAuthController<IUserService, UserCreateDto, UserReadDto, UserUpdateDto, User>
|
||||||
{
|
{
|
||||||
@@ -97,4 +96,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -5,8 +5,8 @@ using DigitalData.UserManager.Domain.Entities;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DigitalData.UserManager.API.Controllers
|
namespace DigitalData.UserManager.API.Controllers;
|
||||||
{
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class UserRepController : BaseAuthController<IUserRepService, UserRepCreateDto, UserRepReadDto, UserRepUpdateDto, UserRep>
|
public class UserRepController : BaseAuthController<IUserRepService, UserRepCreateDto, UserRepReadDto, UserRepUpdateDto, UserRep>
|
||||||
{
|
{
|
||||||
@@ -39,4 +39,3 @@ namespace DigitalData.UserManager.API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -20,7 +20,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DigitalData.Core.API" Version="2.0.0" />
|
<PackageReference Include="DigitalData.Auth.Client" Version="1.3.3" />
|
||||||
|
<PackageReference Include="DigitalData.Core.API" Version="2.1.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.20" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.20" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ try {
|
|||||||
builder.Services.AddUserManager<UserManagerDbContext>();
|
builder.Services.AddUserManager<UserManagerDbContext>();
|
||||||
|
|
||||||
builder.ConfigureBySection<DirectorySearchOptions>();
|
builder.ConfigureBySection<DirectorySearchOptions>();
|
||||||
builder.Services.AddDirectorySearchService();
|
builder.Services.AddDirectorySearchService(config.GetSection("DirectorySearchOptions"));
|
||||||
|
|
||||||
builder.Services.AddCookieBasedLocalizer();
|
builder.Services.AddCookieBasedLocalizer();
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||||
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.1.0" />
|
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.4.0" />
|
||||||
<PackageReference Include="DigitalData.Core.Application" Version="3.0.1" />
|
<PackageReference Include="DigitalData.Core.Application" Version="3.2.0" />
|
||||||
<PackageReference Include="DigitalData.Core.DTO" Version="2.0.1" />
|
<PackageReference Include="DigitalData.Core.DTO" Version="2.0.1" />
|
||||||
<PackageReference Include="DigitalData.EmailProfilerDispatcher.Abstraction" Version="2.0.0" />
|
<PackageReference Include="DigitalData.EmailProfilerDispatcher.Abstraction" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.1.0" />
|
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user