Skip to content

Commit

Permalink
Formatting and copyright headers
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Aug 16, 2024
1 parent 560550b commit cc4f87f
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 62 deletions.
8 changes: 6 additions & 2 deletions src/Duende.Bff.Blazor.Client/AntiforgeryHandler.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
request.Headers.Add("X-CSRF", "1");
return base.SendAsync(request, cancellationToken);
Expand Down
13 changes: 8 additions & 5 deletions src/Duende.Bff.Blazor.Client/BffBlazorOptions.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Options for Blazor BFF
/// Options for Blazor BFF
/// </summary>
public class BffBlazorOptions
{
/// <summary>
/// The base path to use for remote APIs.
/// The base path to use for remote APIs.
/// </summary>
public string RemoteApiPath { get; set; } = "remote-apis/";

/// <summary>
/// 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.
/// </summary>
public string? RemoteApiBaseAddress { get; set; } = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -106,7 +110,7 @@ private async Task<ClaimsPrincipal> FetchUser()
foreach (var claim in claims)
{
identity.AddClaim(new Claim(claim.Type, claim.Value.ToString() ?? "no value"));
}
}
}

return new ClaimsPrincipal(identity);
Expand All @@ -131,4 +135,4 @@ private ClaimsPrincipal GetPersistedUser(PersistentComponentState state)

return lite.ToClaimsPrincipal();
}
}
}
15 changes: 9 additions & 6 deletions src/Duende.Bff.Blazor.Client/ClaimLite.cs
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Serialization friendly claim
/// Serialization friendly claim
/// </summary>
public class ClaimLite
{
/// <summary>
/// The type
/// The type
/// </summary>
public string Type { get; init; } = default!;

/// <summary>
/// The value
/// The value
/// </summary>
public string Value { get; init; } = default!;

/// <summary>
/// The value type
/// The value type
/// </summary>
public string? ValueType { get; init; }
}
}
13 changes: 9 additions & 4 deletions src/Duende.Bff.Blazor.Client/ClaimsLiteExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
// 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;

public static class ClaimsLiteExtensions
{
/// <summary>
/// Converts a ClaimsPrincipalLite to ClaimsPrincipal
/// Converts a ClaimsPrincipalLite to ClaimsPrincipal
/// </summary>
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);
}

/// <summary>
/// Converts a ClaimsPrincipal to ClaimsPrincipalLite
/// Converts a ClaimsPrincipal to ClaimsPrincipalLite
/// </summary>
public static ClaimsPrincipalLite ToClaimsPrincipalLite(this ClaimsPrincipal principal)
{
Expand Down
15 changes: 9 additions & 6 deletions src/Duende.Bff.Blazor.Client/ClaimsPrincipalLite.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Serialization friendly ClaimsPrincipal
/// Serialization friendly ClaimsPrincipal
/// </summary>
public class ClaimsPrincipalLite
{
/// <summary>
/// The authentication type
/// The authentication type
/// </summary>
public string? AuthenticationType { get; init; }

/// <summary>
/// The name claim type
/// The name claim type
/// </summary>
public string? NameClaimType { get; init; }

/// <summary>
/// The role claim type
/// The role claim type
/// </summary>
public string? RoleClaimType { get; init; }

/// <summary>
/// The claims
/// The claims
/// </summary>
public ClaimLite[] Claims { get; init; } = default!;
}
}
42 changes: 27 additions & 15 deletions src/Duende.Bff.Blazor.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -7,9 +10,10 @@ namespace Duende.Bff.Blazor.Client;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddBff(this IServiceCollection services, Action<BffBlazorOptions>? configureAction = null)
public static IServiceCollection AddBff(this IServiceCollection services,
Action<BffBlazorOptions>? configureAction = null)
{
if(configureAction != null)
if (configureAction != null)
{
services.Configure(configureAction);
}
Expand All @@ -24,14 +28,14 @@ public static IServiceCollection AddBff(this IServiceCollection services, Actio
var baseAddress = GetBaseAddress(sp);
client.BaseAddress = new Uri(baseAddress);
}).AddHttpMessageHandler<AntiforgeryHandler>();

return services;
}

private static string GetBaseAddress(IServiceProvider sp)
{
var opt = sp.GetRequiredService<IOptions<BffBlazorOptions>>();
if(opt.Value.RemoteApiBaseAddress != null)
if (opt.Value.RemoteApiBaseAddress != null)
{
return opt.Value.RemoteApiBaseAddress;
}
Expand All @@ -48,7 +52,8 @@ private static string GetRemoteApiPath(IServiceProvider sp)
return opt.Value.RemoteApiPath;
}

private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureClient(Action<IServiceProvider, HttpClient>? configureClient)
private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureClient(
Action<IServiceProvider, HttpClient>? configureClient)
{
return (sp, client) =>
{
Expand All @@ -57,7 +62,8 @@ private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureCli
};
}

private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureClient(Action<HttpClient>? configureClient)
private static Action<IServiceProvider, HttpClient> SetBaseAddressInConfigureClient(
Action<HttpClient>? configureClient)
{
return (sp, client) =>
{
Expand All @@ -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<HttpClient> configureClient)
public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName,
Action<HttpClient> configureClient)
{
return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient))
return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient))
.AddHttpMessageHandler<AntiforgeryHandler>();
}

public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName, Action<IServiceProvider, HttpClient>? configureClient = null)
public static IHttpClientBuilder AddRemoteApiHttpClient(this IServiceCollection services, string clientName,
Action<IServiceProvider, HttpClient>? configureClient = null)
{
return services.AddHttpClient(clientName, SetBaseAddressInConfigureClient(configureClient))
.AddHttpMessageHandler<AntiforgeryHandler>();
.AddHttpMessageHandler<AntiforgeryHandler>();
}

public static IHttpClientBuilder AddRemoteApiHttpClient<T>(this IServiceCollection services, Action<HttpClient> configureClient)

public static IHttpClientBuilder AddRemoteApiHttpClient<T>(this IServiceCollection services,
Action<HttpClient> configureClient)
where T : class
{
return services.AddHttpClient<T>(SetBaseAddressInConfigureClient(configureClient))
.AddHttpMessageHandler<AntiforgeryHandler>();
}

public static IHttpClientBuilder AddRemoteApiHttpClient<T>(this IServiceCollection services, Action<IServiceProvider, HttpClient>? configureClient = null)
public static IHttpClientBuilder AddRemoteApiHttpClient<T>(this IServiceCollection services,
Action<IServiceProvider, HttpClient>? configureClient = null)
where T : class
{
return services.AddHttpClient<T>(SetBaseAddressInConfigureClient(configureClient))
.AddHttpMessageHandler<AntiforgeryHandler>();
.AddHttpMessageHandler<AntiforgeryHandler>();
}
}
}
3 changes: 3 additions & 0 deletions src/Duende.Bff.Blazor/BffBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Duende.Bff.Blazor/CaptureManagementClaimsCookieEvents.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -76,11 +79,11 @@ private async Task OnPersistingAsync()
};

_logger.LogDebug("Persisting Authentication State");

_state.PersistAsJson(nameof(ClaimsPrincipalLite), principal);
}


public void Dispose()
{
_subscription.Dispose();
Expand Down
Loading

0 comments on commit cc4f87f

Please sign in to comment.