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

authorization fix #199

Merged
merged 8 commits into from
Jul 30, 2024
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.161" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.162" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.30.0.95878" PrivateAssets="All" />
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ If all path-items (operations) under a given path all have x-authentication-requ

Authentication-Schemes and Authorize-Roles defined at path/controller level is taken into consideration when generating [Authorize] attributes for path-item/action/method level.

If no path-items (operations) under a given path have the x-authentication-required extension set, then no attributes will be generated for that given path/controller. If you want to force e.g [Authorize] or [AllowAnonymous], set the x-authentication-required extension to `true` or `false` respectively.

### Example

> NOTE: Tags, parameters, responses, request-bodies, schemas etc. are removed for brevity, so the references in spec below are not valid - The specification is only illustrating the various places the 3 new extension tags can be applied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.500" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.Console.Spectre" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.Console.Spectre" Version="2.0.500" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public string Generate()
sb.AppendLine(8, "var responseBuilder = httpMessageFactory.FromResponse(response);");

var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.ParameterName)
.OrderBy(x => x.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void AppendContentIsStatus(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -202,7 +202,7 @@ private void AppendContentWithProblemDetails(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -301,7 +301,7 @@ private void AppendContentWithoutProblemDetails(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void AppendContentIsStatus(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -183,7 +183,7 @@ private void AppendContentWithProblemDetails(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -276,7 +276,7 @@ private void AppendContentWithoutProblemDetails(
StringBuilder sb)
{
var responseModels = parameters.ResponseModels
.AppendUnauthorizedIfNeeded(parameters.Authorization)
.AppendUnauthorizedIfNeeded(parameters.Authorization, parameters.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(parameters.Authorization)
.AppendBadRequestIfNeeded(parameters.HasParameterType)
.OrderBy(x => x.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.500" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public record ContentGeneratorClientEndpointParameters(
string ResultName,
string? ParameterName,
ApiAuthorizeModel? Authorization,
bool IsAuthorizationRequiredFromPath,
IList<ApiOperationResponseModel> ResponseModels,
IList<ContentGeneratorClientEndpointParametersParameters>? Parameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record ContentGeneratorClientEndpointResultInterfaceParameters(
string InheritInterfaceName,
bool HasParameterType,
ApiAuthorizeModel? Authorization,
bool IsAuthorizationRequiredFromPath,
IList<ApiOperationResponseModel> ResponseModels);

public record ContentGeneratorClientEndpointResultInterfaceParametersParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public record ContentGeneratorClientEndpointResultParameters(
string InheritClassName,
bool HasParameterType,
ApiAuthorizeModel? Authorization,
bool IsAuthorizationRequiredFromPath,
IList<ApiOperationResponseModel> ResponseModels,
IList<ContentGeneratorClientEndpointResultParametersParameters>? Parameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,27 @@ public static class ApiOperationResponseModelExtensions

public static IEnumerable<ApiOperationResponseModel> AppendUnauthorizedIfNeeded(
this IEnumerable<ApiOperationResponseModel> responseModels,
ApiAuthorizeModel? authorization)
ApiAuthorizeModel? authorization,
bool isRequiredFromPath)
{
if (authorization is null)
if (authorization is null &&
!isRequiredFromPath)
{
return responseModels;
}

if (authorization is not null &&
authorization.UseAllowAnonymous)
{
return responseModels;
}

var models = responseModels.ToList();

var useAllowAnonymous = authorization?.UseAllowAnonymous ?? false;

if (models.TrueForAll(x => x.StatusCode != HttpStatusCode.Unauthorized) &&
!authorization.UseAllowAnonymous)
(isRequiredFromPath || !useAllowAnonymous))
{
models.Add(
new ApiOperationResponseModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static void AppendProducesWithProblemDetails(
ContentGeneratorServerEndpointMethodParameters item)
{
var responseModels = item.ResponseModels
.AppendUnauthorizedIfNeeded(item.Authorization)
.AppendUnauthorizedIfNeeded(item.Authorization, item.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(item.Authorization)
.AppendBadRequestIfNeeded(item.ParameterTypeName)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -288,7 +288,7 @@ private static void AppendProducesWithoutProblemDetails(
ContentGeneratorServerEndpointMethodParameters item)
{
var responseModels = item.ResponseModels
.AppendUnauthorizedIfNeeded(item.Authorization)
.AppendUnauthorizedIfNeeded(item.Authorization, item.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(item.Authorization)
.AppendBadRequestIfNeeded(item.ParameterTypeName)
.OrderBy(x => x.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.Rest" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.Rest" Version="2.0.500" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static void AppendProducesWithProblemDetails(
ContentGeneratorServerEndpointMethodParameters item)
{
var responseModels = item.ResponseModels
.AppendUnauthorizedIfNeeded(item.Authorization)
.AppendUnauthorizedIfNeeded(item.Authorization, item.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(item.Authorization)
.AppendBadRequestIfNeeded(item.ParameterTypeName)
.OrderBy(x => x.StatusCode)
Expand Down Expand Up @@ -265,7 +265,7 @@ private static void AppendProducesWithoutProblemDetails(
ContentGeneratorServerEndpointMethodParameters item)
{
var responseModels = item.ResponseModels
.AppendUnauthorizedIfNeeded(item.Authorization)
.AppendUnauthorizedIfNeeded(item.Authorization, item.IsAuthorizationRequiredFromPath)
.AppendForbiddenIfNeeded(item.Authorization)
.AppendBadRequestIfNeeded(item.ParameterTypeName)
.OrderBy(x => x.StatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.DotNet" Version="2.0.498" />
<PackageReference Include="Atc.OpenApi" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.DotNet" Version="2.0.500" />
<PackageReference Include="Atc.OpenApi" Version="2.0.500" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public record ContentGeneratorServerEndpointMethodParameters(
long? MultipartBodyLengthLimit,
string ResultName,
ApiAuthorizeModel? Authorization,
bool IsAuthorizationRequiredFromPath,
IEnumerable<ApiOperationResponseModel> ResponseModels);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static ContentGeneratorClientEndpointParameters Create(

var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var operationName = openApiOperation.GetOperationName();
var controllerAuthorization = openApiPath.ExtractApiPathAuthorization();
var endpointAuthorization = openApiOperation.ExtractApiOperationAuthorization(openApiPath);
var responseModels = openApiOperation.ExtractApiOperationResponseModels(modelNamespace).ToList();

Expand All @@ -41,6 +42,7 @@ public static ContentGeneratorClientEndpointParameters Create(
ResultName: $"{operationName}{ContentGeneratorConstants.EndpointResult}",
ParameterName: $"{operationName}{ContentGeneratorConstants.Parameters}",
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null,
ResponseModels: responseModels,
parameters);
}
Expand All @@ -57,6 +59,7 @@ public static ContentGeneratorClientEndpointParameters Create(
ResultName: $"{operationName}{ContentGeneratorConstants.EndpointResult}",
ParameterName: null,
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null,
ResponseModels: responseModels,
Parameters: null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static ContentGeneratorClientEndpointResultInterfaceParameters Create(

var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var operationName = openApiOperation.GetOperationName();
var controllerAuthorization = openApiPath.ExtractApiPathAuthorization();
var endpointAuthorization = openApiOperation.ExtractApiOperationAuthorization(openApiPath);
var responseModels = openApiOperation.ExtractApiOperationResponseModels(modelNamespace).ToList();
var hasParameterType = openApiPath.HasParameters() || openApiOperation.HasParametersOrRequestBody();
Expand All @@ -32,6 +33,7 @@ public static ContentGeneratorClientEndpointResultInterfaceParameters Create(
InheritInterfaceName: "IEndpointResponse",
HasParameterType: hasParameterType,
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null,
ResponseModels: responseModels);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static ContentGeneratorClientEndpointResultParameters Create(

var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var operationName = openApiOperation.GetOperationName();
var controllerAuthorization = openApiPath.ExtractApiPathAuthorization();
var endpointAuthorization = openApiOperation.ExtractApiOperationAuthorization(openApiPath);
var responseModels = openApiOperation.ExtractApiOperationResponseModels(modelNamespace).ToList();
var hasParameterType = openApiPath.HasParameters() || openApiOperation.HasParametersOrRequestBody();
Expand All @@ -35,6 +36,7 @@ public static ContentGeneratorClientEndpointResultParameters Create(
InheritClassName: ContentGeneratorConstants.EndpointResponse,
HasParameterType: hasParameterType,
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null,
ResponseModels: responseModels,
parameters);
}
Expand All @@ -48,6 +50,7 @@ public static ContentGeneratorClientEndpointResultParameters Create(
InheritClassName: ContentGeneratorConstants.EndpointResponse,
HasParameterType: hasParameterType,
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null,
ResponseModels: responseModels,
Parameters: null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static ContentGeneratorServerEndpointParameters Create(
MultipartBodyLengthLimit: GetMultipartBodyLengthLimit(apiOperation.Value),
ResultName: $"{operationName}{ContentGeneratorConstants.Result}",
ResponseModels: responseModels,
Authorization: endpointAuthorization));
Authorization: endpointAuthorization,
IsAuthorizationRequiredFromPath: controllerAuthorization is not null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Atc" Version="2.0.498" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.498" />
<PackageReference Include="Atc.OpenApi" Version="2.0.498" />
<PackageReference Include="Atc" Version="2.0.500" />
<PackageReference Include="Atc.CodeDocumentation" Version="2.0.500" />
<PackageReference Include="Atc.OpenApi" Version="2.0.500" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.15" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.15" />
</ItemGroup>
Expand Down
Loading
Loading