Skip to content

Commit

Permalink
Use Options Pattern for AzureAd config
Browse files Browse the repository at this point in the history
  • Loading branch information
EjPlatzer committed Aug 5, 2024
1 parent d9073e8 commit b9c9344
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
9 changes: 0 additions & 9 deletions Gordon360/Authorization/AzureAdConfig.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Gordon360/Options/AzureAdOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Options;
using System.ComponentModel.DataAnnotations;

namespace Gordon360.Options;

public sealed record AzureAdOptions
{
public const string AzureAd = "AzureAd";

[Required]
public required string Instance { get; set; }
[Required]
public required string ClientId { get; set; }
[Required]
public required string TenantId { get; set; }
[Required]
public required string Audience { get; set; }
}

[OptionsValidator]
public partial class ValidateAzureAdOptions : IValidateOptions<AzureAdOptions> { }

5 changes: 5 additions & 0 deletions Gordon360/Options/OptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public static class OptionsExtensions
{
public static IServiceCollection Add360Options(this IServiceCollection services)
{
services.AddSingleton<IValidateOptions<AzureAdOptions>, ValidateAzureAdOptions>();
services.AddOptions<AzureAdOptions>()
.BindConfiguration(AzureAdOptions.AzureAd)
.ValidateOnStart();

services.AddSingleton<IValidateOptions<BonAppetitOptions>, ValidateBonAppetitOptions>();
services.AddOptions<BonAppetitOptions>()
.BindConfiguration(BonAppetitOptions.BonAppetit)
Expand Down
15 changes: 8 additions & 7 deletions Gordon360/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.Add360Options();

builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
Expand All @@ -41,7 +43,7 @@

builder.Services.AddEndpointsApiExplorer();

var azureConfig = builder.Configuration.GetSection("AzureAd").Get<AzureAdConfig>();
var azureOptions = builder.Configuration.GetSection("AzureAd").Get<AzureAdOptions>();

builder.Services.AddSwaggerGen(c =>
{
Expand All @@ -52,11 +54,11 @@
{
AuthorizationCode = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{azureConfig.TenantId}/oauth2/v2.0/authorize"),
TokenUrl = new Uri($"https://login.microsoftonline.com/{azureConfig.TenantId}/oauth2/v2.0/token"),
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{azureOptions.TenantId}/oauth2/v2.0/authorize"),

Check warning on line 57 in Gordon360/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
TokenUrl = new Uri($"https://login.microsoftonline.com/{azureOptions.TenantId}/oauth2/v2.0/token"),
Scopes = new Dictionary<string, string> {
{
$"{azureConfig.Audience}/access_as_user",
$"{azureOptions.Audience}/access_as_user",
"Access 360 as you."
}
}
Expand Down Expand Up @@ -92,7 +94,6 @@
options.UseSqlServer(builder.Configuration.GetConnectionString("webSQL"))
);

builder.Services.Add360Options();
builder.Services.Add360Services();
builder.Services.AddHostedService<EventCacheRefreshService>();
builder.Services.AddScoped<ServerUtils, ServerUtils>();
Expand All @@ -106,8 +107,8 @@
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.OAuthClientId(azureConfig.ClientId);
c.OAuthScopes($"{azureConfig.Audience}/access_as_user");
c.OAuthClientId(azureOptions.ClientId);

Check warning on line 110 in Gordon360/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
c.OAuthScopes($"{azureOptions.Audience}/access_as_user");
c.OAuthUsePkce();
});

Expand Down

0 comments on commit b9c9344

Please sign in to comment.