Files
DiunaBI/DiunaBI.API/Controllers/AuthController.cs
Michał Zieliński 595076033b
All checks were successful
Build Docker Images / test (map[name:Morska plugin_project:DiunaBI.Plugins.Morska]) (push) Successful in 1m25s
Build Docker Images / test (map[name:PedrolloPL plugin_project:DiunaBI.Plugins.PedrolloPL]) (push) Successful in 1m25s
Build Docker Images / build-and-push (map[image_suffix:morska name:Morska plugin_project:DiunaBI.Plugins.Morska]) (push) Successful in 1m41s
Build Docker Images / build-and-push (map[image_suffix:pedrollopl name:PedrolloPL plugin_project:DiunaBI.Plugins.PedrolloPL]) (push) Successful in 1m40s
More security!
2025-12-05 23:41:56 +01:00

55 lines
1.7 KiB
C#

using DiunaBI.API.Services;
using DiunaBI.Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
namespace DiunaBI.API.Controllers;
[AllowAnonymous]
[ApiController]
[Route("[controller]")]
public class AuthController(
GoogleAuthService googleAuthService,
JwtTokenService jwtTokenService,
ILogger<AuthController> logger)
: ControllerBase
{
[HttpPost("apiToken")]
[EnableRateLimiting("auth")]
public async Task<IActionResult> ApiToken([FromBody] string idToken)
{
try
{
if (string.IsNullOrEmpty(idToken))
{
logger.LogWarning("Empty idToken received");
return BadRequest("IdToken is required");
}
var (isValid, user, error) = await googleAuthService.ValidateGoogleTokenAsync(idToken);
if (!isValid || user == null)
{
logger.LogWarning("Google token validation failed: {Error}", error);
return Unauthorized();
}
var jwt = jwtTokenService.GenerateToken(user);
logger.LogInformation("User authenticated successfully: {Email}", user.Email);
return Ok(new
{
token = jwt,
id = user.Id,
expirationTime = DateTime.UtcNow.AddDays(7) // z JwtSettings
});
}
catch (Exception ex)
{
logger.LogError(ex, "Error during authentication");
return StatusCode(500, "Internal server error");
}
}
}