Skip to content

Commit

Permalink
Add AuthorizeWith support off ConnectionBuilder (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoWilhelm authored Aug 16, 2020
1 parent 31e8693 commit 625069e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ branches:

install:
- ps: Install-Product node LTS
- ps: choco install dotnetcore-sdk --no-progress --confirm --version 2.2.104
- ps: choco install dotnetcore-sdk --no-progress --confirm --version 3.1.401
- node --version
- npm --version
- dotnet --version
Expand Down
65 changes: 54 additions & 11 deletions src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using GraphQL;
using GraphQL.Types;
using GraphQL.Types.Relay.DataObjects;
using Xunit;

namespace GraphQL.Authorization.Tests
Expand All @@ -13,7 +14,7 @@ public void class_policy_success()
Settings.AddPolicy("ClassPolicy", _ => _.RequireClaim("admin"));
Settings.AddPolicy("FieldPolicy", _ => _.RequireClaim("admin"));

ShouldPassRule(_=>
ShouldPassRule(_ =>
{
_.Query = @"query { post }";
_.Schema = BasicSchema();
Expand All @@ -32,7 +33,7 @@ public void class_policy_fail()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { post }";
_.Schema = BasicSchema();
Expand All @@ -45,7 +46,7 @@ public void field_policy_success()
Settings.AddPolicy("ClassPolicy", _ => _.RequireClaim("admin"));
Settings.AddPolicy("FieldPolicy", _ => _.RequireClaim("admin"));

ShouldPassRule(_=>
ShouldPassRule(_ =>
{
_.Query = @"query { post }";
_.Schema = BasicSchema();
Expand All @@ -64,7 +65,7 @@ public void field_policy_fail()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { post }";
_.Schema = BasicSchema();
Expand All @@ -79,7 +80,7 @@ public void nested_type_policy_success()
_.RequireClaim("admin");
});

ShouldPassRule(_=>
ShouldPassRule(_ =>
{
_.Query = @"query { post }";
_.Schema = NestedSchema();
Expand All @@ -98,7 +99,7 @@ public void nested_type_policy_fail()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { post }";
_.Schema = NestedSchema();
Expand All @@ -113,7 +114,7 @@ public void nested_type_list_policy_fail()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { posts }";
_.Schema = NestedSchema();
Expand All @@ -128,7 +129,7 @@ public void nested_type_list_non_null_policy_fail()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { postsNonNull }";
_.Schema = NestedSchema();
Expand All @@ -143,7 +144,7 @@ public void passes_with_claim_on_input_type()
_.RequireClaim("admin");
});

ShouldPassRule(_=>
ShouldPassRule(_ =>
{
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
_.Schema = TypedSchema();
Expand All @@ -162,7 +163,7 @@ public void fails_on_missing_claim_on_input_type()
_.RequireClaim("admin");
});

ShouldFailRule(_=>
ShouldFailRule(_ =>
{
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
_.Schema = TypedSchema();
Expand All @@ -187,6 +188,35 @@ public void passes_with_multiple_policies_on_field_and_single_on_input_type()
});
}

[Fact]
public void passes_with_policy_on_connection_type()
{
Settings.AddPolicy("ConnectionPolicy", _ => _.RequireClaim("admin"));

ShouldPassRule(_ =>
{
_.Query = @"query { posts { items { id } } }";
_.Schema = TypedSchema();
_.User = CreatePrincipal(claims: new Dictionary<string, string>
{
{ "Admin", "true" }
});
});
}

[Fact]
public void fails_on_missing_claim_on_connection_type()
{
Settings.AddPolicy("ConnectionPolicy", _ => _.RequireClaim("admin"));

ShouldFailRule(_ =>
{
_.Query = @"query { posts { items { id } } }";
_.Schema = TypedSchema();
_.User = CreatePrincipal();
});
}

private ISchema BasicSchema()
{
var defs = @"
Expand Down Expand Up @@ -258,9 +288,17 @@ public class Post
public string Id { get; set; }
}

public class PostGraphType : ObjectGraphType<Post>
{
public PostGraphType()
{
Field(p => p.Id);
}
}

public class Author
{
public string Name { get; set;}
public string Name { get; set; }
}

private ISchema TypedSchema()
Expand All @@ -272,6 +310,11 @@ private ISchema TypedSchema()
resolve: context => "testing"
);

query.Connection<PostGraphType>()
.Name("posts")
.AuthorizeWith("ConnectionPolicy")
.Resolve(ctx => new Connection<Post>());

query.Field<StringGraphType>(
"project",
arguments: new QueryArguments(new QueryArgument<AuthorInputType> { Name = "input" }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GraphQL.NewtonsoftJson" Version="3.0.0-preview-1648" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
Expand Down
7 changes: 7 additions & 0 deletions src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public static FieldBuilder<TSourceType, TReturnType> AuthorizeWith<TSourceType,
return builder;
}

public static ConnectionBuilder<TSourceType> AuthorizeWith<TSourceType>(
this ConnectionBuilder<TSourceType> builder, string policy)
{
builder.FieldType.AuthorizeWith(policy);
return builder;
}

public static List<string> GetPolicies(this IProvideMetadata type)
{
return type.GetMetadata(PolicyKey, new List<string>());
Expand Down
10 changes: 5 additions & 5 deletions src/Harness/Harness.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,10 +13,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GraphQL" Version="3.0.0-preview-1648" />
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="3.5.0-alpha0027" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0027" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="GraphQL" Version="3.0.0-preview-1712" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="3.5.0-alpha0072" />
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="3.5.0-alpha0072" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0072" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 5 additions & 7 deletions src/Harness/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,19 @@ type Query {
services.AddGraphQL(options =>
{
options.ExposeExceptions = true;
}).AddUserContextBuilder(context => new GraphQLUserContext { User = context.User });

services.AddMvc();
})
.AddSystemTextJson()
.AddUserContextBuilder(context => new GraphQLUserContext { User = context.User });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();

var validationRules = app.ApplicationServices.GetServices<IValidationRule>();

app.UseGraphQL<ISchema>("/graphql");
app.UseGraphiQLServer(new GraphiQLOptions());

app.UseMvc();
app.UseGraphiQLServer();
}
}
}

0 comments on commit 625069e

Please sign in to comment.