Skip to content

Commit ce417b5

Browse files
committed
Bump GraphQL Version, update rules
1 parent 395d629 commit ce417b5

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/GraphQL.Authorization/AuthorizationPolicyBuilder.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23

34
namespace GraphQL.Authorization
45
{
@@ -24,9 +25,16 @@ public AuthorizationPolicyBuilder RequireClaim(string claimType)
2425
return this;
2526
}
2627

27-
public AuthorizationPolicyBuilder RequireClaim(string claimType, params string[] requiredValues)
28+
public AuthorizationPolicyBuilder RequireClaim(string claimType, params string[] allowedValues)
2829
{
29-
var requirement = new ClaimAuthorizationRequirement(claimType, requiredValues);
30+
var requirement = new ClaimAuthorizationRequirement(claimType, allowedValues);
31+
_requirements.Add(requirement);
32+
return this;
33+
}
34+
35+
public AuthorizationPolicyBuilder RequireClaim(string claimType, IEnumerable<string> allowedValues, IEnumerable<string> displayValues)
36+
{
37+
var requirement = new ClaimAuthorizationRequirement(claimType, allowedValues, displayValues);
3038
_requirements.Add(requirement);
3139
return this;
3240
}
Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GraphQL.Language.AST;
2+
using GraphQL.Types;
23
using GraphQL.Validation;
34

45
namespace GraphQL.Authorization
@@ -18,32 +19,52 @@ public INodeVisitor Validate(ValidationContext context)
1819

1920
return new EnterLeaveListener(_ =>
2021
{
21-
// this could leak info about hidden fields in error messages
22+
var operationType = OperationType.Query;
23+
24+
// this could leak info about hidden fields or types in error messages
2225
// it would be better to implement a filter on the Schema so it
2326
// acts as if they just don't exist vs. an auth denied error
2427
// - filtering the Schema is not currently supported
28+
29+
_.Match<Operation>(astType =>
30+
{
31+
operationType = astType.OperationType;
32+
33+
var type = context.TypeInfo.GetLastType();
34+
CheckAuth(astType, type, userContext, context, operationType);
35+
});
36+
2537
_.Match<Field>(fieldAst =>
2638
{
2739
var fieldDef = context.TypeInfo.GetFieldDef();
40+
CheckAuth(fieldAst, fieldDef, userContext, context, operationType);
41+
});
42+
});
43+
}
2844

29-
if (!fieldDef.RequiresAuthorization()) return;
45+
private void CheckAuth(
46+
INode node,
47+
IProvideMetadata type,
48+
IProvideClaimsPrincipal userContext,
49+
ValidationContext context,
50+
OperationType operationType)
51+
{
52+
if (type == null || !type.RequiresAuthorization()) return;
3053

31-
var result = fieldDef
32-
.Authorize(userContext?.User, context.UserContext, _evaluator)
33-
.GetAwaiter()
34-
.GetResult();
54+
var result = type
55+
.Authorize(userContext?.User, context.UserContext, _evaluator)
56+
.GetAwaiter()
57+
.GetResult();
3558

36-
if (result.Succeeded) return;
59+
if (result.Succeeded) return;
3760

38-
var errors = string.Join("\n", result.Errors);
61+
var errors = string.Join("\n", result.Errors);
3962

40-
context.ReportError(new ValidationError(
41-
context.OriginalQuery,
42-
"authorization",
43-
$"You are not authorized to run this query.\n{errors}",
44-
fieldAst));
45-
});
46-
});
63+
context.ReportError(new ValidationError(
64+
context.OriginalQuery,
65+
"authorization",
66+
$"You are not authorized to run this {operationType.ToString().ToLower()}.\n{errors}",
67+
node));
4768
}
4869
}
4970
}

src/GraphQL.Authorization/ClaimAuthorizationRequirement.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@ namespace GraphQL.Authorization
88
public class ClaimAuthorizationRequirement : IAuthorizationRequirement
99
{
1010
private readonly string _claimType;
11+
private readonly IEnumerable<string> _displayValues;
1112
private readonly IEnumerable<string> _allowedValues;
1213

1314
public ClaimAuthorizationRequirement(string claimType)
14-
: this(claimType, null)
15+
: this(claimType, null, null)
1516
{
1617
}
1718

1819
public ClaimAuthorizationRequirement(string claimType, IEnumerable<string> allowedValues)
20+
: this(claimType, allowedValues, null)
21+
{
22+
}
23+
24+
public ClaimAuthorizationRequirement(string claimType, IEnumerable<string> allowedValues, IEnumerable<string> displayValues)
1925
{
2026
_claimType = claimType;
2127
_allowedValues = allowedValues ?? new List<string>();
28+
_displayValues = displayValues;
2229
}
2330

2431
public Task Authorize(AuthorizationContext context)
2532
{
2633
var found = false;
27-
if(_allowedValues == null || !_allowedValues.Any())
34+
if (_allowedValues == null || !_allowedValues.Any())
2835
{
2936
found = context.User.Claims.Any(
3037
c => string.Equals(c.Type, _claimType, StringComparison.OrdinalIgnoreCase));
@@ -40,7 +47,7 @@ public Task Authorize(AuthorizationContext context)
4047
{
4148
if (_allowedValues != null && _allowedValues.Any())
4249
{
43-
var values = string.Join(", ", _allowedValues);
50+
var values = string.Join(", ", _displayValues ?? _allowedValues);
4451
context.ReportError($"Required claim '{_claimType}' with any value of '{values}' is not present.");
4552
}
4653
else

src/GraphQL.Authorization/GraphQL.Authorization.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="GraphQL" Version="0.18.0-alpha-754" />
30-
<PackageReference Include="GraphQL-Parser" Version="2.0.0" />
29+
<PackageReference Include="GraphQL" Version="2.0.0-alpha-783" />
3130
</ItemGroup>
3231

3332
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">

0 commit comments

Comments
 (0)