Add google authentication

This commit is contained in:
Oscar Morales
2025-07-02 22:35:57 -06:00
parent 0429704cac
commit e7a3ee2389
12 changed files with 341 additions and 87 deletions

View File

@@ -3,10 +3,16 @@ using Core.Thalos.Adapters;
using Core.Thalos.Adapters.Common.Constants;
using Core.Thalos.Adapters.Contracts;
using Core.Thalos.Application.UseCases.Users.Input;
using Core.Thalos.BFF.Api.Services;
using Core.Thalos.External.Clients.Thalos.Requests.Users;
using Google.Apis.Auth.OAuth2;
using LSA.Dashboard.External.Clients.Dashboard;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Text.Json;
namespace Core.Thalos.BFF.Api.Controllers
{
@@ -18,8 +24,23 @@ namespace Core.Thalos.BFF.Api.Controllers
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class AuthenticationController(IThalosServiceClient thalosServiceClient, ILogger<AuthenticationController> logger, ITokenService tokenService) : BaseController(logger)
public class AuthenticationController(
IThalosServiceClient thalosServiceClient,
ILogger<AuthenticationController> logger,
ITokenService tokenService,
IGoogleAuthorization googleAuthorization) : BaseController(logger)
{
[HttpGet]
public IActionResult Authorize() => Ok(googleAuthorization.GetAuthorizationUrl());
[HttpGet("callback")]
public async Task<IActionResult> Callback(string code)
{
var userCredential = await googleAuthorization.ExchangeCodeForToken(code);
return Ok(new Token(userCredential!.Token.IdToken));
}
/// <summary>
/// Get token for user.
/// </summary>
@@ -30,7 +51,7 @@ namespace Core.Thalos.BFF.Api.Controllers
[HttpGet]
[Route(Routes.GenerateToken)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.AzureScheme)]
[Authorize]
public async Task<IActionResult> GenerateTokenService(CancellationToken cancellationToken)
{
try