зеркало из https://github.com/Azure/azure-saas.git
Merge pull request #255 from 1iveowl/main
Leverage C# 12 language improvements
This commit is contained in:
Коммит
680f52bf2d
|
@ -12,7 +12,7 @@ public class NewTenantRequest
|
|||
|
||||
internal Tenant ToTenant()
|
||||
{
|
||||
Tenant tenant = new Tenant()
|
||||
Tenant tenant = new()
|
||||
{
|
||||
Name = Name,
|
||||
Route = Route,
|
||||
|
|
|
@ -33,7 +33,7 @@ public class TenantDTO
|
|||
|
||||
public Tenant ToTenant()
|
||||
{
|
||||
Tenant tenant = new Tenant()
|
||||
Tenant tenant = new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
|
@ -68,16 +68,9 @@ public class TenantDTO
|
|||
public string? Version { get; set; }
|
||||
}
|
||||
|
||||
public class TenantDTOPage
|
||||
public class TenantDTOPage(IEnumerable<TenantDTO> tenants, int totalCount, int startIndex)
|
||||
{
|
||||
public TenantDTOPage(IEnumerable<TenantDTO> tenants, int totalCount, int startIndex)
|
||||
{
|
||||
Tenants = tenants;
|
||||
TotalCount = totalCount;
|
||||
StartIndex = startIndex;
|
||||
}
|
||||
|
||||
public IEnumerable<TenantDTO> Tenants { get; }
|
||||
public int TotalCount { get; }
|
||||
public int StartIndex { get; }
|
||||
public IEnumerable<TenantDTO> Tenants { get; } = tenants;
|
||||
public int TotalCount { get; } = totalCount;
|
||||
public int StartIndex { get; } = startIndex;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class TenantInfoDTO
|
|||
|
||||
public Tenant ToTenant()
|
||||
{
|
||||
Tenant tenant = new Tenant()
|
||||
Tenant tenant = new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
|
|
|
@ -9,6 +9,7 @@ public class Tenant
|
|||
public int CategoryId { get; set; }
|
||||
public string CreatorEmail { get; set; } = string.Empty;
|
||||
public DateTime? CreatedTime { get; set; }
|
||||
|
||||
[Timestamp]
|
||||
public byte[]? ConcurrencyToken { get; set; }
|
||||
}
|
||||
|
|
|
@ -13,8 +13,4 @@ public class ItemNotFoundExcepton : Exception
|
|||
public ItemNotFoundExcepton(string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected ItemNotFoundExcepton(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ public interface ITenantService
|
|||
Task<IEnumerable<TenantDTO>> GetAllTenantsAsync();
|
||||
|
||||
Task<TenantDTO> GetTenantAsync(Guid tenantId);
|
||||
|
||||
Task<IEnumerable<TenantDTO>> GetTenantsByIdAsync(IEnumerable<Guid> ids);
|
||||
|
||||
Task<TenantDTO> AddTenantAsync(NewTenantRequest newTenantRequest, Guid adminId);
|
||||
|
@ -16,6 +17,8 @@ public interface ITenantService
|
|||
Task DeleteTenantAsync(Guid tenantId);
|
||||
|
||||
Task<TenantInfoDTO> GetTenantInfoByRouteAsync(string route);
|
||||
|
||||
Task<bool> TenantExistsAsync(Guid tenantId);
|
||||
|
||||
Task<bool> CheckPathExists(string path);
|
||||
}
|
||||
|
|
|
@ -4,18 +4,11 @@ using Saas.Permissions.Client;
|
|||
|
||||
namespace Saas.Admin.Service.Services;
|
||||
|
||||
public class TenantService : ITenantService
|
||||
public class TenantService(TenantsContext tenantContext, IPermissionsServiceClient permissionService, ILogger<TenantService> logger) : ITenantService
|
||||
{
|
||||
private readonly TenantsContext _context;
|
||||
private readonly IPermissionsServiceClient _permissionService;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public TenantService(TenantsContext tenantContext, IPermissionsServiceClient permissionService, ILogger<TenantService> logger)
|
||||
{
|
||||
_context = tenantContext;
|
||||
_permissionService = permissionService;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly TenantsContext _context = tenantContext;
|
||||
private readonly IPermissionsServiceClient _permissionService = permissionService;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public async Task<IEnumerable<TenantDTO>> GetAllTenantsAsync()
|
||||
{
|
||||
|
|
|
@ -5,14 +5,9 @@ namespace Saas.Admin.Service.Utilities;
|
|||
|
||||
// This is to use key name prefixes to only load in the secrets that pertain to this microservice
|
||||
// https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-6.0#use-a-key-name-prefix
|
||||
public class CustomPrefixKeyVaultSecretManager : KeyVaultSecretManager
|
||||
public class CustomPrefixKeyVaultSecretManager(string prefix) : KeyVaultSecretManager
|
||||
{
|
||||
private readonly string _prefix;
|
||||
|
||||
public CustomPrefixKeyVaultSecretManager(string prefix)
|
||||
{
|
||||
_prefix = $"{prefix}-";
|
||||
}
|
||||
private readonly string _prefix = $"{prefix}-";
|
||||
|
||||
public override bool Load(SecretProperties properties)
|
||||
{
|
||||
|
|
|
@ -15,7 +15,7 @@ public static class AppHttpContext
|
|||
}
|
||||
set
|
||||
{
|
||||
if (services != null)
|
||||
if (services is not null)
|
||||
{
|
||||
throw new Exception("Can't set once a value has already been set.");
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public static class AppHttpContext
|
|||
{
|
||||
get
|
||||
{
|
||||
if(services != null)
|
||||
if(services is not null)
|
||||
{
|
||||
IHttpContextAccessor? httpContextAccessor = services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
|
||||
|
||||
|
|
|
@ -5,16 +5,10 @@ namespace Saas.Permissions.Service.Controllers;
|
|||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class CustomClaimsController : ControllerBase
|
||||
public class CustomClaimsController(IPermissionsService permissionsService, ILogger<CustomClaimsController> logger) : ControllerBase
|
||||
{
|
||||
private readonly IPermissionsService _permissionsService;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public CustomClaimsController(IPermissionsService permissionsService, ILogger<CustomClaimsController> logger)
|
||||
{
|
||||
_permissionsService = permissionsService;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly IPermissionsService _permissionsService = permissionsService;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
// This is the endpoint that is called by Azure AD B2C to get alle the custom claims defined for a specific user.
|
||||
[HttpPost("permissions")]
|
||||
|
@ -74,7 +68,7 @@ public class CustomClaimsController : ControllerBase
|
|||
|
||||
RolesClaimResponse response = new()
|
||||
{
|
||||
Roles = Array.Empty<string>()
|
||||
Roles = []
|
||||
};
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
|
|
@ -6,19 +6,14 @@ namespace Saas.Permissions.Service.Controllers;
|
|||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PermissionsController : ControllerBase
|
||||
public class PermissionsController(
|
||||
IPermissionsService permissionsService,
|
||||
IGraphAPIService graphAPIService, ILogger<PermissionsController> logger) : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
private readonly IPermissionsService _permissionsService;
|
||||
private readonly IGraphAPIService _graphAPIService;
|
||||
|
||||
public PermissionsController(IPermissionsService permissionsService, IGraphAPIService graphAPIService, ILogger<PermissionsController> logger)
|
||||
{
|
||||
_permissionsService = permissionsService;
|
||||
_graphAPIService = graphAPIService;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly IPermissionsService _permissionsService = permissionsService;
|
||||
private readonly IGraphAPIService _graphAPIService = graphAPIService;
|
||||
|
||||
[HttpGet]
|
||||
[Produces("application/json")]
|
||||
|
@ -38,7 +33,7 @@ public class PermissionsController : ControllerBase
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Unable to get Tenant Users.", ex);
|
||||
_logger.LogError("Unable to get Tenant Users: {ex}", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
|
@ -67,7 +62,7 @@ public class PermissionsController : ControllerBase
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Unhandled exception", ex);
|
||||
_logger.LogError("Unhandled exception: {ex}", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -229,7 +224,5 @@ public class PermissionsController : ControllerBase
|
|||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,13 +3,8 @@ using Saas.Permissions.Service.Data.Configuration;
|
|||
|
||||
namespace Saas.Permissions.Service.Data.Context;
|
||||
|
||||
public class SaasPermissionsContext : DbContext
|
||||
public class SaasPermissionsContext(DbContextOptions<SaasPermissionsContext> options) : DbContext(options)
|
||||
{
|
||||
public SaasPermissionsContext(DbContextOptions<SaasPermissionsContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DbSet<SaasPermission> SaasPermissions { get; set; }
|
||||
public DbSet<TenantPermission> TenantPermissions { get; set; }
|
||||
public DbSet<UserPermission> UserPermissions { get; set; }
|
||||
|
|
|
@ -13,8 +13,4 @@ public class ItemAlreadyExistsException : Exception
|
|||
public ItemAlreadyExistsException(string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected ItemAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,4 @@ public class ItemNotFoundException : Exception
|
|||
public ItemNotFoundException(string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected ItemNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,4 @@ public class UserNotFoundException : Exception
|
|||
public UserNotFoundException(string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected UserNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
//using Azure.Core;
|
||||
|
||||
//namespace Saas.Permissions.Service.Interfaces;
|
||||
|
||||
//public interface IKeyVaultCredentialService
|
||||
//{
|
||||
// TokenCredential GetCredential();
|
||||
//}
|
|
@ -4,16 +4,12 @@ using Saas.Shared.Options;
|
|||
|
||||
namespace Saas.Permissions.Service.Middleware;
|
||||
|
||||
public class ApiKeyMiddleware {
|
||||
|
||||
private readonly RequestDelegate _next;
|
||||
public class ApiKeyMiddleware(IOptions<PermissionsApiOptions> permissionOptions, RequestDelegate next)
|
||||
{
|
||||
private readonly RequestDelegate _next = next;
|
||||
private const string API_KEY = "x-api-key";
|
||||
private readonly PermissionsApiOptions _permissionOptions;
|
||||
|
||||
public ApiKeyMiddleware(IOptions<PermissionsApiOptions> permissionOptions, RequestDelegate next) {
|
||||
_next = next;
|
||||
_permissionOptions = permissionOptions.Value;
|
||||
}
|
||||
private readonly PermissionsApiOptions _permissionOptions = permissionOptions.Value;
|
||||
|
||||
public async Task InvokeAsync(HttpContext context) {
|
||||
|
||||
if (!context.Request.Headers.TryGetValue(API_KEY, out var extractedApiKey)) {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
namespace Saas.Permissions.Service.Models;
|
||||
|
||||
|
||||
public record PermissionsClaimResponse
|
||||
{
|
||||
public string[]? Permissions { get; init; }
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
namespace Saas.Permissions.Service.Models;
|
||||
|
||||
|
||||
public record RolesClaimResponse
|
||||
{
|
||||
public string[]? Roles { get; set; }
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
namespace Saas.Permissions.Service.Models;
|
||||
|
||||
|
||||
|
||||
public record UnauthorizedResponse
|
||||
{
|
||||
public UnauthorizedResponse(string _error)
|
||||
|
|
|
@ -9,9 +9,12 @@ using Saas.Shared.Options;
|
|||
|
||||
namespace Saas.Permissions.Service.Services;
|
||||
|
||||
public class GraphAPIService : IGraphAPIService
|
||||
public class GraphAPIService(
|
||||
IOptions<AzureB2CPermissionsApiOptions> permissionApiOptions,
|
||||
IGraphApiClientFactory graphClientFactory,
|
||||
ILogger<GraphAPIService> logger) : IGraphAPIService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-7.0
|
||||
private static readonly Action<ILogger, Exception> _logError = LoggerMessage.Define(
|
||||
|
@ -19,18 +22,9 @@ public class GraphAPIService : IGraphAPIService
|
|||
new EventId(1, nameof(GraphAPIService)),
|
||||
"Client Assertion Signing Provider");
|
||||
|
||||
private readonly GraphServiceClient _graphServiceClient;
|
||||
private readonly AzureB2CPermissionsApiOptions _permissionOptions;
|
||||
private readonly GraphServiceClient _graphServiceClient = graphClientFactory.Create();
|
||||
private readonly AzureB2CPermissionsApiOptions _permissionOptions = permissionApiOptions.Value;
|
||||
|
||||
public GraphAPIService(
|
||||
IOptions<AzureB2CPermissionsApiOptions> permissionApiOptions,
|
||||
IGraphApiClientFactory graphClientFactory,
|
||||
ILogger<GraphAPIService> logger)
|
||||
{
|
||||
_logger= logger;
|
||||
_graphServiceClient = graphClientFactory.Create();
|
||||
_permissionOptions = permissionApiOptions.Value;
|
||||
}
|
||||
public async Task<string[]> GetAppRolesAsync(ClaimsRequest request)
|
||||
{
|
||||
try
|
||||
|
|
|
@ -6,21 +6,14 @@ using Microsoft.Kiota.Abstractions.Authentication;
|
|||
|
||||
namespace Saas.Permissions.Service.Services;
|
||||
|
||||
public class GraphApiClientFactory : IGraphApiClientFactory
|
||||
public class GraphApiClientFactory(
|
||||
IOptions<MSGraphOptions> msGraphOptions,
|
||||
IAuthenticationProvider authenticationProvider,
|
||||
HttpClient httpClient) : IGraphApiClientFactory
|
||||
{
|
||||
private readonly IAuthenticationProvider _authenticationProvider;
|
||||
private readonly MSGraphOptions _msGraphOptions;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public GraphApiClientFactory(
|
||||
IOptions<MSGraphOptions> msGraphOptions,
|
||||
IAuthenticationProvider authenticationProvider,
|
||||
HttpClient httpClient)
|
||||
{
|
||||
_msGraphOptions = msGraphOptions.Value;
|
||||
_authenticationProvider = authenticationProvider;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
private readonly IAuthenticationProvider _authenticationProvider = authenticationProvider;
|
||||
private readonly MSGraphOptions _msGraphOptions = msGraphOptions.Value;
|
||||
private readonly HttpClient _httpClient = httpClient;
|
||||
|
||||
public GraphServiceClient Create() =>
|
||||
new(_httpClient, _authenticationProvider, _msGraphOptions.BaseUrl);
|
||||
|
|
|
@ -7,22 +7,14 @@ using Saas.Permissions.Service.Models;
|
|||
|
||||
namespace Saas.Permissions.Service.Services;
|
||||
|
||||
public class PermissionsService : IPermissionsService
|
||||
public class PermissionsService(
|
||||
SaasPermissionsContext permissionsContext,
|
||||
ILogger<PermissionsService> logger,
|
||||
IGraphAPIService graphAPIService) : IPermissionsService
|
||||
{
|
||||
private readonly SaasPermissionsContext _permissionsContext;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IGraphAPIService _graphAPIService;
|
||||
|
||||
public PermissionsService(
|
||||
SaasPermissionsContext permissionsContext,
|
||||
ILogger<PermissionsService> logger,
|
||||
IGraphAPIService graphAPIService)
|
||||
{
|
||||
_permissionsContext = permissionsContext;
|
||||
|
||||
_logger = logger;
|
||||
_graphAPIService = graphAPIService;
|
||||
}
|
||||
private readonly SaasPermissionsContext _permissionsContext = permissionsContext;
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IGraphAPIService _graphAPIService = graphAPIService;
|
||||
|
||||
public async Task<ICollection<SaasPermission>> GetPermissionsAsync(Guid userId)
|
||||
{
|
||||
|
|
|
@ -2,12 +2,7 @@
|
|||
namespace Saas.Identity.Authorization.Attribute;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class SaasRequirementAttribute : System.Attribute
|
||||
public class SaasRequirementAttribute(string name) : System.Attribute
|
||||
{
|
||||
public string PermissionEntityName { get; }
|
||||
|
||||
public SaasRequirementAttribute(string name)
|
||||
{
|
||||
PermissionEntityName = name;
|
||||
}
|
||||
public string PermissionEntityName { get; } = name;
|
||||
}
|
||||
|
|
|
@ -9,21 +9,15 @@ using System.Collections;
|
|||
using System.Security.Claims;
|
||||
|
||||
namespace Saas.Identity.Authorization.Handler;
|
||||
public abstract class SaasPermissionAuthorizationHandlerBase<TSaasRequirement, TSaasPermissionKind> : AuthorizationHandler<TSaasRequirement>
|
||||
public abstract class SaasPermissionAuthorizationHandlerBase<TSaasRequirement, TSaasPermissionKind>(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions) : AuthorizationHandler<TSaasRequirement>
|
||||
where TSaasRequirement : ISaasRequirement
|
||||
where TSaasPermissionKind : struct, Enum
|
||||
{
|
||||
protected readonly IHttpContextAccessor _httpContextAccessor;
|
||||
protected readonly Guid _globalEntity;
|
||||
|
||||
public SaasPermissionAuthorizationHandlerBase(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_globalEntity = saasAuthorizationOptions?.Value.Global
|
||||
protected readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
|
||||
protected readonly Guid _globalEntity = saasAuthorizationOptions?.Value.Global
|
||||
?? throw new InvalidOperationException($"Global entity guid in '{nameof(saasAuthorizationOptions)}' cannot be null and must be defined.");
|
||||
}
|
||||
|
||||
protected virtual HashSet<int> GetGrantedPermissionValues(AuthorizationHandlerContext context, TSaasRequirement requirement)
|
||||
{
|
||||
|
|
|
@ -5,11 +5,8 @@ using Saas.Identity.Authorization.Option;
|
|||
using Saas.Identity.Authorization.Requirement;
|
||||
|
||||
namespace Saas.Identity.Authorization.Handler;
|
||||
public sealed class SaasTenantPermissionAuthorizationHandler : SaasPermissionAuthorizationHandlerBase<SaasTenantPermissionRequirement, TenantPermissionKind>
|
||||
public sealed class SaasTenantPermissionAuthorizationHandler(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions) : SaasPermissionAuthorizationHandlerBase<SaasTenantPermissionRequirement, TenantPermissionKind>(httpContextAccessor, saasAuthorizationOptions)
|
||||
{
|
||||
public SaasTenantPermissionAuthorizationHandler(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions) : base(httpContextAccessor, saasAuthorizationOptions)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,14 +8,11 @@ using Saas.Identity.Authorization.Requirement;
|
|||
using System.Security.Claims;
|
||||
|
||||
namespace Saas.Identity.Authorization.Handler;
|
||||
public sealed class SaasUserPermissionAuthorizationHandler : SaasPermissionAuthorizationHandlerBase<SaasUserPermissionRequirement, UserPermissionKind>
|
||||
public sealed class SaasUserPermissionAuthorizationHandler(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions)
|
||||
: SaasPermissionAuthorizationHandlerBase<SaasUserPermissionRequirement, UserPermissionKind>(httpContextAccessor, saasAuthorizationOptions)
|
||||
{
|
||||
public SaasUserPermissionAuthorizationHandler(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IOptions<SaasAuthorizationOptions> saasAuthorizationOptions) : base(httpContextAccessor, saasAuthorizationOptions)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool IsValidPermission(
|
||||
SaasPermissionClaim<UserPermissionKind> permission,
|
||||
AuthorizationHandlerContext context,
|
||||
|
|
|
@ -6,14 +6,9 @@ using Saas.Identity.Authorization.Requirement;
|
|||
using System.Reflection;
|
||||
|
||||
namespace Saas.Identity.Authorization.Provider;
|
||||
public class SaasPermissionAuthorizationPolicyProvider : DefaultAuthorizationPolicyProvider
|
||||
public class SaasPermissionAuthorizationPolicyProvider(
|
||||
IOptions<AuthorizationOptions> options) : DefaultAuthorizationPolicyProvider(options)
|
||||
{
|
||||
public SaasPermissionAuthorizationPolicyProvider(
|
||||
IOptions<AuthorizationOptions> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override async Task<AuthorizationPolicy?> GetPolicyAsync(string policyName)
|
||||
{
|
||||
AuthorizationPolicy? policy = await base.GetPolicyAsync(policyName);
|
||||
|
|
|
@ -14,9 +14,12 @@ using Saas.Identity.Model;
|
|||
using Saas.Identity.Crypto.Util;
|
||||
|
||||
namespace Saas.Identity.Crypto;
|
||||
public class ClientAssertionSigningProvider : IClientAssertionSigningProvider
|
||||
public class ClientAssertionSigningProvider(
|
||||
IMemoryCache menoryCache,
|
||||
ILogger<ClientAssertionSigningProvider> logger,
|
||||
IPublicX509CertificateDetailProvider publicX509CertificateDetailProvider) : IClientAssertionSigningProvider
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-7.0
|
||||
private static readonly Action<ILogger, Exception> _logError = LoggerMessage.Define(
|
||||
|
@ -24,19 +27,8 @@ public class ClientAssertionSigningProvider : IClientAssertionSigningProvider
|
|||
new EventId(1, nameof(ClientAssertionSigningProvider)),
|
||||
"Client Assertion Signing Provider");
|
||||
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private readonly IPublicX509CertificateDetailProvider _publicX509CertificateDetailProvider;
|
||||
|
||||
public ClientAssertionSigningProvider(
|
||||
IMemoryCache menoryCache,
|
||||
ILogger<ClientAssertionSigningProvider> logger,
|
||||
IPublicX509CertificateDetailProvider publicX509CertificateDetailProvider)
|
||||
{
|
||||
_logger = logger;
|
||||
_memoryCache = menoryCache;
|
||||
|
||||
_publicX509CertificateDetailProvider = publicX509CertificateDetailProvider;
|
||||
}
|
||||
private readonly IMemoryCache _memoryCache = menoryCache;
|
||||
private readonly IPublicX509CertificateDetailProvider _publicX509CertificateDetailProvider = publicX509CertificateDetailProvider;
|
||||
|
||||
public async Task<string> GetClientAssertion(string keyVaultUrl,
|
||||
string certKeyName,
|
||||
|
@ -63,7 +55,7 @@ public class ClientAssertionSigningProvider : IClientAssertionSigningProvider
|
|||
if (_memoryCache.TryGetValue<string>(cacheItemName, out var clientAssertion)
|
||||
&& clientAssertion is not null)
|
||||
{
|
||||
_logger.LogInformation($"Cache item found.", cacheItemName);
|
||||
_logger.LogInformation("Cache item found: {cacheItemName}", cacheItemName);
|
||||
return clientAssertion;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,9 +9,11 @@ using Saas.Identity.Model;
|
|||
using Saas.Identity.Crypto.Util;
|
||||
|
||||
namespace Saas.Identity.Crypto;
|
||||
public class PublicX509CertificateDetailProvider : IPublicX509CertificateDetailProvider
|
||||
public class PublicX509CertificateDetailProvider(
|
||||
IMemoryCache memoryCache,
|
||||
ILogger<PublicX509CertificateDetailProvider> logger) : IPublicX509CertificateDetailProvider
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-7.0
|
||||
private static readonly Action<ILogger, Exception> _logError = LoggerMessage.Define(
|
||||
|
@ -19,15 +21,7 @@ public class PublicX509CertificateDetailProvider : IPublicX509CertificateDetailP
|
|||
new EventId(1, nameof(PublicX509CertificateDetailProvider)),
|
||||
"Client Assertion Signing Provider");
|
||||
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
public PublicX509CertificateDetailProvider(
|
||||
IMemoryCache memoryCache,
|
||||
ILogger<PublicX509CertificateDetailProvider> logger)
|
||||
{
|
||||
_memoryCache = memoryCache;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly IMemoryCache _memoryCache = memoryCache;
|
||||
|
||||
public async Task<IPublicX509CertificateDetail> GetX509Detail(IKeyVaultInfo keyInfo, TokenCredential credential)
|
||||
{
|
||||
|
|
|
@ -45,16 +45,11 @@ public static partial class SaasIdentityConfigurationBuilderExtensions
|
|||
}
|
||||
}
|
||||
|
||||
public class SaasApiClientCredentialBuilder<TProvider, TOptions>
|
||||
public class SaasApiClientCredentialBuilder<TProvider, TOptions>(IServiceCollection services)
|
||||
where TProvider : ISaasApi
|
||||
where TOptions : AzureAdB2CBase
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
|
||||
public SaasApiClientCredentialBuilder(IServiceCollection services)
|
||||
{
|
||||
_services = services;
|
||||
}
|
||||
private readonly IServiceCollection _services = services;
|
||||
|
||||
public IServiceCollection AddMicrosoftGraphAuthenticationProvider()
|
||||
{
|
||||
|
|
|
@ -43,18 +43,12 @@ public static partial class SaasIdentityConfigurationBuilderExtensions
|
|||
return new SaasWebAppClientCredentialBuilder(authenticationBuilder, scopes);
|
||||
}
|
||||
|
||||
public class SaasWebAppClientCredentialBuilder
|
||||
public class SaasWebAppClientCredentialBuilder(
|
||||
MicrosoftIdentityWebAppAuthenticationBuilder authenticationBuilder,
|
||||
IEnumerable<string> scopes)
|
||||
{
|
||||
private readonly MicrosoftIdentityWebAppAuthenticationBuilder _authenticationBuilder;
|
||||
private readonly IEnumerable<string> _scopes;
|
||||
|
||||
public SaasWebAppClientCredentialBuilder(
|
||||
MicrosoftIdentityWebAppAuthenticationBuilder authenticationBuilder,
|
||||
IEnumerable<string> scopes)
|
||||
{
|
||||
_authenticationBuilder= authenticationBuilder;
|
||||
_scopes= scopes;
|
||||
}
|
||||
private readonly MicrosoftIdentityWebAppAuthenticationBuilder _authenticationBuilder = authenticationBuilder;
|
||||
private readonly IEnumerable<string> _scopes = scopes;
|
||||
|
||||
public MicrosoftIdentityAppCallsWebApiAuthenticationBuilder SaaSAppCallDownstreamApi(IEnumerable<string>? scopes = default)
|
||||
{
|
||||
|
|
|
@ -6,14 +6,9 @@ using Microsoft.Identity.Web;
|
|||
namespace Saas.Identity.Helper;
|
||||
|
||||
// For more details please see: https://github.com/AzureAD/microsoft-identity-web/issues/13#issuecomment-878528492
|
||||
public class RejectSessionCookieWhenAccountNotInCacheEvents : CookieAuthenticationEvents
|
||||
public class RejectSessionCookieWhenAccountNotInCacheEvents(IEnumerable<string> scopes) : CookieAuthenticationEvents
|
||||
{
|
||||
private readonly IEnumerable<string> _scopes;
|
||||
|
||||
public RejectSessionCookieWhenAccountNotInCacheEvents(IEnumerable<string> scopes)
|
||||
{
|
||||
_scopes = scopes;
|
||||
}
|
||||
private readonly IEnumerable<string> _scopes = scopes;
|
||||
|
||||
public async override Task ValidatePrincipal(CookieValidatePrincipalContext context)
|
||||
{
|
||||
|
|
|
@ -6,11 +6,13 @@ using Saas.Shared.Interface;
|
|||
using Saas.Shared.Options;
|
||||
|
||||
namespace Saas.Identity.Provider;
|
||||
public class SaasGraphClientCredentialsProvider<TOptions> : IAuthenticationProvider
|
||||
public class SaasGraphClientCredentialsProvider<TOptions>(
|
||||
SaasApiAuthenticationProvider<ISaasMicrosoftGraphApi, TOptions> authProvider,
|
||||
ILogger<SaasGraphClientCredentialsProvider<TOptions>> logger) : IAuthenticationProvider
|
||||
where TOptions : AzureAdB2CBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly SaasApiAuthenticationProvider<ISaasMicrosoftGraphApi, TOptions> _authProvider;
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly SaasApiAuthenticationProvider<ISaasMicrosoftGraphApi, TOptions> _authProvider = authProvider;
|
||||
|
||||
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-7.0
|
||||
private static readonly Action<ILogger, Exception> _logError = LoggerMessage.Define(
|
||||
|
@ -18,28 +20,6 @@ public class SaasGraphClientCredentialsProvider<TOptions> : IAuthenticationProvi
|
|||
new EventId(1, nameof(SaasGraphClientCredentialsProvider<TOptions>)),
|
||||
"Client Assertion Signing Provider");
|
||||
|
||||
public SaasGraphClientCredentialsProvider(
|
||||
SaasApiAuthenticationProvider<ISaasMicrosoftGraphApi, TOptions> authProvider,
|
||||
ILogger<SaasGraphClientCredentialsProvider<TOptions>> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_authProvider = authProvider;
|
||||
}
|
||||
|
||||
//public async Task AuthenticateRequestAsync(HttpRequestMessage requestMessage)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// requestMessage.Headers.Authorization =
|
||||
// new AuthenticationHeaderValue("bearer", await _authProvider.GetAccessTokenAsync());
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// _logError(_logger, ex);
|
||||
// throw;
|
||||
// }
|
||||
//}
|
||||
|
||||
public async Task AuthenticateRequestAsync(
|
||||
RequestInformation request,
|
||||
Dictionary<string, object>? additionalAuthenticationContext = null,
|
||||
|
|
|
@ -7,5 +7,4 @@ public record AdminApiOptions
|
|||
|
||||
public string? ApplicationIdUri { get; init; }
|
||||
public string[]? Scopes { get; init; }
|
||||
|
||||
}
|
||||
|
|
|
@ -7,5 +7,4 @@ public record SqlOptions
|
|||
public string? SQLAdministratorLoginName { get; init; }
|
||||
public string? TenantSQLConnectionString { get; init; }
|
||||
public string? PermissionsSQLConnectionString { get; init; }
|
||||
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче