diff --git a/appveyor.yml b/appveyor.yml index ed7e586..bfbe3f9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 diff --git a/src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs b/src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs index e1237cb..572208b 100644 --- a/src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs +++ b/src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using GraphQL; using GraphQL.Types; +using GraphQL.Types.Relay.DataObjects; using Xunit; namespace GraphQL.Authorization.Tests @@ -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(); @@ -32,7 +33,7 @@ public void class_policy_fail() _.RequireClaim("admin"); }); - ShouldFailRule(_=> + ShouldFailRule(_ => { _.Query = @"query { post }"; _.Schema = BasicSchema(); @@ -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(); @@ -64,7 +65,7 @@ public void field_policy_fail() _.RequireClaim("admin"); }); - ShouldFailRule(_=> + ShouldFailRule(_ => { _.Query = @"query { post }"; _.Schema = BasicSchema(); @@ -79,7 +80,7 @@ public void nested_type_policy_success() _.RequireClaim("admin"); }); - ShouldPassRule(_=> + ShouldPassRule(_ => { _.Query = @"query { post }"; _.Schema = NestedSchema(); @@ -98,7 +99,7 @@ public void nested_type_policy_fail() _.RequireClaim("admin"); }); - ShouldFailRule(_=> + ShouldFailRule(_ => { _.Query = @"query { post }"; _.Schema = NestedSchema(); @@ -113,7 +114,7 @@ public void nested_type_list_policy_fail() _.RequireClaim("admin"); }); - ShouldFailRule(_=> + ShouldFailRule(_ => { _.Query = @"query { posts }"; _.Schema = NestedSchema(); @@ -128,7 +129,7 @@ public void nested_type_list_non_null_policy_fail() _.RequireClaim("admin"); }); - ShouldFailRule(_=> + ShouldFailRule(_ => { _.Query = @"query { postsNonNull }"; _.Schema = NestedSchema(); @@ -143,7 +144,7 @@ public void passes_with_claim_on_input_type() _.RequireClaim("admin"); }); - ShouldPassRule(_=> + ShouldPassRule(_ => { _.Query = @"query { author(input: { name: ""Quinn"" }) }"; _.Schema = TypedSchema(); @@ -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(); @@ -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 + { + { "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 = @" @@ -258,9 +288,17 @@ public class Post public string Id { get; set; } } + public class PostGraphType : ObjectGraphType + { + public PostGraphType() + { + Field(p => p.Id); + } + } + public class Author { - public string Name { get; set;} + public string Name { get; set; } } private ISchema TypedSchema() @@ -272,6 +310,11 @@ private ISchema TypedSchema() resolve: context => "testing" ); + query.Connection() + .Name("posts") + .AuthorizeWith("ConnectionPolicy") + .Resolve(ctx => new Connection()); + query.Field( "project", arguments: new QueryArguments(new QueryArgument { Name = "input" }), diff --git a/src/GraphQL.Authorization.Tests/GraphQL.Authorization.Tests.csproj b/src/GraphQL.Authorization.Tests/GraphQL.Authorization.Tests.csproj index daa8393..d5db143 100644 --- a/src/GraphQL.Authorization.Tests/GraphQL.Authorization.Tests.csproj +++ b/src/GraphQL.Authorization.Tests/GraphQL.Authorization.Tests.csproj @@ -1,18 +1,17 @@  - netcoreapp2.2 + netcoreapp3.1 true false false false false - true - + diff --git a/src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs b/src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs index 10759df..2a6672b 100644 --- a/src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs +++ b/src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs @@ -47,6 +47,13 @@ public static FieldBuilder AuthorizeWith AuthorizeWith( + this ConnectionBuilder builder, string policy) + { + builder.FieldType.AuthorizeWith(policy); + return builder; + } + public static List GetPolicies(this IProvideMetadata type) { return type.GetMetadata(PolicyKey, new List()); diff --git a/src/Harness/Harness.csproj b/src/Harness/Harness.csproj index b6328ac..6900903 100644 --- a/src/Harness/Harness.csproj +++ b/src/Harness/Harness.csproj @@ -1,7 +1,7 @@ - netcoreapp2.0 + netcoreapp3.1 @@ -13,10 +13,10 @@ - - - - + + + + diff --git a/src/Harness/Startup.cs b/src/Harness/Startup.cs index 4f5c6a7..eff1029 100644 --- a/src/Harness/Startup.cs +++ b/src/Harness/Startup.cs @@ -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(); app.UseGraphQL("/graphql"); - app.UseGraphiQLServer(new GraphiQLOptions()); - - app.UseMvc(); + app.UseGraphiQLServer(); } } }