Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine logic for modifying class methods and global methods, update Web API code modifier configs #1833

Merged
merged 11 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ class CodeChange
public string? Block { get; set; } --> C# statement that is parsed using SyntaxFactory.ParseStatement
public string? Parent { get; set; } --> Add C# statement syntax node upon this parent statement syntax node based on Type
public string? Type { get; set; } --> CodeChangeType (below) string.
public bool? Append { get; set; } = false; --> Insert Block at the top of the method.
public bool? Prepend { get; set; } = false; --> Insert Block at the top of the method.
public string ReplaceSnippet { get; set; } --> Replace snippet with Block
public string Parameter { get; set; } --> Parameter for adding or updating a lambda expression
}

class CodeChangeType
{
public const string MemberAccess = nameof(MemberAccess); --> Add a SimpleMemberAccess expression to the parent statement syntax.
public const string InLambdaBlock = nameof(InBlock); --> Add in lambda block to the parent statement syntax.
public const string Lambda = nameof(Lambda); --> Add or update lambda to the parent statement syntax.
}

This info is also available in CodeModifierConfig folder as well.

The scenarios below need to be supported for all project types :
Expand All @@ -44,9 +46,9 @@ The scenarios below need to be supported for all project types :
| ASP .NET Core Web App (w/ B2C tenant) | Config w/out layout(.cshtml) files |
| ASP .NET Core Web Api | Config w/out layout(.cshtml) files |
| ASP .NET Core Web Api (w/ B2C tenant) | Config w/out layout(.cshtml) files |
| Blazor Server App | Need config |
| Blazor Server App | Config w/ .razor files |
| Blazor Server App (w/ B2C tenant) | Need config |
| Blazor WebAssembly App | Need config |
| Blazor WebAssembly App | Config w/ .razor files, needs config for Calls Graph / Downstream API |
| Blazor WebAssembly App (w/ B2C tenant) | Need config |
| Blazor Hosted WebAssembly App | Need config |
| Blazor Hosted WebAssembly App (w/ B2C tenant) | Need config |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
"Global": {
"CodeChanges": [
{
"Block": "var initialScopes = WebApplication.CreateBuilder.Configuration[\"DownstreamApi:Scopes\"]?.Split(' ') ?? WebApplication.CreateBuilder.Configuration[\"MicrosoftGraph:Scopes\"]?.Split(' ');\n",
"Block": "var initialScopes = WebApplication.CreateBuilder.Configuration[\"DownstreamApi:Scopes\"]?.Split(' ') ?? WebApplication.CreateBuilder.Configuration[\"MicrosoftGraph:Scopes\"]?.Split(' ')",
"Options": [
"DownstreamApi",
"MicrosoftGraph"
],
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddAuthentication",
"WebApplication.CreateBuilder.Services.AddAuthorization",
"WebApplication.CreateBuilder.Services.AddRazorPages()",
"WebApplication.CreateBuilder.Build()"
"WebApplication.CreateBuilder.Services.AddRazorPages",
"WebApplication.CreateBuilder.Build"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)\n .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection(\"AzureAd\"));",
"Block": "WebApplication.CreateBuilder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddControllersWithViews",
"WebApplication.CreateBuilder.Services.AddAuthorization",
Expand All @@ -35,21 +35,37 @@
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddControllersWithViews()\r\n .AddMicrosoftIdentityUI();",
"Block": "AddMicrosoftIdentityWebApp(builder.Configuration.GetSection(\"AzureAd\"))",
"Parent": "WebApplication.CreateBuilder.Services.AddAuthentication",
"CodeChangeType": "MemberAccess",
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddControllersWithViews()",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddAuthentication",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddAuthorization",
"WebApplication.CreateBuilder.Services.AddRazorPages",
"WebApplication.CreateBuilder.Build()"
"WebApplication.CreateBuilder.Build"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "AddMicrosoftIdentityUI()",
"Parent": "WebApplication.CreateBuilder.Services.AddControllersWithViews",
"CodeChangeType":"MemberAccess",
"CodeFormatting": {
"Newline": true
}
},
{
"Parent": "AddMicrosoftIdentityWebApp",
"CodeChangeType": "MemberAccess",
"Options": [
"MemberAccess",
"MicrosoftGraph",
"DownstreamApi"
],
Expand All @@ -60,103 +76,72 @@
}
},
{
"Parent": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"Parent": "EnableTokenAcquisitionToCallDownstreamApi",
"CodeChangeType": "MemberAccess",
"Options": [
"MemberAccess",
"MicrosoftGraph"
],
"Block": "AddMicrosoftGraph(builder.Configuration.GetSection(\"MicrosoftGraph\"))",
"Block": "AddMicrosoftGraph(builder.Configuration.GetSection(\"MicrosoftGraph\"))\r\n .AddInMemoryTokenCaches()",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Parent": "EnableTokenAcquisitionToCallDownstreamApi(initialScopes)",
"CodeChangeType": "MemberAccess",
"Options": [
"MemberAccess",
"DownstreamApi"
],
"Block": "AddDownstreamWebApi(\"DownstreamApi\",builder.Configuration.GetSection(\"DownstreamApi\"))",
"Block": "AddDownstreamWebApi(\"DownstreamApi\",builder.Configuration.GetSection(\"DownstreamApi\"))\r\n .AddInMemoryTokenCaches()",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Parent": "AddDownstreamWebApi",
"Options": [
"MemberAccess",
"DownstreamApi"
],
"Block": "AddInMemoryTokenCaches()",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Parent": "AddMicrosoftGraph",
"Options": [
"MemberAccess",
"MicrosoftGraph"
],
"Block": "AddInMemoryTokenCaches()",
"Block": "WebApplication.CreateBuilder.Services.AddAuthorization()",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddControllersWithViews",
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddAuthorization(options =>\r\n{\r\n // By default, all incoming requests will be authorized according to the default policy\r\n options.FallbackPolicy = options.DefaultPolicy;\r\n});",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddControllersWithViews",
"InsertBefore": [
"WebApplication.CreateBuilder.Services.AddRazorPages()",
"WebApplication.CreateBuilder.Build()"
],
"Block": "options.FallbackPolicy = options.DefaultPolicy",
"Parameter": "options",
"CodeChangeType": "Lambda",
"Parent": "WebApplication.CreateBuilder.Services.AddAuthorization",
"CodeFormatting": {
"Newline": true
"Newline": true,
"NumberOfSpaces": 4
}
},
{
"Block": "AddMicrosoftIdentityConsentHandler()",
"Parent": "WebApplication.CreateBuilder.Services.AddServerSideBlazor()",
"Options": [
"MemberAccess"
],
"Block": "AddMicrosoftIdentityConsentHandler()"
"CodeChangeType": "MemberAccess",
"CodeFormatting":{
"Newline": true,
"NumberOfSpaces": 4
}
},
{
"Block": "app.UseAuthentication();",
"Block": "app.UseAuthentication()",
"InsertAfter": "app.UseRouting()",
"InsertBefore": [
"app.UseAuthorization",
"app.MapControllers",
"app.MapBlazorHub"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "app.UseAuthorization();",
"Block": "app.UseAuthorization()",
"InsertAfter": "app.UseAuthentication()",
"InsertBefore": [
"app.MapControllers",
"app.MapBlazorHub",
"app.MapFallbackToPage"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "app.MapControllers();",
"Block": "app.MapControllers()",
"InsertAfter": "app.UseAuthorization",
"InsertBefore": [
"app.MapBlazorHub",
"app.MapFallbackToPage",
"app.Run"
],
"CodeFormatting": {
"Newline": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
"Global": {
"CodeChanges": [
{
"Block": "builder.Services.AddMsalAuthentication(options =>\r\n{\r\n builder.Configuration.Bind(\"AzureAd\", options.ProviderOptions.Authentication);\r\n});\r\n",
"Block": "builder.Services.AddMsalAuthentication()",
"InsertAfter": "builder.Services.AddScoped",
"InsertBefore": [
"builder.Build"
],
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "builder.Configuration.Bind(\"AzureAd\", options.ProviderOptions.Authentication)",
"CodeChangeType": "Lambda",
"Parameter": "options",
"Parent": "builder.Services.AddMsalAuthentication",
"CodeFormatting": {
"Newline": true,
"NumberOfSpaces": 4
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"Identifier": "dotnet-minimal_api",
"Files": [
{
"FileName": "Program.cs",
"Usings": [
"Microsoft.AspNetCore.Authentication",
"Microsoft.AspNetCore.Authentication.JwtBearer",
"Microsoft.Identity.Web",
"Microsoft.Identity.Web.Resource"
],
"Methods": {
"Global": {
"CodeChanges": [
{
"Block": "WebApplication.CreateBuilder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)",
"InsertAfter": "WebApplication.CreateBuilder",
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "AddMicrosoftIdentityWebApi(WebApplication.CreateBuilder.Configuration.GetSection(\"AzureAd\"))",
"Parent": "AddAuthentication",
"CodeChangeType": "MemberAccess",
"CodeFormatting": {
"Newline": true,
"NumberOfSpaces": 4
}
},
{
"Block": "EnableTokenAcquisitionToCallDownstreamApi()",
"Parent": "AddMicrosoftIdentityWebApi",
"CodeChangeType": "MemberAccess",
"Options": [ "MicrosoftGraph", "DownstreamApi" ],
"CodeFormatting": {
"Newline": true,
"NumberOfSpaces": 8
}
},
{
"Block": "AddMicrosoftGraph(WebApplication.CreateBuilder.Configuration.GetSection(\"MicrosoftGraph\"))\r\n .AddInMemoryTokenCaches()",
"Parent": "EnableTokenAcquisitionToCallDownstreamApi",
"CodeChangeType": "MemberAccess",
"Options": [ "MicrosoftGraph" ],
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "AddDownstreamWebApi(\"DownstreamApi\", WebApplication.CreateBuilder.Configuration.GetSection(\"DownstreamApi\"))\r\n .AddInMemoryTokenCaches()",
"Parent": "EnableTokenAcquisitionToCallDownstreamApi",
"CodeChangeType": "MemberAccess",
"Options": [ "DownstreamApi" ],
"CodeFormatting": {
"NumberOfSpaces": 12,
"Newline": true
}
},
{
"Block": "WebApplication.CreateBuilder.Services.AddAuthorization()",
"InsertAfter": "WebApplication.CreateBuilder.Services.AddAuthentication"
},
{
"Block": "app.UseAuthentication()",
"InsertAfter": "app.UseHttpsRedirection",
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "app.UseAuthorization()",
"InsertAfter": "app.UseAuthentication"
},
{
"Block": "var scopeRequiredByApi = app.Configuration[\"AzureAd:Scopes\"]",
"InsertAfter": "app.UseAuthorization",
"CodeFormatting": {
"Newline": true
}
},
{
"Block": "httpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi)",
"Parent": "app.MapGet",
"Parameter": "HttpContext httpContext",
"CodeChangeType": "Lambda",
"Prepend": true,
"CodeFormatting": {
"NumberOfSpaces": 4
}
},
{
"Parent": "app.MapGet",
"CodeChangeType": "MemberAccess",
"Block": "RequireAuthorization()",
"CodeFormatting": {
"Newline": true
}
}
]
}
}
}
]
}
Loading