Skip to content

Commit

Permalink
Expand Azure AD auth integrations, cleanup
Browse files Browse the repository at this point in the history
Added JWT Bearer authentication to the AzureAdHostApplicationBuilderIdentityExtensions, allowing more flexible authentication setups. Reformatted several class members for clarity and readability. Updated AzureAdConstants with new constants for easier identification and use of JWT Bearer schemes. Extensions for MicrosoftIdentityOptions now include methods to check for client secrets and certificates, improving configuration validation.

Removed outdated README.md that may have contained stale information and potentially caused confusion. Adjusted file permissions on LICENSE.md to be consistent with typical source code files. Introduced a new props file for logging extensions, laying the groundwork for a more robust logging configuration.

Refactored instances of the 'string.IsNullOrWhiteSpace' method to custom 'IsNullOrWhiteSpace' methods to foster consistency across utility extensions, ensuring better maintainability.
  • Loading branch information
dgmjr committed Jan 18, 2024
1 parent 5199882 commit 68afb26
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 46 deletions.
19 changes: 17 additions & 2 deletions AzureAd/AzureAdConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ public static class Constants
{
/// <value>DownstreamApis</value>
public const string DownstreamApis = nameof(DownstreamApis);

/// <value>MicrosoftGraphOptions</value>
public const string MicrosoftGraphOptions = nameof(MicrosoftGraphOptions);

/// <value>MicrosoftGraph</value>
public const string MicrosoftGraph = nameof(MicrosoftGraph);

/// <value>Scopes</value>
public const string Scopes = nameof(Scopes);

/// <value>AzureAdB2C</value>
public const string AzureAdB2C = Microsoft.Identity.Web.Constants.AzureAdB2C;

/// <value>AzureAd</value>
public const string AzureAd = Microsoft.Identity.Web.Constants.AzureAd;

/// <value>OpenIdConnect</value>
public const string OpenIdConnect = OpenIdConnectDefaults.AuthenticationScheme;

/// <value><inheritdoc cref="DownstreamApis" path="/value" />:<inheritdoc cref="MicrosoftGraph" path="/value" />:<inheritdoc cref="Scopes" path="/value" /></value>
public const string DownstreamApis_MsGraph_ScopesConfigurationKey = DownstreamApis + ":" + MicrosoftGraph + ":" + Scopes;
public const string DownstreamApis_MsGraph_ScopesConfigurationKey =
DownstreamApis + ":" + MicrosoftGraph + ":" + Scopes;

/// <value><inheritdoc cref="DownstreamApis" path="/value" />:<inheritdoc cref="MicrosoftGraph" path="/value" /></value>
public const string DownstreamApis_MsGraphConfigurationKey = DownstreamApis + ":" + MicrosoftGraph;
public const string DownstreamApis_MsGraphConfigurationKey =
DownstreamApis + ":" + MicrosoftGraph;

/// <value>JwtBearer</value>
public const string JwtBearerSchemeName = "JwtBearer";

/// <value>JWT Bearer</value>
public const string JwtBearerSchemeDisplayName = "JWT Bearer";
}
33 changes: 22 additions & 11 deletions AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Web.Resource;
using Microsoft.Identity.Web.UI;
using MicrosoftIdentityOptions = Dgmjr.Identity.Web.MicrosoftIdentityOptions;

Expand Down Expand Up @@ -51,6 +52,12 @@ this IHostApplicationBuilder builder
);
}

authenticationBuilder.AddJwtBearer(
JwtBearerSchemeName,
JwtBearerSchemeDisplayName,
options => configurationSection.Bind(options)
);

callsWebApiAuthenticationBuilder
.AddMicrosoftGraph(
builder.Configuration.GetSection(DownstreamApis_MsGraphConfigurationKey)
Expand All @@ -73,18 +80,22 @@ var downstreamApiConfig in builder.Configuration
.AddMicrosoftIdentityConsentHandler()
.AddTransient<Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Controllers.AccountController>();
builder.Services
.ConfigureAll<DownstreamApiOptions>(downstreamApiOptions =>
downstreamApiOptions.Serializer = requestObject =>
new StringContent(
Serialize(
requestObject,
builder.Services
.BuildServiceProvider()
.CreateScope()
.ServiceProvider.GetRequiredService<IOptionsMonitor<JsonOptions>>()
.CurrentValue.JsonSerializerOptions
.ConfigureAll<DownstreamApiOptions>(
downstreamApiOptions =>
downstreamApiOptions.Serializer = requestObject =>
new StringContent(
Serialize(
requestObject,
builder.Services
.BuildServiceProvider()
.CreateScope()
.ServiceProvider.GetRequiredService<
IOptionsMonitor<JsonOptions>
>()
.CurrentValue.JsonSerializerOptions
)
)
))
)
.Configure<MicrosoftIdentityApplicationOptions>(
builder.Configuration.GetSection(AzureAdB2C)
)
Expand Down
22 changes: 11 additions & 11 deletions AzureAd/LICENSE.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
date: 2023-07-13T05:44:46:00+05:00Z
description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda...
keywords:
- IP
- copyright
- license
- mit
- IP
- copyright
- license
- mit
permissions:
- commercial-use
- modifications
- distribution
- private-use
- commercial-use
- modifications
- distribution
- private-use
conditions:
- include-copyright
- include-copyright
limitations:
- liability
- warranty
- liability
- warranty
lastmod: 2024-01-0T00:39:00.0000+05:00Z
license: MIT
slug: mit-license
Expand Down
6 changes: 6 additions & 0 deletions AzureAd/OpenIdConnectOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ public static class OpenIdConnectOptionsExtensions
{
public static bool IsUsingClientSecret(this OpenIdConnectOptions options) =>
!IsNullOrEmpty(options.ClientSecret);

public static bool IsUsingClientSecret(this MicrosoftIdentityOptions options) =>
options.ClientCredentials.Any(cc => cc.CredentialType == CredentialType.Secret);

public static bool IsUsingClientCertificate(this MicrosoftIdentityOptions options) =>
options.ClientCredentials.Any(cc => cc.CredentialType == CredentialType.Certificate);
}
18 changes: 0 additions & 18 deletions AzureAd/README.md

This file was deleted.

17 changes: 17 additions & 0 deletions Logging/Dgmjr.Extensions.Logging.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
* Dgmjr.Extensions.Logging.props
*
* Created: 2024-45-16T05:45:10-05:00
* Modified: 2024-45-16T05:45:10-05:00
*
* Author: David G. Moore, Jr. <[email protected]>
*
* Copyright © 2024 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
-->

<Project>
<ItemGroup>
<Using Include="Microsoft.Extensions.Logging" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion System/System.ComponentModel/StringToDateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string Convert(DateTime value, object? parameter = default)
/// </returns>
public DateTime ConvertBack(string value, object? parameter = default)
{
if (string.IsNullOrWhiteSpace(value))
if (IsNullOrWhiteSpace(value))
{
return DateTime.MinValue;
}
Expand Down
2 changes: 1 addition & 1 deletion System/System/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static string Escape(this string str)
/// </param>
/// <returns>A <see langword="bool"/> value indicating whether the string
/// was null or whitespace</returns>
public static bool IsNullOrWhitespace(this string? str) => string.IsNullOrWhiteSpace(str);
public static bool IsNullOrWhitespace(this string? str) => IsNullOrWhiteSpace(str);

/// <summary>
/// Determines if a string is null or empty
Expand Down
4 changes: 2 additions & 2 deletions System/System/UriOrString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public UriOrString(Uri uri)
public static implicit operator UriOrString(string str) => new(str);

public static implicit operator Uri(UriOrString uriOrString) =>
uriOrString.IsT0 ? uriOrString.AsT0 : new(uriOrString.AsT1);
uriOrIsT0 ? uriOrString.AsT0 : new(uriOrString.AsT1);

public override string ToString()
{
return ((Uri)this).ToString();
}

public static explicit operator string(UriOrString uriOrString) =>
uriOrString.IsT1 ? uriOrString.AsT1 : uriOrString.AsT0.ToString();
uriOrIsT1 ? uriOrString.AsT1 : uriOrString.AsT0.ToString();
}

0 comments on commit 68afb26

Please sign in to comment.