@@ -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