From 6ddb9ea88049e3c725681c05dbdcdb453dbb43d3 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Fri, 16 Aug 2024 16:01:01 -0500 Subject: [PATCH] Formatting and copyright headers --- .../AntiforgeryHandler.cs | 8 +++- .../BffBlazorOptions.cs | 13 +++--- .../BffClientAuthenticationStateProvider.cs | 12 ++++-- src/Duende.Bff.Blazor.Client/ClaimLite.cs | 15 ++++--- .../ClaimsLiteExtensions.cs | 13 ++++-- .../ClaimsPrincipalLite.cs | 15 ++++--- .../ServiceCollectionExtensions.cs | 42 ++++++++++++------- src/Duende.Bff.Blazor/BffBuilderExtensions.cs | 3 ++ .../CaptureManagementClaimsCookieEvents.cs | 4 +- .../PersistingAuthenticationStateProvider.cs | 7 +++- src/Duende.Bff.Blazor/ServerSideTokenStore.cs | 35 ++++++++-------- 11 files changed, 105 insertions(+), 62 deletions(-) diff --git a/src/Duende.Bff.Blazor.Client/AntiforgeryHandler.cs b/src/Duende.Bff.Blazor.Client/AntiforgeryHandler.cs index b4f158e4..d29ab014 100644 --- a/src/Duende.Bff.Blazor.Client/AntiforgeryHandler.cs +++ b/src/Duende.Bff.Blazor.Client/AntiforgeryHandler.cs @@ -1,8 +1,12 @@ -namespace Duende.Bff.Blazor.Client; +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + +namespace Duende.Bff.Blazor.Client; public class AntiforgeryHandler : DelegatingHandler { - protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + protected override Task SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) { request.Headers.Add("X-CSRF", "1"); return base.SendAsync(request, cancellationToken); diff --git a/src/Duende.Bff.Blazor.Client/BffBlazorOptions.cs b/src/Duende.Bff.Blazor.Client/BffBlazorOptions.cs index bd451d9d..d9b83e4c 100644 --- a/src/Duende.Bff.Blazor.Client/BffBlazorOptions.cs +++ b/src/Duende.Bff.Blazor.Client/BffBlazorOptions.cs @@ -1,18 +1,21 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + namespace Duende.Bff.Blazor.Client; /// -/// Options for Blazor BFF +/// Options for Blazor BFF /// public class BffBlazorOptions { /// - /// The base path to use for remote APIs. + /// The base path to use for remote APIs. /// public string RemoteApiPath { get; set; } = "remote-apis/"; - + /// - /// The base address to use for remote APIs. If unset (the default), the - /// blazor hosting environment's base address is used. + /// The base address to use for remote APIs. If unset (the default), the + /// blazor hosting environment's base address is used. /// public string? RemoteApiBaseAddress { get; set; } = null; diff --git a/src/Duende.Bff.Blazor.Client/BffClientAuthenticationStateProvider.cs b/src/Duende.Bff.Blazor.Client/BffClientAuthenticationStateProvider.cs index c2e5107f..b39ac291 100644 --- a/src/Duende.Bff.Blazor.Client/BffClientAuthenticationStateProvider.cs +++ b/src/Duende.Bff.Blazor.Client/BffClientAuthenticationStateProvider.cs @@ -1,4 +1,7 @@ -using System.Net.Http.Json; +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + +using System.Net.Http.Json; using System.Security.Claims; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Components; @@ -27,10 +30,11 @@ public BffClientAuthenticationStateProvider( _client = factory.CreateClient("BffAuthenticationStateProvider"); _logger = logger; _cachedUser = GetPersistedUser(state); - if (_cachedUser.Identity?.IsAuthenticated == true) + if (_cachedUser.Identity?.IsAuthenticated == true) { _userLastCheck = DateTimeOffset.Now; } + _options = options.Value; } @@ -106,7 +110,7 @@ private async Task FetchUser() foreach (var claim in claims) { identity.AddClaim(new Claim(claim.Type, claim.Value.ToString() ?? "no value")); - } + } } return new ClaimsPrincipal(identity); @@ -131,4 +135,4 @@ private ClaimsPrincipal GetPersistedUser(PersistentComponentState state) return lite.ToClaimsPrincipal(); } -} +} \ No newline at end of file diff --git a/src/Duende.Bff.Blazor.Client/ClaimLite.cs b/src/Duende.Bff.Blazor.Client/ClaimLite.cs index 7af15c7f..306dba75 100644 --- a/src/Duende.Bff.Blazor.Client/ClaimLite.cs +++ b/src/Duende.Bff.Blazor.Client/ClaimLite.cs @@ -1,24 +1,27 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + namespace Duende.Bff.Blazor.Client; // TODO - Consider consolidating this and Duende.Bff.ClaimLite /// -/// Serialization friendly claim +/// Serialization friendly claim /// public class ClaimLite { /// - /// The type + /// The type /// public string Type { get; init; } = default!; - + /// - /// The value + /// The value /// public string Value { get; init; } = default!; /// - /// The value type + /// The value type /// public string? ValueType { get; init; } -} +} \ No newline at end of file diff --git a/src/Duende.Bff.Blazor.Client/ClaimsLiteExtensions.cs b/src/Duende.Bff.Blazor.Client/ClaimsLiteExtensions.cs index 95ef570b..cddd7596 100644 --- a/src/Duende.Bff.Blazor.Client/ClaimsLiteExtensions.cs +++ b/src/Duende.Bff.Blazor.Client/ClaimsLiteExtensions.cs @@ -1,3 +1,6 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + using System.Security.Claims; namespace Duende.Bff.Blazor.Client; @@ -5,18 +8,20 @@ namespace Duende.Bff.Blazor.Client; public static class ClaimsLiteExtensions { /// - /// Converts a ClaimsPrincipalLite to ClaimsPrincipal + /// Converts a ClaimsPrincipalLite to ClaimsPrincipal /// public static ClaimsPrincipal ToClaimsPrincipal(this ClaimsPrincipalLite principal) { - var claims = principal.Claims.Select(x => new Claim(x.Type, x.Value, x.ValueType ?? ClaimValueTypes.String)).ToArray(); - var id = new ClaimsIdentity(claims, principal.AuthenticationType, principal.NameClaimType, principal.RoleClaimType); + var claims = principal.Claims.Select(x => new Claim(x.Type, x.Value, x.ValueType ?? ClaimValueTypes.String)) + .ToArray(); + var id = new ClaimsIdentity(claims, principal.AuthenticationType, principal.NameClaimType, + principal.RoleClaimType); return new ClaimsPrincipal(id); } /// - /// Converts a ClaimsPrincipal to ClaimsPrincipalLite + /// Converts a ClaimsPrincipal to ClaimsPrincipalLite /// public static ClaimsPrincipalLite ToClaimsPrincipalLite(this ClaimsPrincipal principal) { diff --git a/src/Duende.Bff.Blazor.Client/ClaimsPrincipalLite.cs b/src/Duende.Bff.Blazor.Client/ClaimsPrincipalLite.cs index 3d1cd6b7..f825ff44 100644 --- a/src/Duende.Bff.Blazor.Client/ClaimsPrincipalLite.cs +++ b/src/Duende.Bff.Blazor.Client/ClaimsPrincipalLite.cs @@ -1,27 +1,30 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + namespace Duende.Bff.Blazor.Client; /// -/// Serialization friendly ClaimsPrincipal +/// Serialization friendly ClaimsPrincipal /// public class ClaimsPrincipalLite { /// - /// The authentication type + /// The authentication type /// public string? AuthenticationType { get; init; } /// - /// The name claim type + /// The name claim type /// public string? NameClaimType { get; init; } /// - /// The role claim type + /// The role claim type /// public string? RoleClaimType { get; init; } /// - /// The claims + /// The claims /// public ClaimLite[] Claims { get; init; } = default!; -} +} \ No newline at end of file diff --git a/src/Duende.Bff.Blazor.Client/ServiceCollectionExtensions.cs b/src/Duende.Bff.Blazor.Client/ServiceCollectionExtensions.cs index 2655d41e..87ecef21 100644 --- a/src/Duende.Bff.Blazor.Client/ServiceCollectionExtensions.cs +++ b/src/Duende.Bff.Blazor.Client/ServiceCollectionExtensions.cs @@ -1,3 +1,6 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -7,9 +10,10 @@ namespace Duende.Bff.Blazor.Client; public static class ServiceCollectionExtensions { - public static IServiceCollection AddBff(this IServiceCollection services, Action? configureAction = null) + public static IServiceCollection AddBff(this IServiceCollection services, + Action? configureAction = null) { - if(configureAction != null) + if (configureAction != null) { services.Configure(configureAction); } @@ -24,14 +28,14 @@ public static IServiceCollection AddBff(this IServiceCollection services, Actio var baseAddress = GetBaseAddress(sp); client.BaseAddress = new Uri(baseAddress); }).AddHttpMessageHandler(); - + return services; } private static string GetBaseAddress(IServiceProvider sp) { var opt = sp.GetRequiredService>(); - if(opt.Value.RemoteApiBaseAddress != null) + if (opt.Value.RemoteApiBaseAddress != null) { return opt.Value.RemoteApiBaseAddress; } @@ -48,7 +52,8 @@ private static string GetRemoteApiPath(IServiceProvider sp) return opt.Value.RemoteApiPath; } - private static Action SetBaseAddressInConfigureClient(Action? configureClient) + private static Action SetBaseAddressInConfigureClient( + Action? configureClient) { return (sp, client) => { @@ -57,7 +62,8 @@ private static Action SetBaseAddressInConfigureCli }; } - private static Action SetBaseAddressInConfigureClient(Action? configureClient) + private static Action SetBaseAddressInConfigureClient( + Action? configureClient) { return (sp, client) => { @@ -81,37 +87,43 @@ private static void SetBaseAddress(IServiceProvider sp, HttpClient client) { remoteApiPath = remoteApiPath.Substring(1); } + if (!remoteApiPath.EndsWith("/")) { remoteApiPath += "/"; } } + client.BaseAddress = new Uri(new Uri(baseAddress), remoteApiPath); } - public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName, Action configureClient) + public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName, + Action configureClient) { - return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient)) + return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient)) .AddHttpMessageHandler(); } - public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName, Action? configureClient = null) + public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName, + Action? configureClient = null) { return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient)) - .AddHttpMessageHandler(); + .AddHttpMessageHandler(); } - - public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, Action configureClient) + + public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, + Action configureClient) where T : class { return services.AddHttpClient(SetBaseAddressInConfigureClient(configureClient)) .AddHttpMessageHandler(); } - public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, Action? configureClient = null) + public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, + Action? configureClient = null) where T : class { return services.AddHttpClient(SetBaseAddressInConfigureClient(configureClient)) - .AddHttpMessageHandler(); + .AddHttpMessageHandler(); } -} +} \ No newline at end of file diff --git a/src/Duende.Bff.Blazor/BffBuilderExtensions.cs b/src/Duende.Bff.Blazor/BffBuilderExtensions.cs index 7bb33d68..5e8de8ae 100644 --- a/src/Duende.Bff.Blazor/BffBuilderExtensions.cs +++ b/src/Duende.Bff.Blazor/BffBuilderExtensions.cs @@ -1,3 +1,6 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Duende.Bff.Blazor/CaptureManagementClaimsCookieEvents.cs b/src/Duende.Bff.Blazor/CaptureManagementClaimsCookieEvents.cs index d6e21b43..95916502 100644 --- a/src/Duende.Bff.Blazor/CaptureManagementClaimsCookieEvents.cs +++ b/src/Duende.Bff.Blazor/CaptureManagementClaimsCookieEvents.cs @@ -1,3 +1,6 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + using System.Security.Claims; using Microsoft.AspNetCore.Authentication.Cookies; @@ -20,7 +23,6 @@ public override async Task ValidatePrincipal(CookieValidatePrincipalContext cont if (context.Principal?.Identity is ClaimsIdentity id) { - foreach (var claim in managementClaims) { if (context.Principal.Claims.Any(c => c.Type == claim.type) != true) diff --git a/src/Duende.Bff.Blazor/PersistingAuthenticationStateProvider.cs b/src/Duende.Bff.Blazor/PersistingAuthenticationStateProvider.cs index eafd8b2b..3a1840a3 100644 --- a/src/Duende.Bff.Blazor/PersistingAuthenticationStateProvider.cs +++ b/src/Duende.Bff.Blazor/PersistingAuthenticationStateProvider.cs @@ -1,3 +1,6 @@ +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. + using System.Diagnostics; using System.Security.Claims; using Duende.Bff.Blazor.Client; @@ -77,11 +80,11 @@ private async Task OnPersistingAsync() }; _logger.LogDebug("Persisting Authentication State"); - + _state.PersistAsJson(nameof(ClaimsPrincipalLite), principal); } - + public void Dispose() { _subscription.Dispose(); diff --git a/src/Duende.Bff.Blazor/ServerSideTokenStore.cs b/src/Duende.Bff.Blazor/ServerSideTokenStore.cs index d97bee81..099c9d86 100644 --- a/src/Duende.Bff.Blazor/ServerSideTokenStore.cs +++ b/src/Duende.Bff.Blazor/ServerSideTokenStore.cs @@ -1,28 +1,32 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. +// Copyright (c) Duende Software. All rights reserved. +// See LICENSE in the project root for license information. using System.Security.Claims; using Duende.AccessTokenManagement.OpenIdConnect; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging; + namespace Duende.Bff.Blazor; /// -/// A token store that retrieves tokens from server side sessions. +/// A token store that retrieves tokens from server side sessions. /// public class ServerSideTokenStore( IStoreTokensInAuthenticationProperties tokensInAuthProperties, - IUserSessionStore sessionStore, + IUserSessionStore sessionStore, IDataProtectionProvider dataProtectionProvider, ILogger logger) : IUserTokenStore { - private readonly IDataProtector protector = dataProtectionProvider.CreateProtector(ServerSideTicketStore.DataProtectorPurpose); + private readonly IDataProtector protector = + dataProtectionProvider.CreateProtector(ServerSideTicketStore.DataProtectorPurpose); + public async Task GetTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null) { logger.LogDebug("Retrieving token for user {user}", user.Identity?.Name); var session = await GetSession(user); - var ticket = session.Deserialize(protector, logger) ?? throw new InvalidOperationException("Failed to deserialize authentication ticket from session"); + var ticket = session.Deserialize(protector, logger) ?? + throw new InvalidOperationException("Failed to deserialize authentication ticket from session"); return tokensInAuthProperties.GetUserToken(ticket.Properties, parameters); } @@ -46,28 +50,25 @@ private async Task GetSession(ClaimsPrincipal user) return sessions.First(); } - public async Task StoreTokenAsync(ClaimsPrincipal user, UserToken token, UserTokenRequestParameters? parameters = null) + public async Task StoreTokenAsync(ClaimsPrincipal user, UserToken token, + UserTokenRequestParameters? parameters = null) { logger.LogDebug("Storing token for user {user}", user.Identity?.Name); - await UpdateTicket(user, ticket => - { - tokensInAuthProperties.SetUserToken(token, ticket.Properties, parameters); - }); + await UpdateTicket(user, + ticket => { tokensInAuthProperties.SetUserToken(token, ticket.Properties, parameters); }); } public async Task ClearTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null) { logger.LogDebug("Removing token for user {user}", user.Identity?.Name); - await UpdateTicket(user, ticket => - { - tokensInAuthProperties.RemoveUserToken(ticket.Properties, parameters); - }); + await UpdateTicket(user, ticket => { tokensInAuthProperties.RemoveUserToken(ticket.Properties, parameters); }); } protected async Task UpdateTicket(ClaimsPrincipal user, Action updateAction) { var session = await GetSession(user); - var ticket = session.Deserialize(protector, logger) ?? throw new InvalidOperationException("Failed to deserialize authentication ticket from session"); + var ticket = session.Deserialize(protector, logger) ?? + throw new InvalidOperationException("Failed to deserialize authentication ticket from session"); updateAction(ticket); @@ -75,4 +76,4 @@ protected async Task UpdateTicket(ClaimsPrincipal user, Action