From 45cc299866fc18decd358bfcc182957d5627832f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 17 Feb 2025 23:56:28 +0000 Subject: [PATCH] Get User Id from Web API --- .../Blogspot/FileSystemApiController.cs | 2 + .../Controllers/Blogspot/FileSystemStream.cs | 1 + .../Business/ActivityApiController.cs | 2 +- .../Controllers/Business/BillingController.cs | 1 + .../accounting/AccountController.cs | 10 +--- .../Helpers/RequestHelpers.cs | 2 +- src/Api/Helpers/UserHelpers.cs | 17 ++++++ src/Api/Program.cs | 53 ++++++++++--------- .../Validation/ValidRemoteUserDirAttribute.cs | 2 +- .../FileSystem/AbstractFileSystemHelpers.cs | 2 +- .../FileSystem/FsOperationInfo.cs | 2 +- .../FileSystem/UserDirectoryInfo.cs | 2 +- src/Yavsc.Server/Helpers/BillingHelpers.cs | 5 +- src/Yavsc.Server/Helpers/FileSystemHelpers.cs | 5 +- src/Yavsc.Server/Helpers/UserHelpers.cs | 1 - .../ViewModels/LiveCastHandler.cs | 1 + .../Contracting/EstimateController.cs | 1 + .../Contracting/FrontOfficeController.cs | 1 + src/Yavsc/Controllers/FileSystemController.cs | 1 + src/Yavsc/Extensions/HostingExtensions.cs | 2 +- src/Yavsc/Helpers/UserHelpers.cs | 10 ++++ src/Yavsc/Startup.cs | 1 + src/Yavsc/ViewComponents/BillViewComponent.cs | 1 + .../ViewComponents/DirectoryViewComponent.cs | 1 + src/Yavsc/app.config | 11 ---- src/sampleWebAsWebApiClient/Program.cs | 6 ++- 26 files changed, 83 insertions(+), 60 deletions(-) rename src/{Yavsc.Server => Api}/Helpers/RequestHelpers.cs (96%) create mode 100644 src/Api/Helpers/UserHelpers.cs create mode 100644 src/Yavsc/Helpers/UserHelpers.cs delete mode 100644 src/Yavsc/app.config diff --git a/src/Api/Controllers/Blogspot/FileSystemApiController.cs b/src/Api/Controllers/Blogspot/FileSystemApiController.cs index e7b40dfe..2dc63d59 100644 --- a/src/Api/Controllers/Blogspot/FileSystemApiController.cs +++ b/src/Api/Controllers/Blogspot/FileSystemApiController.cs @@ -14,6 +14,8 @@ namespace Yavsc.ApiControllers using Yavsc.Attributes.Validation; using System.IO; using Yavsc.Exceptions; + using Yavsc.Server.Helpers; + using Yavsc.Abstract.Helpers; [Authorize,Route("api/fs")] public partial class FileSystemApiController : Controller diff --git a/src/Api/Controllers/Blogspot/FileSystemStream.cs b/src/Api/Controllers/Blogspot/FileSystemStream.cs index af80fe7d..e6b3c159 100644 --- a/src/Api/Controllers/Blogspot/FileSystemStream.cs +++ b/src/Api/Controllers/Blogspot/FileSystemStream.cs @@ -8,6 +8,7 @@ using Yavsc.Models; using Yavsc.Models.Messaging; using Yavsc.Services; using Microsoft.AspNetCore.SignalR; +using Yavsc.Server.Helpers; namespace Yavsc.ApiControllers { diff --git a/src/Api/Controllers/Business/ActivityApiController.cs b/src/Api/Controllers/Business/ActivityApiController.cs index c661857a..709fab7a 100644 --- a/src/Api/Controllers/Business/ActivityApiController.cs +++ b/src/Api/Controllers/Business/ActivityApiController.cs @@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using Yavsc.Helpers; +using Yavsc.Api.Helpers; using Yavsc.Models; using Yavsc.Models.Workflow; diff --git a/src/Api/Controllers/Business/BillingController.cs b/src/Api/Controllers/Business/BillingController.cs index 041359a2..5ab4a332 100644 --- a/src/Api/Controllers/Business/BillingController.cs +++ b/src/Api/Controllers/Business/BillingController.cs @@ -15,6 +15,7 @@ namespace Yavsc.ApiControllers using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; using Yavsc.ViewModels.Auth; + using Yavsc.Server.Helpers; [Route("api/bill"), Authorize] public class BillingController : Controller diff --git a/src/Api/Controllers/accounting/AccountController.cs b/src/Api/Controllers/accounting/AccountController.cs index 5ae21995..c1bb811a 100644 --- a/src/Api/Controllers/accounting/AccountController.cs +++ b/src/Api/Controllers/accounting/AccountController.cs @@ -1,15 +1,11 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using System.Security.Claims; using Microsoft.EntityFrameworkCore; using Yavsc.Models; -using Yavsc.Models.Account; -using Yavsc.ViewModels.Account; -using Yavsc.Helpers; -using Yavsc.Abstract.Identity; -using System.Diagnostics; +using Yavsc.Api.Helpers; +using Yavsc.Server.Helpers; namespace Yavsc.WebApi.Controllers { @@ -27,8 +23,6 @@ namespace Yavsc.WebApi.Controllers _dbContext = dbContext; } - - [HttpGet("me")] public async Task Me() { diff --git a/src/Yavsc.Server/Helpers/RequestHelpers.cs b/src/Api/Helpers/RequestHelpers.cs similarity index 96% rename from src/Yavsc.Server/Helpers/RequestHelpers.cs rename to src/Api/Helpers/RequestHelpers.cs index 96f060c3..2c2fae27 100644 --- a/src/Yavsc.Server/Helpers/RequestHelpers.cs +++ b/src/Api/Helpers/RequestHelpers.cs @@ -8,7 +8,7 @@ using Yavsc.ViewModels; using Yavsc.Models; using System.Linq; -namespace Yavsc.Helpers +namespace Yavsc.Api.Helpers { public static class RequestHelpers { diff --git a/src/Api/Helpers/UserHelpers.cs b/src/Api/Helpers/UserHelpers.cs new file mode 100644 index 00000000..2d480ea1 --- /dev/null +++ b/src/Api/Helpers/UserHelpers.cs @@ -0,0 +1,17 @@ +using System.Security.Claims; +using System.Collections.Generic; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Yavsc.Models; +using Yavsc.Models.Blog; + +namespace Yavsc.Api.Helpers +{ + public static class UserHelpers + { + public static string GetUserId(this ClaimsPrincipal user) + { + return user.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"); + } + } +} diff --git a/src/Api/Program.cs b/src/Api/Program.cs index dd8a4606..5ef8afde 100644 --- a/src/Api/Program.cs +++ b/src/Api/Program.cs @@ -32,7 +32,7 @@ internal class Program // accepts any access token issued by identity server // adds an authorization policy for scope 'scope1' - + services .AddAuthorization(options => { @@ -40,7 +40,7 @@ internal class Program { policy .RequireAuthenticatedUser() - .RequireClaim(JwtClaimTypes.Scope, new string [] {"scope2"}); + .RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" }); }); }) .AddCors(options => @@ -63,21 +63,21 @@ internal class Program options.Authority = "https://localhost:5001"; options.TokenValidationParameters = new() { ValidateAudience = false }; - }); - - services.AddDbContext(options => - options.UseNpgsql(builder.Configuration.GetConnectionString("Default"))); - - services.AddTransient() - .AddTransient() - .AddTransient(); - /* - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient(); - services.AddIdentityApiEndpoints(); - services.AddSession(); -*/ + }); + + services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); + + services.AddTransient() + .AddTransient() + .AddTransient(); + /* + services.AddSingleton(); + services.AddSingleton(); + services.AddTransient(); + services.AddIdentityApiEndpoints(); + services.AddSession(); + */ using (var app = builder.Build()) { if (app.Environment.IsDevelopment()) @@ -88,22 +88,23 @@ internal class Program .UseAuthentication() .UseAuthorization() .UseCors("default") - /* .UseEndpoints(endpoints => - { - endpoints.MapDefaultControllerRoute() - .RequireAuthorization(); - })*/ - + /* .UseEndpoints(endpoints => + { + endpoints.MapDefaultControllerRoute() + .RequireAuthorization(); + })*/ + ; - // app.MapIdentityApi().RequireAuthorization("ApiScope"); + // app.MapIdentityApi().RequireAuthorization("ApiScope"); app.MapDefaultControllerRoute(); app.MapGet("/identity", (HttpContext context) => new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value })) ); - // app.UseSession(); + // app.UseSession(); await app.RunAsync(); - }; + } + ; diff --git a/src/Yavsc.Abstract/Attributes/Validation/ValidRemoteUserDirAttribute.cs b/src/Yavsc.Abstract/Attributes/Validation/ValidRemoteUserDirAttribute.cs index 2c59f689..43c44054 100644 --- a/src/Yavsc.Abstract/Attributes/Validation/ValidRemoteUserDirAttribute.cs +++ b/src/Yavsc.Abstract/Attributes/Validation/ValidRemoteUserDirAttribute.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -using Yavsc.Helpers; +using Yavsc.Server.Helpers; namespace Yavsc.Attributes.Validation { diff --git a/src/Yavsc.Abstract/FileSystem/AbstractFileSystemHelpers.cs b/src/Yavsc.Abstract/FileSystem/AbstractFileSystemHelpers.cs index 0b4aefbb..5e571e24 100644 --- a/src/Yavsc.Abstract/FileSystem/AbstractFileSystemHelpers.cs +++ b/src/Yavsc.Abstract/FileSystem/AbstractFileSystemHelpers.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using Yavsc.ViewModels.UserFiles; -namespace Yavsc.Helpers +namespace Yavsc.Server.Helpers { public static class AbstractFileSystemHelpers { diff --git a/src/Yavsc.Abstract/FileSystem/FsOperationInfo.cs b/src/Yavsc.Abstract/FileSystem/FsOperationInfo.cs index ba748fbd..63c69cb4 100644 --- a/src/Yavsc.Abstract/FileSystem/FsOperationInfo.cs +++ b/src/Yavsc.Abstract/FileSystem/FsOperationInfo.cs @@ -1,4 +1,4 @@ -namespace Yavsc.Helpers +namespace Yavsc.Abstract.Helpers { public enum ErrorCode { NotFound, diff --git a/src/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs b/src/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs index 77b7e6e4..cced9397 100644 --- a/src/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs +++ b/src/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs @@ -2,7 +2,7 @@ using System; using System.IO; using System.Linq; using Yavsc.Abstract.FileSystem; -using Yavsc.Helpers; +using Yavsc.Server.Helpers; namespace Yavsc.ViewModels.UserFiles { diff --git a/src/Yavsc.Server/Helpers/BillingHelpers.cs b/src/Yavsc.Server/Helpers/BillingHelpers.cs index 0a826447..c10a7615 100644 --- a/src/Yavsc.Server/Helpers/BillingHelpers.cs +++ b/src/Yavsc.Server/Helpers/BillingHelpers.cs @@ -1,10 +1,7 @@ -using System.Collections.Generic; using System.Globalization; -using System.IO; -using System.Linq; -using Yavsc.Abstract.FileSystem; using Yavsc.Billing; using Yavsc.Models.Billing; +using Yavsc.Server.Helpers; using Yavsc.Services; namespace Yavsc.Helpers diff --git a/src/Yavsc.Server/Helpers/FileSystemHelpers.cs b/src/Yavsc.Server/Helpers/FileSystemHelpers.cs index 28721815..f0d5782b 100644 --- a/src/Yavsc.Server/Helpers/FileSystemHelpers.cs +++ b/src/Yavsc.Server/Helpers/FileSystemHelpers.cs @@ -11,8 +11,9 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using Microsoft.AspNetCore.Http; using Yavsc.Exceptions; - -namespace Yavsc.Helpers +using Yavsc.Helpers; +using Yavsc.Abstract.Helpers; +namespace Yavsc.Server.Helpers { public static class FileSystemHelpers { diff --git a/src/Yavsc.Server/Helpers/UserHelpers.cs b/src/Yavsc.Server/Helpers/UserHelpers.cs index e6db8313..e319239d 100644 --- a/src/Yavsc.Server/Helpers/UserHelpers.cs +++ b/src/Yavsc.Server/Helpers/UserHelpers.cs @@ -9,7 +9,6 @@ namespace Yavsc.Helpers { public static class UserHelpers { - public static string GetUserId(this ClaimsPrincipal user) { return user.FindFirstValue("sub"); diff --git a/src/Yavsc.Server/ViewModels/LiveCastHandler.cs b/src/Yavsc.Server/ViewModels/LiveCastHandler.cs index fc71e760..e5e83506 100644 --- a/src/Yavsc.Server/ViewModels/LiveCastHandler.cs +++ b/src/Yavsc.Server/ViewModels/LiveCastHandler.cs @@ -10,6 +10,7 @@ using Newtonsoft.Json; using Yavsc.Helpers; using Yavsc.Models; using Yavsc.Models.FileSystem; +using Yavsc.Server.Helpers; namespace Yavsc.ViewModels.Streaming { diff --git a/src/Yavsc/Controllers/Contracting/EstimateController.cs b/src/Yavsc/Controllers/Contracting/EstimateController.cs index d68c3656..b81af756 100644 --- a/src/Yavsc/Controllers/Contracting/EstimateController.cs +++ b/src/Yavsc/Controllers/Contracting/EstimateController.cs @@ -12,6 +12,7 @@ namespace Yavsc.Controllers using Models.Billing; using Models.Workflow; using ViewModels.Auth; + using Yavsc.Server.Helpers; [Authorize] public class EstimateController : Controller diff --git a/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs b/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs index 55a04fb0..666fa501 100644 --- a/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs +++ b/src/Yavsc/Controllers/Contracting/FrontOfficeController.cs @@ -10,6 +10,7 @@ namespace Yavsc.Controllers using Microsoft.Extensions.Localization; using Models; using ViewModels.FrontOffice; + using Yavsc.Server.Helpers; using Yavsc.Services; public class FrontOfficeController : Controller diff --git a/src/Yavsc/Controllers/FileSystemController.cs b/src/Yavsc/Controllers/FileSystemController.cs index 36ebcb04..84579675 100644 --- a/src/Yavsc/Controllers/FileSystemController.cs +++ b/src/Yavsc/Controllers/FileSystemController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Yavsc.Helpers; +using Yavsc.Server.Helpers; namespace Yavsc.Controllers { diff --git a/src/Yavsc/Extensions/HostingExtensions.cs b/src/Yavsc/Extensions/HostingExtensions.cs index d781de86..d86f8259 100644 --- a/src/Yavsc/Extensions/HostingExtensions.cs +++ b/src/Yavsc/Extensions/HostingExtensions.cs @@ -29,6 +29,7 @@ using Yavsc.Models.Workflow; using Yavsc.Services; using Yavsc.Settings; using Yavsc.ViewModels.Auth; +using Yavsc.Server.Helpers; namespace Yavsc.Extensions; @@ -335,7 +336,6 @@ public static class HostingExtensions .AddInMemoryIdentityResources(Config.IdentityResources) .AddInMemoryClients(Config.Clients) .AddInMemoryApiScopes(Config.ApiScopes) - .AddAspNetIdentity() .AddProfileService() ; diff --git a/src/Yavsc/Helpers/UserHelpers.cs b/src/Yavsc/Helpers/UserHelpers.cs new file mode 100644 index 00000000..ade8cac0 --- /dev/null +++ b/src/Yavsc/Helpers/UserHelpers.cs @@ -0,0 +1,10 @@ +using System.Security.Claims; + +namespace Yavsc.Helpers +{ + public static class UserHelpers + { + + + } +} diff --git a/src/Yavsc/Startup.cs b/src/Yavsc/Startup.cs index 1625debf..93cf5dcf 100644 --- a/src/Yavsc/Startup.cs +++ b/src/Yavsc/Startup.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; using Yavsc.Helpers; +using Yavsc.Server.Helpers; using Yavsc.Settings; namespace Yavsc; diff --git a/src/Yavsc/ViewComponents/BillViewComponent.cs b/src/Yavsc/ViewComponents/BillViewComponent.cs index 4f7c3fc5..d0d0b9fe 100644 --- a/src/Yavsc/ViewComponents/BillViewComponent.cs +++ b/src/Yavsc/ViewComponents/BillViewComponent.cs @@ -8,6 +8,7 @@ using Yavsc.ViewModels; using Yavsc.ViewModels.Gen; using Yavsc.Services; using Microsoft.EntityFrameworkCore; +using Yavsc.Server.Helpers; namespace Yavsc.ViewComponents { diff --git a/src/Yavsc/ViewComponents/DirectoryViewComponent.cs b/src/Yavsc/ViewComponents/DirectoryViewComponent.cs index b5f5b902..af7f436b 100644 --- a/src/Yavsc/ViewComponents/DirectoryViewComponent.cs +++ b/src/Yavsc/ViewComponents/DirectoryViewComponent.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using Yavsc.Helpers; using Yavsc.Models; +using Yavsc.Server.Helpers; using Yavsc.ViewModels.UserFiles; namespace Yavsc.ViewComponents diff --git a/src/Yavsc/app.config b/src/Yavsc/app.config deleted file mode 100644 index 8460dd43..00000000 --- a/src/Yavsc/app.config +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/src/sampleWebAsWebApiClient/Program.cs b/src/sampleWebAsWebApiClient/Program.cs index 31426483..5cc25c36 100644 --- a/src/sampleWebAsWebApiClient/Program.cs +++ b/src/sampleWebAsWebApiClient/Program.cs @@ -11,6 +11,7 @@ */ using System.IdentityModel.Tokens.Jwt; +using Microsoft.AspNetCore.Authentication; JwtSecurityTokenHandler.DefaultMapInboundClaims = false; @@ -36,7 +37,10 @@ builder.Services options.Scope.Add("openid"); options.Scope.Add("profile"); options.Scope.Add("scope2"); - + options.MapInboundClaims = true; + options.ClaimActions.MapUniqueJsonKey("preferred_username","preferred_username"); + options.ClaimActions.MapUniqueJsonKey("gender", "gender"); + options.SaveTokens = true; });