diff --git a/src/backend/server/src/Todo.Api/Controllers/AuthController.cs b/src/backend/server/src/Todo.Api/Controllers/AuthController.cs index 26079f6..013a832 100644 --- a/src/backend/server/src/Todo.Api/Controllers/AuthController.cs +++ b/src/backend/server/src/Todo.Api/Controllers/AuthController.cs @@ -1,5 +1,7 @@ using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Todo.Core.Interfaces.Persistence; @@ -25,6 +27,35 @@ namespace Todo.Api.Controllers return Ok(user); } + + [HttpGet("login")] + public async Task Login([FromQuery] string returnUrl) + { + var props = new AuthenticationProperties + { + RedirectUri = Url.Action(nameof(Callback)), + Items = + { + {"returnUrl", returnUrl} + } + }; + return Challenge(props); + } + + [HttpGet] + public async Task Callback() + { + // read external identity from the temporary cookie + var result = + await HttpContext.AuthenticateAsync("oidc"); + if (result?.Succeeded != true) + { + throw new Exception("External authentication error"); + } + + var returnUrl = result.Properties?.Items["returnUrl"] ?? "~/"; + return Redirect(returnUrl); + } public record RegisterUserRequest { diff --git a/src/backend/server/src/Todo.Api/Hubs/TodoHub.cs b/src/backend/server/src/Todo.Api/Hubs/TodoHub.cs index 9863fd2..2b8f982 100644 --- a/src/backend/server/src/Todo.Api/Hubs/TodoHub.cs +++ b/src/backend/server/src/Todo.Api/Hubs/TodoHub.cs @@ -1,10 +1,12 @@ using System.Text.Json; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using Todo.Api.Hubs.Models; using Todo.Core.Interfaces.Persistence; namespace Todo.Api.Hubs { + [Authorize] public class TodoHub : Hub { private readonly ITodoRepository _todoRepository; diff --git a/src/backend/server/src/Todo.Api/Properties/launchSettings.json b/src/backend/server/src/Todo.Api/Properties/launchSettings.json index cc48b0b..63ce39b 100644 --- a/src/backend/server/src/Todo.Api/Properties/launchSettings.json +++ b/src/backend/server/src/Todo.Api/Properties/launchSettings.json @@ -24,7 +24,7 @@ "MONGODB__Port": "27017", "GITEA__Url": "https://git.front.kjuulh.io", "GITEA__ClientId": "6982ef4f-cfc1-431c-a442-fad98355a059", - "GITEA__": "stabKPEZ6di0VfPjYT4rb0jRGLA2gPSd2NEkGoBi0xLO" + "GITEA__ClientSecret": "hXUrUz5xPhC7IE3dQKft9lHboBEwhNC8yFjSzKgF9Nyr" } } } diff --git a/src/backend/server/src/Todo.Infrastructure/DependencyInjection.cs b/src/backend/server/src/Todo.Infrastructure/DependencyInjection.cs index 785dcd4..faef2f2 100644 --- a/src/backend/server/src/Todo.Infrastructure/DependencyInjection.cs +++ b/src/backend/server/src/Todo.Infrastructure/DependencyInjection.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Todo.Infrastructure; diff --git a/src/client/src/presentation/contexts/SocketContext.tsx b/src/client/src/presentation/contexts/SocketContext.tsx index 71317ff..c6e82b6 100644 --- a/src/client/src/presentation/contexts/SocketContext.tsx +++ b/src/client/src/presentation/contexts/SocketContext.tsx @@ -70,6 +70,8 @@ export const SocketProvider: FC = (props) => { connection.start().then(() => { setConn(connection); + }).catch(e => { + window.location.href = `${serverUrl}/api/auth/login?returnUrl=${window.location.href}` }); }, []);