From 4d831a70614e9865002aae677187e8d849302a7f Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 28 Oct 2022 20:27:36 +0000 Subject: [PATCH] Add endpoint names and setup Azure AD auth --- .../Podcast.MinimalAPI.csproj | 2 +- .../Podcasts/Podcast.MinimalAPI/Program.cs | 7 +- .../Routes/CategoriesApi.cs | 2 +- .../Podcast.MinimalAPI/Routes/EpisodesApi.cs | 2 +- .../Podcast.MinimalAPI/Routes/FeedsApi.cs | 66 ++++++++++--------- .../Podcast.MinimalAPI/Routes/ShowsApi.cs | 4 +- .../appsettings.Development.json | 3 +- 7 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Podcast.MinimalAPI.csproj b/src/Services/Podcasts/Podcast.MinimalAPI/Podcast.MinimalAPI.csproj index 0f7e047..d4722fb 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Podcast.MinimalAPI.csproj +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Podcast.MinimalAPI.csproj @@ -22,11 +22,11 @@ + - diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs index a21e5e4..061d369 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Program.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.RateLimiting; using System.Threading.RateLimiting; using Microsoft.AspNetCore.Authentication.JwtBearer; using Swashbuckle.AspNetCore.SwaggerGen; +using Microsoft.Identity.Web; var builder = WebApplication.CreateBuilder(args); @@ -20,8 +21,8 @@ builder.Services.AddSingleton(new QueueClient(queueConnectionString, "feed-queue builder.Services.AddHttpClient(); // Authentication and authorization-related services -builder.Services.AddAuthentication().AddJwtBearer(); -builder.Services.AddAuthorization(); +builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration); +builder.Services.AddAuthorizationBuilder().AddPolicy("modify_feeds", policy => policy.RequireScope("API.Access")); // OpenAPI and versioning-related services builder.Services.AddSwaggerGen(); @@ -60,7 +61,7 @@ await EnsureDbAsync(app.Services); app.UseSwagger(); app.UseSwaggerUI(c => { - c.SwaggerEndpoint("/swagger/v1/swagger.json", "NetPodcast Api v1"); + c.SwaggerEndpoint("/swagger/v1/swagger.json", ".NET Podcasts Minimal API"); }); app.UseCors(); app.UseRateLimiter(); diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/CategoriesApi.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/CategoriesApi.cs index c2350db..f4de851 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/CategoriesApi.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/CategoriesApi.cs @@ -10,7 +10,7 @@ public static class CategoriesApi { public static RouteGroupBuilder MapCategoriesApi(this RouteGroupBuilder group) { - group.MapPost("/", GetAllCategories); + group.MapPost("/", GetAllCategories).WithName("GetCategories"); return group; } diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/EpisodesApi.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/EpisodesApi.cs index f8cb682..cf35ac7 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/EpisodesApi.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/EpisodesApi.cs @@ -10,7 +10,7 @@ public static class EpisodesApi { public static RouteGroupBuilder MapEpisodesApi(this RouteGroupBuilder group) { - group.MapPost("/{id}", GetEpisodeById); + group.MapPost("/{id}", GetEpisodeById).WithName("GetEpisodeById"); return group; } diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/FeedsApi.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/FeedsApi.cs index 4762ad9..8f4d380 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/FeedsApi.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/FeedsApi.cs @@ -6,45 +6,51 @@ using Podcast.Infrastructure.Data; using Podcast.Infrastructure.Data.Models; using Podcast.Infrastructure.Http.Feeds; using Microsoft.OpenApi.Models; -using Microsoft.AspNetCore.Authentication.JwtBearer; - +using Microsoft.AspNetCore.Authentication.JwtBearer; + namespace Podcast.API.Routes; public static class FeedsApi { public static RouteGroupBuilder MapFeedsApi(this RouteGroupBuilder group) { - group.MapPost("/", CreateFeed); - group.MapGet("/", GetAllFeeds); - group.MapPut("/{id}", UpdateFeed).RequireAuthorization().AddOpenApiSecurityRequirement(); - group.MapDelete("/{id}", DeleteFeed).RequireAuthorization().AddOpenApiSecurityRequirement(); + group.MapPost("/", CreateFeed).WithName("CreateFeed"); + group.MapGet("/", GetAllFeeds).WithName("GetFeeds"); + group.MapPut("/{id}", UpdateFeed) + .RequireAuthorization("modify_feeds") + .AddOpenApiSecurityRequirement() + .WithName("UpdateFeedById"); + group.MapDelete("/{id}", DeleteFeed) + .RequireAuthorization("modify_feeds") + .AddOpenApiSecurityRequirement() + .WithName("DeleteFeedById"); return group; } - private static RouteHandlerBuilder AddOpenApiSecurityRequirement(this RouteHandlerBuilder builder) - { - var scheme = new OpenApiSecurityScheme() - { - Type = SecuritySchemeType.Http, - Name = JwtBearerDefaults.AuthenticationScheme, - Scheme = JwtBearerDefaults.AuthenticationScheme, - Reference = new() - { - Type = ReferenceType.SecurityScheme, - Id = JwtBearerDefaults.AuthenticationScheme - } - }; - builder.WithOpenApi(operation => new(operation) - { - Security = - { - new() - { - [scheme] = new List() - } - } - }); - return builder; + private static RouteHandlerBuilder AddOpenApiSecurityRequirement(this RouteHandlerBuilder builder) + { + var scheme = new OpenApiSecurityScheme() + { + Type = SecuritySchemeType.Http, + Name = JwtBearerDefaults.AuthenticationScheme, + Scheme = JwtBearerDefaults.AuthenticationScheme, + Reference = new() + { + Type = ReferenceType.SecurityScheme, + Id = JwtBearerDefaults.AuthenticationScheme + } + }; + builder.WithOpenApi(operation => new(operation) + { + Security = + { + new() + { + [scheme] = new List() + } + } + }); + return builder; } public static async ValueTask CreateFeed(QueueClient queueClient, UserSubmittedFeedDto feed, CancellationToken cancellationToken) diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/ShowsApi.cs b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/ShowsApi.cs index 9f7068f..591ab8a 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/Routes/ShowsApi.cs +++ b/src/Services/Podcasts/Podcast.MinimalAPI/Routes/ShowsApi.cs @@ -9,8 +9,8 @@ public static class ShowsApi { public static RouteGroupBuilder MapShowsApi(this RouteGroupBuilder group) { - group.MapGet("/", GetAllShows); - group.MapGet("/{id}", GetShowById); + group.MapGet("/", GetAllShows).WithName("GetShows"); + group.MapGet("/{id}", GetShowById).WithName("GetShowsById"); return group; } diff --git a/src/Services/Podcasts/Podcast.MinimalAPI/appsettings.Development.json b/src/Services/Podcasts/Podcast.MinimalAPI/appsettings.Development.json index 1edf043..e53b149 100644 --- a/src/Services/Podcasts/Podcast.MinimalAPI/appsettings.Development.json +++ b/src/Services/Podcasts/Podcast.MinimalAPI/appsettings.Development.json @@ -17,7 +17,8 @@ "http://localhost:56906", "https://localhost:44385", "https://localhost:5001", - "http://localhost:5000" + "http://localhost:5000", + "1ba2c41d-3a54-414a-9700-1f9393cfafca" ], "ValidIssuer": "dotnet-user-jwts" }