Skip to content

Commit a193954

Browse files
authored
Change private fields to public properties for ClaimAuthorizationRequirement (#178)
1 parent e006387 commit a193954

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/BasicSample/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ type Query {
4444
// remove claims to see the failure
4545
var authorizedUser = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("role", "Admin") }));
4646

47-
string json = await schema.ExecuteAsync(_ =>
47+
string json = await schema.ExecuteAsync(options =>
4848
{
49-
_.Query = "{ viewer { id name } }";
50-
_.ValidationRules = serviceProvider
49+
options.Query = "{ viewer { id name } }";
50+
options.ValidationRules = serviceProvider
5151
.GetServices<IValidationRule>()
5252
.Concat(DocumentValidator.CoreRules);
53-
_.RequestServices = serviceProvider;
54-
_.UserContext = new GraphQLUserContext { User = authorizedUser };
53+
options.RequestServices = serviceProvider;
54+
options.UserContext = new GraphQLUserContext { User = authorizedUser };
5555
});
5656

5757
Console.WriteLine(json);

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionPrefix>4.0.0-preview</VersionPrefix>
4+
<VersionPrefix>4.1.0-preview</VersionPrefix>
55
<LangVersion>8.0</LangVersion>
66
<Authors>Joe McBride</Authors>
77
<PackageLicenseExpression>MIT</PackageLicenseExpression>

src/GraphQL.Authorization.ApiTests/GraphQL.Authorization.approved.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ namespace GraphQL.Authorization
6363
public ClaimAuthorizationRequirement(string claimType, System.Collections.Generic.IEnumerable<string> allowedValues) { }
6464
public ClaimAuthorizationRequirement(string claimType, params string[] allowedValues) { }
6565
public ClaimAuthorizationRequirement(string claimType, System.Collections.Generic.IEnumerable<string> allowedValues, System.Collections.Generic.IEnumerable<string> displayValues) { }
66+
public System.Collections.Generic.IEnumerable<string> AllowedValues { get; }
67+
public string ClaimType { get; }
68+
public System.Collections.Generic.IEnumerable<string> DisplayValues { get; }
6669
public System.Threading.Tasks.Task Authorize(GraphQL.Authorization.AuthorizationContext context) { }
6770
}
6871
public interface IAuthorizationEvaluator

src/GraphQL.Authorization/Requirements/ClaimAuthorizationRequirement.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ namespace GraphQL.Authorization
1111
/// </summary>
1212
public class ClaimAuthorizationRequirement : IAuthorizationRequirement
1313
{
14-
private readonly string _claimType;
15-
private readonly IEnumerable<string> _displayValues;
16-
private readonly IEnumerable<string> _allowedValues;
17-
1814
/// <summary>
1915
/// Creates a new instance of <see cref="ClaimAuthorizationRequirement"/> with
2016
/// the specified claim type.
@@ -53,41 +49,58 @@ public ClaimAuthorizationRequirement(string claimType, params string[] allowedVa
5349
/// </summary>
5450
public ClaimAuthorizationRequirement(string claimType, IEnumerable<string> allowedValues, IEnumerable<string> displayValues)
5551
{
56-
_claimType = claimType ?? throw new ArgumentNullException(nameof(claimType));
57-
_allowedValues = allowedValues ?? Enumerable.Empty<string>();
58-
_displayValues = displayValues;
52+
ClaimType = claimType ?? throw new ArgumentNullException(nameof(claimType));
53+
AllowedValues = allowedValues ?? Enumerable.Empty<string>();
54+
DisplayValues = displayValues;
5955
}
6056

57+
/// <summary>
58+
/// Claim type that claims principal from <see cref="AuthorizationContext"/> should have.
59+
/// </summary>
60+
public string ClaimType { get; }
61+
62+
/// <summary>
63+
/// List of claim values, which, if present, the claim must match.
64+
/// </summary>
65+
public IEnumerable<string> AllowedValues { get; }
66+
67+
/// <summary>
68+
/// Specifies the set of displayed claim values that will be used
69+
/// to generate an error message if the requirement is not met.
70+
/// If null then values from <see cref="AllowedValues"/> are used.
71+
/// </summary>
72+
public IEnumerable<string> DisplayValues { get; }
73+
6174
/// <inheritdoc />
6275
public Task Authorize(AuthorizationContext context)
6376
{
6477
bool found = false;
6578

6679
if (context.User != null)
6780
{
68-
if (_allowedValues == null || !_allowedValues.Any())
81+
if (AllowedValues == null || !AllowedValues.Any())
6982
{
7083
found = context.User.Claims.Any(
71-
claim => string.Equals(claim.Type, _claimType, StringComparison.OrdinalIgnoreCase));
84+
claim => string.Equals(claim.Type, ClaimType, StringComparison.OrdinalIgnoreCase));
7285
}
7386
else
7487
{
7588
found = context.User.Claims.Any(
76-
claim => string.Equals(claim.Type, _claimType, StringComparison.OrdinalIgnoreCase)
77-
&& _allowedValues.Contains(claim.Value, StringComparer.Ordinal));
89+
claim => string.Equals(claim.Type, ClaimType, StringComparison.OrdinalIgnoreCase)
90+
&& AllowedValues.Contains(claim.Value, StringComparer.Ordinal));
7891
}
7992
}
8093

8194
if (!found)
8295
{
83-
if (_allowedValues != null && _allowedValues.Any())
96+
if (AllowedValues != null && AllowedValues.Any())
8497
{
85-
string values = string.Join(", ", _displayValues ?? _allowedValues);
86-
context.ReportError($"Required claim '{_claimType}' with any value of '{values}' is not present.");
98+
string values = string.Join(", ", DisplayValues ?? AllowedValues);
99+
context.ReportError($"Required claim '{ClaimType}' with any value of '{values}' is not present.");
87100
}
88101
else
89102
{
90-
context.ReportError($"Required claim '{_claimType}' is not present.");
103+
context.ReportError($"Required claim '{ClaimType}' is not present.");
91104
}
92105
}
93106

0 commit comments

Comments
 (0)