2025-01-01 19:24:35 -06:00
|
|
|
|
using LANCommander.Server.Models;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Services;
|
2023-01-05 01:06:30 -06:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-01-04 23:45:11 -06:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
using System.Text;
|
2025-01-03 00:55:22 -06:00
|
|
|
|
using AutoMapper;
|
2025-01-01 19:24:35 -06:00
|
|
|
|
using LANCommander.SDK.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2025-01-05 21:09:57 -06:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2025-01-01 19:24:35 -06:00
|
|
|
|
using AuthenticationService = LANCommander.Server.Services.AuthenticationService;
|
|
|
|
|
|
using User = LANCommander.Server.Data.Models.User;
|
2023-01-04 23:45:11 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Controllers.Api
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
|
|
|
|
|
public class LoginModel
|
|
|
|
|
|
{
|
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 02:31:42 -05:00
|
|
|
|
public class RegisterModel
|
|
|
|
|
|
{
|
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
|
public string Password { get; set; }
|
2025-01-05 21:09:57 -06:00
|
|
|
|
public string PasswordConfirmation { get; set; }
|
2023-03-14 02:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-04 23:45:11 -06:00
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
[ApiController]
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public class AuthController : BaseApiController
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
private readonly AuthenticationService AuthenticationService;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
private readonly UserService UserService;
|
|
|
|
|
|
private readonly RoleService RoleService;
|
2025-01-05 21:09:57 -06:00
|
|
|
|
private readonly IFusionCache Cache;
|
2025-01-03 00:55:22 -06:00
|
|
|
|
private readonly IMapper Mapper;
|
2023-01-04 23:45:11 -06:00
|
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
|
public AuthController(
|
2025-01-01 19:24:35 -06:00
|
|
|
|
AuthenticationService authenticationService,
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
UserService userService,
|
2025-01-01 19:24:35 -06:00
|
|
|
|
RoleService roleService,
|
2025-01-05 21:09:57 -06:00
|
|
|
|
IFusionCache cache,
|
2025-01-03 00:55:22 -06:00
|
|
|
|
IMapper mapper,
|
2025-01-01 19:24:35 -06:00
|
|
|
|
ILogger<AuthController> logger) : base(logger)
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
AuthenticationService = authenticationService;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
UserService = userService;
|
|
|
|
|
|
RoleService = roleService;
|
2025-01-05 21:09:57 -06:00
|
|
|
|
Cache = cache;
|
2025-01-03 00:55:22 -06:00
|
|
|
|
Mapper = mapper;
|
2023-01-04 23:45:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-01 19:24:35 -06:00
|
|
|
|
[HttpGet("Login")]
|
|
|
|
|
|
public async Task<IActionResult> LoginAsync(string provider = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(provider) && !User.Identity.IsAuthenticated)
|
|
|
|
|
|
{
|
|
|
|
|
|
var properties = new AuthenticationProperties(new Dictionary<string, string>()
|
|
|
|
|
|
{
|
|
|
|
|
|
{ "Action", AuthenticationProviderActionType.Login }
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-02 18:47:04 -06:00
|
|
|
|
properties.RedirectUri = Url.Action("Login", "Auth", new { provider = provider });
|
2025-01-01 19:24:35 -06:00
|
|
|
|
|
|
|
|
|
|
return Challenge(properties, provider);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!String.IsNullOrWhiteSpace(provider) && User.Identity.IsAuthenticated)
|
|
|
|
|
|
{
|
2025-01-05 21:09:57 -06:00
|
|
|
|
var code = Guid.NewGuid().ToString();
|
|
|
|
|
|
var token = await AuthenticationService.LoginAsync(User.Identity.Name);
|
|
|
|
|
|
|
|
|
|
|
|
await Cache.SetAsync($"AuthToken/{code}", token, TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
|
|
|
|
return Redirect($"/RedeemToken/{code}");
|
2025-01-01 19:24:35 -06:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 03:47:02 -06:00
|
|
|
|
[HttpPost("Login")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> LoginAsync([FromBody] LoginModel model)
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2023-03-14 02:31:42 -05:00
|
|
|
|
try
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
var token = await AuthenticationService.LoginAsync(model.UserName, model.Password);
|
2023-01-04 23:45:11 -06:00
|
|
|
|
|
2025-01-01 19:24:35 -06:00
|
|
|
|
Logger?.LogDebug("Successfully logged in user {UserName}", model.UserName);
|
2023-09-14 17:14:03 -05:00
|
|
|
|
|
2023-03-14 02:31:42 -05:00
|
|
|
|
return Ok(token);
|
|
|
|
|
|
}
|
2023-09-14 17:14:03 -05:00
|
|
|
|
catch (Exception ex)
|
2023-03-14 02:31:42 -05:00
|
|
|
|
{
|
2024-08-28 20:33:59 -05:00
|
|
|
|
Logger?.LogError(ex, "An error occurred while trying to log in {UserName}", model.UserName);
|
2025-01-25 13:46:33 -06:00
|
|
|
|
|
|
|
|
|
|
return Unauthorized(ex.Message);
|
2023-01-04 23:45:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-23 18:01:12 -06:00
|
|
|
|
[HttpPost("Logout")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> LogoutAsync()
|
2024-01-23 18:01:12 -06:00
|
|
|
|
{
|
|
|
|
|
|
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
await UserService.SignOut();
|
2024-01-23 18:01:12 -06:00
|
|
|
|
|
2024-09-17 00:18:39 -05:00
|
|
|
|
Logger?.LogInformation("Logged out user {UserName}", User.Identity.Name);
|
|
|
|
|
|
|
2024-01-23 18:01:12 -06:00
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-05 01:06:30 -06:00
|
|
|
|
[HttpPost("Validate")]
|
2023-01-06 02:39:00 -06:00
|
|
|
|
[Authorize(AuthenticationSchemes = "Bearer")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public IActionResult ValidateAsync()
|
2023-01-05 01:06:30 -06:00
|
|
|
|
{
|
|
|
|
|
|
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
else
|
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-04 23:45:11 -06:00
|
|
|
|
[HttpPost("Refresh")]
|
2025-01-01 19:24:35 -06:00
|
|
|
|
public async Task<IActionResult> RefreshAsync(AuthToken token)
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
try
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
return Ok(await AuthenticationService.RefreshTokenAsync(token));
|
2023-01-04 23:45:11 -06:00
|
|
|
|
}
|
2025-01-01 19:24:35 -06:00
|
|
|
|
catch (Exception ex)
|
2023-01-04 23:45:11 -06:00
|
|
|
|
{
|
2025-01-01 19:24:35 -06:00
|
|
|
|
return BadRequest(ex);
|
|
|
|
|
|
} ;
|
2023-01-04 23:45:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 02:31:42 -05:00
|
|
|
|
[HttpPost("Register")]
|
2024-11-12 23:22:01 -06:00
|
|
|
|
public async Task<IActionResult> RegisterAsync([FromBody] RegisterModel model)
|
2023-03-14 02:31:42 -05:00
|
|
|
|
{
|
2025-02-04 18:45:24 -06:00
|
|
|
|
try
|
2023-09-14 17:14:03 -05:00
|
|
|
|
{
|
2025-02-04 18:45:24 -06:00
|
|
|
|
var token = await AuthenticationService.RegisterAsync(model.UserName, model.Password,
|
|
|
|
|
|
model.PasswordConfirmation);
|
2023-09-14 17:14:03 -05:00
|
|
|
|
|
2025-02-04 18:45:24 -06:00
|
|
|
|
return Ok(token);
|
2023-09-14 17:14:03 -05:00
|
|
|
|
}
|
2025-02-04 18:45:24 -06:00
|
|
|
|
catch (Exception ex)
|
2023-03-14 02:31:42 -05:00
|
|
|
|
{
|
2025-02-04 18:45:24 -06:00
|
|
|
|
return Unauthorized(ex.Message);
|
2023-03-14 02:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-03 00:55:22 -06:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("AuthenticationProviders")]
|
|
|
|
|
|
public IActionResult GetAuthenticationProviders()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok(Mapper.Map<IEnumerable<AuthenticationProvider>>(Settings.Authentication.AuthenticationProviders));
|
|
|
|
|
|
}
|
2023-01-04 23:45:11 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|