commit 3a6c189b273ddb50382bf2950b7d49eae27cb63e
Author: Lukáš Vlček <vlcek@net-inout.cz>
Date:   Sat Apr 9 11:46:08 2022 +0200

    ClientInfo in DependencyInjection
    
    ClientInfo contains IP and info about logged user

diff --git a/Backend/Backend/Controllers/AuthController.cs b/Backend/Backend/Controllers/AuthController.cs
index 490e77a..0157a39 100644
--- a/Backend/Backend/Controllers/AuthController.cs
+++ b/Backend/Backend/Controllers/AuthController.cs
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
 using Models.Authentication;
 using RestAPI.Controllers.Common;
+using RestAPI.Utils;
 
 namespace RestAPI.Controllers;
 
@@ -48,10 +49,20 @@ public class AuthController : CommonControllerBase
     }
 
     [HttpGet("/auth/test")]
-    [ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(User))]
+    [ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(ClientInfo))]
     [ProducesResponseType((int) HttpStatusCode.Forbidden)]
-    public ActionResult<User> TestLogged([FromServices] User loggedUser)
+    public ActionResult<ClientInfo> TestLogged([FromServices] ClientInfo clientInfo)
     {
-        return loggedUser;
+        return clientInfo;
+    }
+    
+    
+    [AllowAnonymous]
+    [HttpGet("/auth/test/aa")]
+    [ProducesResponseType((int) HttpStatusCode.OK, Type = typeof(ClientInfo))]
+    [ProducesResponseType((int) HttpStatusCode.Forbidden)]
+    public ActionResult<ClientInfo> TestLoggedAA([FromServices] ClientInfo clientInfo)
+    {
+        return clientInfo;
     }
 }
\ No newline at end of file
diff --git a/Backend/Backend/Program.cs b/Backend/Backend/Program.cs
index 8f2de67..34021fb 100644
--- a/Backend/Backend/Program.cs
+++ b/Backend/Backend/Program.cs
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.OpenApi.Models;
 using RestAPI.Middleware;
+using RestAPI.Utils;
 using Serilog;
 
 var builder = WebApplication.CreateBuilder(args);
@@ -29,7 +30,7 @@ builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen(c =>
 {
     c.SwaggerDoc("v1",
-        new OpenApiInfo { Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1" });
+        new OpenApiInfo {Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1"});
 });
 
 // JWT authentication
@@ -50,8 +51,8 @@ builder.Services.AddAuthentication(options =>
     );
 
 builder.Services.AddHttpContextAccessor();
-builder.Services.AddScoped<User>(provider =>
-    ((User?)provider.GetRequiredService<IHttpContextAccessor>().HttpContext?.Items["User"]));
+builder.Services.AddScoped<ClientInfo>(provider =>
+    ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
 
 // Database
 builder.Services.AddDbContext<DatabaseContext>();
diff --git a/Backend/Backend/Utils/ClientInfo.cs b/Backend/Backend/Utils/ClientInfo.cs
new file mode 100644
index 0000000..7b02771
--- /dev/null
+++ b/Backend/Backend/Utils/ClientInfo.cs
@@ -0,0 +1,12 @@
+using Core.Entities;
+
+namespace RestAPI.Utils;
+
+public class ClientInfo
+{
+    public bool IsLogged => LoggedUser != null;
+
+    public User? LoggedUser { get; set; } = null;
+
+    public string IP { get; set; } = "unknown";
+}
\ No newline at end of file
diff --git a/Backend/Backend/Utils/ContextUtils.cs b/Backend/Backend/Utils/ContextUtils.cs
new file mode 100644
index 0000000..ee0ddca
--- /dev/null
+++ b/Backend/Backend/Utils/ContextUtils.cs
@@ -0,0 +1,44 @@
+using Core.Entities;
+using Serilog;
+
+namespace RestAPI.Utils;
+
+public static class ContextUtils
+{
+    private static string GetIP(HttpContext httpContext)
+    {
+        try
+        {
+            return httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown";
+        }
+        catch (Exception ex)
+        {
+            Log.Logger.Warning("Not able to GetIP(): {Exception}", ex);
+            return "unknown";
+        }
+    }
+
+    public static ClientInfo GetClientInfo(HttpContext? context)
+    {
+        var ci = new ClientInfo();
+        if (context == null)
+        {
+            return ci;
+        }
+
+        string IP = GetIP(context);
+        ci.IP = IP;
+
+        User? loggedUser = (User?) context.Items["User"];
+        if (loggedUser == null)
+        {
+            ci.LoggedUser = null;
+        }
+        else
+        {
+            ci.LoggedUser = loggedUser;
+        }
+
+        return ci;
+    }
+}
\ No newline at end of file
