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

Replace authorization project with new rule #803

Merged
merged 19 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
6 changes: 0 additions & 6 deletions GraphQL.Server.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ui.GraphiQL", "src\Ui.Graph
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ui.Voyager", "src\Ui.Voyager\Ui.Voyager.csproj", "{B2C278E4-6A1A-4F83-AE53-C9469B4056EE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authorization.AspNetCore", "src\Authorization.AspNetCore\Authorization.AspNetCore.csproj", "{7A71AF0D-FE5F-4607-A6F6-960FD98CF840}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authorization.AspNetCore.Tests", "tests\Authorization.AspNetCore.Tests\Authorization.AspNetCore.Tests.csproj", "{741DEEE6-FD0B-4F99-8A6F-43584B3E8D5F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.Server.Tests", "tests\Samples.Server.Tests\Samples.Server.Tests.csproj", "{62E7B30D-CB34-45EA-A410-2CAE127385D7}"
Expand Down Expand Up @@ -103,10 +101,6 @@ Global
{B2C278E4-6A1A-4F83-AE53-C9469B4056EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2C278E4-6A1A-4F83-AE53-C9469B4056EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2C278E4-6A1A-4F83-AE53-C9469B4056EE}.Release|Any CPU.Build.0 = Release|Any CPU
{7A71AF0D-FE5F-4607-A6F6-960FD98CF840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A71AF0D-FE5F-4607-A6F6-960FD98CF840}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A71AF0D-FE5F-4607-A6F6-960FD98CF840}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A71AF0D-FE5F-4607-A6F6-960FD98CF840}.Release|Any CPU.Build.0 = Release|Any CPU
{741DEEE6-FD0B-4F99-8A6F-43584B3E8D5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{741DEEE6-FD0B-4F99-8A6F-43584B3E8D5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{741DEEE6-FD0B-4F99-8A6F-43584B3E8D5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
27 changes: 8 additions & 19 deletions samples/Samples.Server/CustomErrorInfoProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;
using GraphQL.Execution;
using GraphQL.Server.Authorization.AspNetCore;
using GraphQL.Server.Transports.AspNetCore.Errors;
using Microsoft.AspNetCore.Authorization;

namespace GraphQL.Samples.Server;
Expand All @@ -11,30 +11,22 @@ namespace GraphQL.Samples.Server;
/// </summary>
public class CustomErrorInfoProvider : ErrorInfoProvider
{
private readonly IAuthorizationErrorMessageBuilder _messageBuilder;

public CustomErrorInfoProvider(IAuthorizationErrorMessageBuilder messageBuilder)
{
_messageBuilder = messageBuilder;
}

public override ErrorInfo GetInfo(ExecutionError executionError)
{
var info = base.GetInfo(executionError);
info.Message = executionError switch
{
AuthorizationError authorizationError => GetAuthorizationErrorMessage(authorizationError),
_ => info.Message,
};

if (executionError is AccessDeniedError accessDeniedError)
info.Message = GetAuthorizationErrorMessage(accessDeniedError);

return info;
}

private string GetAuthorizationErrorMessage(AuthorizationError error)
private string GetAuthorizationErrorMessage(AccessDeniedError error)
{
var errorMessage = new StringBuilder();
_messageBuilder.AppendFailureHeader(errorMessage, error.OperationType);
errorMessage.Append(error.Message);

foreach (var failedRequirement in error.AuthorizationResult.Failure.FailedRequirements)
foreach (var failedRequirement in error.PolicyAuthorizationResult.Failure.FailedRequirements)
{
switch (failedRequirement)
{
Expand All @@ -44,9 +36,6 @@ private string GetAuthorizationErrorMessage(AuthorizationError error)
errorMessage.Append(minimumAgeRequirement.MinimumAge);
errorMessage.Append(" years old.");
break;
default:
_messageBuilder.AppendFailureLine(errorMessage, failedRequirement);
break;
}
}

Expand Down
1 change: 0 additions & 1 deletion samples/Samples.Server/Samples.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Transports.AspNetCore\Transports.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Authorization.AspNetCore\Authorization.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Ui.GraphiQL\Ui.GraphiQL.csproj" />
<ProjectReference Include="..\..\src\Ui.Playground\Ui.Playground.csproj" />
<ProjectReference Include="..\..\src\Ui.Altair\Ui.Altair.csproj" />
Expand Down
4 changes: 1 addition & 3 deletions samples/Samples.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using GraphQL.MicrosoftDI;
using GraphQL.Samples.Schemas.Chat;
using GraphQL.Server;
using GraphQL.Server.Authorization.AspNetCore;
using GraphQL.Server.Transports.AspNetCore;
using GraphQL.Server.Ui.Altair;
using GraphQL.Server.Ui.GraphiQL;
Expand All @@ -30,8 +29,7 @@ public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<IChat, Chat>()
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
.AddTransient<IAuthorizationErrorMessageBuilder, DefaultAuthorizationErrorMessageBuilder>(); // required by CustomErrorInfoProvider
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment());

services.AddGraphQL(builder => builder
.AddApolloTracing()
Expand Down
4 changes: 1 addition & 3 deletions samples/Samples.Server/StartupWithRouting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using GraphQL.MicrosoftDI;
using GraphQL.Samples.Schemas.Chat;
using GraphQL.Server;
using GraphQL.Server.Authorization.AspNetCore;
using GraphQL.Server.Transports.AspNetCore;
using GraphQL.Server.Ui.Altair;
using GraphQL.Server.Ui.GraphiQL;
Expand Down Expand Up @@ -31,8 +30,7 @@ public void ConfigureServices(IServiceCollection services)
services
.AddRouting()
.AddSingleton<IChat, Chat>()
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
.AddTransient<IAuthorizationErrorMessageBuilder, DefaultAuthorizationErrorMessageBuilder>(); // required by CustomErrorInfoProvider
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment());

services.AddGraphQL(builder => builder
.AddApolloTracing()
Expand Down
14 changes: 0 additions & 14 deletions src/Authorization.AspNetCore/Authorization.AspNetCore.csproj

This file was deleted.

34 changes: 0 additions & 34 deletions src/Authorization.AspNetCore/AuthorizationError.cs

This file was deleted.

214 changes: 0 additions & 214 deletions src/Authorization.AspNetCore/AuthorizationValidationRule.cs

This file was deleted.

Loading