From a97427cf468a6f65d30dc478498b357f14d032e2 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 8 Sep 2024 16:03:15 +0200 Subject: [PATCH] use RouteTemplateAuthorizationConvention from ACR --- .../RouteTemplateAuthorizationConvention.cs | 45 ------------------- src/EthernaSSO/Program.cs | 2 +- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 src/EthernaSSO/Conventions/RouteTemplateAuthorizationConvention.cs diff --git a/src/EthernaSSO/Conventions/RouteTemplateAuthorizationConvention.cs b/src/EthernaSSO/Conventions/RouteTemplateAuthorizationConvention.cs deleted file mode 100644 index aa9db32..0000000 --- a/src/EthernaSSO/Conventions/RouteTemplateAuthorizationConvention.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021-present Etherna SA -// This file is part of Etherna Sso. -// -// Etherna Sso is free software: you can redistribute it and/or modify it under the terms of the -// GNU Affero General Public License as published by the Free Software Foundation, -// either version 3 of the License, or (at your option) any later version. -// -// Etherna Sso is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License along with Etherna Sso. -// If not, see . - -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc.ApplicationModels; -using Microsoft.AspNetCore.Mvc.Authorization; -using System; -using System.Linq; - -namespace Etherna.SSOServer.Conventions -{ - public class RouteTemplateAuthorizationConvention(string routeTemplate, string policyName) - : IApplicationModelConvention - { - public void Apply(ApplicationModel application) - { - ArgumentNullException.ThrowIfNull(application, nameof(application)); - - foreach (var controller in application.Controllers) - { - var isInRouteTemplate = controller.Selectors.Any( - s => s.AttributeRouteModel?.Template?.StartsWith( - routeTemplate, - StringComparison.OrdinalIgnoreCase) ?? false); - - //give priority to authorize attribute - var hasAuthorizeAttribute = controller.Attributes.OfType().Any(); - - if (isInRouteTemplate && !hasAuthorizeAttribute) - controller.Filters.Add(new AuthorizeFilter(policyName)); - } - } - } -} \ No newline at end of file diff --git a/src/EthernaSSO/Program.cs b/src/EthernaSSO/Program.cs index 48b2e8e..af50192 100644 --- a/src/EthernaSSO/Program.cs +++ b/src/EthernaSSO/Program.cs @@ -14,6 +14,7 @@ using Duende.IdentityServer.Stores; using Duende.IdentityServer.Validation; +using Etherna.ACR.Conventions; using Etherna.ACR.Exceptions; using Etherna.ACR.Middlewares.DebugPages; using Etherna.ACR.Settings; @@ -29,7 +30,6 @@ using Etherna.SSOServer.Configs.MongODM; using Etherna.SSOServer.Configs.Swagger; using Etherna.SSOServer.Configs.SystemStore; -using Etherna.SSOServer.Conventions; using Etherna.SSOServer.Domain; using Etherna.SSOServer.Domain.Models; using Etherna.SSOServer.Extensions;