Skip to content

Commit 625069e

Browse files
authored
Add AuthorizeWith support off ConnectionBuilder (#80)
1 parent 31e8693 commit 625069e

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ branches:
1919

2020
install:
2121
- ps: Install-Product node LTS
22-
- ps: choco install dotnetcore-sdk --no-progress --confirm --version 2.2.104
22+
- ps: choco install dotnetcore-sdk --no-progress --confirm --version 3.1.401
2323
- node --version
2424
- npm --version
2525
- dotnet --version

src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using GraphQL;
33
using GraphQL.Types;
4+
using GraphQL.Types.Relay.DataObjects;
45
using Xunit;
56

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

16-
ShouldPassRule(_=>
17+
ShouldPassRule(_ =>
1718
{
1819
_.Query = @"query { post }";
1920
_.Schema = BasicSchema();
@@ -32,7 +33,7 @@ public void class_policy_fail()
3233
_.RequireClaim("admin");
3334
});
3435

35-
ShouldFailRule(_=>
36+
ShouldFailRule(_ =>
3637
{
3738
_.Query = @"query { post }";
3839
_.Schema = BasicSchema();
@@ -45,7 +46,7 @@ public void field_policy_success()
4546
Settings.AddPolicy("ClassPolicy", _ => _.RequireClaim("admin"));
4647
Settings.AddPolicy("FieldPolicy", _ => _.RequireClaim("admin"));
4748

48-
ShouldPassRule(_=>
49+
ShouldPassRule(_ =>
4950
{
5051
_.Query = @"query { post }";
5152
_.Schema = BasicSchema();
@@ -64,7 +65,7 @@ public void field_policy_fail()
6465
_.RequireClaim("admin");
6566
});
6667

67-
ShouldFailRule(_=>
68+
ShouldFailRule(_ =>
6869
{
6970
_.Query = @"query { post }";
7071
_.Schema = BasicSchema();
@@ -79,7 +80,7 @@ public void nested_type_policy_success()
7980
_.RequireClaim("admin");
8081
});
8182

82-
ShouldPassRule(_=>
83+
ShouldPassRule(_ =>
8384
{
8485
_.Query = @"query { post }";
8586
_.Schema = NestedSchema();
@@ -98,7 +99,7 @@ public void nested_type_policy_fail()
9899
_.RequireClaim("admin");
99100
});
100101

101-
ShouldFailRule(_=>
102+
ShouldFailRule(_ =>
102103
{
103104
_.Query = @"query { post }";
104105
_.Schema = NestedSchema();
@@ -113,7 +114,7 @@ public void nested_type_list_policy_fail()
113114
_.RequireClaim("admin");
114115
});
115116

116-
ShouldFailRule(_=>
117+
ShouldFailRule(_ =>
117118
{
118119
_.Query = @"query { posts }";
119120
_.Schema = NestedSchema();
@@ -128,7 +129,7 @@ public void nested_type_list_non_null_policy_fail()
128129
_.RequireClaim("admin");
129130
});
130131

131-
ShouldFailRule(_=>
132+
ShouldFailRule(_ =>
132133
{
133134
_.Query = @"query { postsNonNull }";
134135
_.Schema = NestedSchema();
@@ -143,7 +144,7 @@ public void passes_with_claim_on_input_type()
143144
_.RequireClaim("admin");
144145
});
145146

146-
ShouldPassRule(_=>
147+
ShouldPassRule(_ =>
147148
{
148149
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
149150
_.Schema = TypedSchema();
@@ -162,7 +163,7 @@ public void fails_on_missing_claim_on_input_type()
162163
_.RequireClaim("admin");
163164
});
164165

165-
ShouldFailRule(_=>
166+
ShouldFailRule(_ =>
166167
{
167168
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
168169
_.Schema = TypedSchema();
@@ -187,6 +188,35 @@ public void passes_with_multiple_policies_on_field_and_single_on_input_type()
187188
});
188189
}
189190

191+
[Fact]
192+
public void passes_with_policy_on_connection_type()
193+
{
194+
Settings.AddPolicy("ConnectionPolicy", _ => _.RequireClaim("admin"));
195+
196+
ShouldPassRule(_ =>
197+
{
198+
_.Query = @"query { posts { items { id } } }";
199+
_.Schema = TypedSchema();
200+
_.User = CreatePrincipal(claims: new Dictionary<string, string>
201+
{
202+
{ "Admin", "true" }
203+
});
204+
});
205+
}
206+
207+
[Fact]
208+
public void fails_on_missing_claim_on_connection_type()
209+
{
210+
Settings.AddPolicy("ConnectionPolicy", _ => _.RequireClaim("admin"));
211+
212+
ShouldFailRule(_ =>
213+
{
214+
_.Query = @"query { posts { items { id } } }";
215+
_.Schema = TypedSchema();
216+
_.User = CreatePrincipal();
217+
});
218+
}
219+
190220
private ISchema BasicSchema()
191221
{
192222
var defs = @"
@@ -258,9 +288,17 @@ public class Post
258288
public string Id { get; set; }
259289
}
260290

291+
public class PostGraphType : ObjectGraphType<Post>
292+
{
293+
public PostGraphType()
294+
{
295+
Field(p => p.Id);
296+
}
297+
}
298+
261299
public class Author
262300
{
263-
public string Name { get; set;}
301+
public string Name { get; set; }
264302
}
265303

266304
private ISchema TypedSchema()
@@ -272,6 +310,11 @@ private ISchema TypedSchema()
272310
resolve: context => "testing"
273311
);
274312

313+
query.Connection<PostGraphType>()
314+
.Name("posts")
315+
.AuthorizeWith("ConnectionPolicy")
316+
.Resolve(ctx => new Connection<Post>());
317+
275318
query.Field<StringGraphType>(
276319
"project",
277320
arguments: new QueryArguments(new QueryArgument<AuthorInputType> { Name = "input" }),

src/GraphQL.Authorization.Tests/GraphQL.Authorization.Tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
55
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
66
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
77
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
88
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
99
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
10-
<IsTestProject>true</IsTestProject>
1110
</PropertyGroup>
1211

1312
<ItemGroup>
1413
<PackageReference Include="GraphQL.NewtonsoftJson" Version="3.0.0-preview-1648" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
1615
<PackageReference Include="Shouldly" Version="3.0.2" />
1716
<PackageReference Include="xunit" Version="2.4.1" />
1817
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />

src/GraphQL.Authorization/AuthorizationMetadataExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public static FieldBuilder<TSourceType, TReturnType> AuthorizeWith<TSourceType,
4747
return builder;
4848
}
4949

50+
public static ConnectionBuilder<TSourceType> AuthorizeWith<TSourceType>(
51+
this ConnectionBuilder<TSourceType> builder, string policy)
52+
{
53+
builder.FieldType.AuthorizeWith(policy);
54+
return builder;
55+
}
56+
5057
public static List<string> GetPolicies(this IProvideMetadata type)
5158
{
5259
return type.GetMetadata(PolicyKey, new List<string>());

src/Harness/Harness.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
@@ -13,10 +13,10 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="GraphQL" Version="3.0.0-preview-1648" />
17-
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="3.5.0-alpha0027" />
18-
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0027" />
19-
<PackageReference Include="Microsoft.AspNetCore.All" />
16+
<PackageReference Include="GraphQL" Version="3.0.0-preview-1712" />
17+
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="3.5.0-alpha0072" />
18+
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="3.5.0-alpha0072" />
19+
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0072" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

src/Harness/Startup.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,19 @@ type Query {
6363
services.AddGraphQL(options =>
6464
{
6565
options.ExposeExceptions = true;
66-
}).AddUserContextBuilder(context => new GraphQLUserContext { User = context.User });
67-
68-
services.AddMvc();
66+
})
67+
.AddSystemTextJson()
68+
.AddUserContextBuilder(context => new GraphQLUserContext { User = context.User });
6969
}
7070

71-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
71+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7272
{
7373
app.UseDeveloperExceptionPage();
7474

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

7777
app.UseGraphQL<ISchema>("/graphql");
78-
app.UseGraphiQLServer(new GraphiQLOptions());
79-
80-
app.UseMvc();
78+
app.UseGraphiQLServer();
8179
}
8280
}
8381
}

0 commit comments

Comments
 (0)