@@ -72,6 +72,14 @@ public virtual async Task<ResourceValidationResult> ValidateRequestedResourcesAs
72
72
return result ;
73
73
}
74
74
75
+ var subjectIdOrDefault = request . Subject ? . GetSubjectIdOrDefault ( ) ;
76
+
77
+ var userHasLoginByPassRoleInClient = ! string . IsNullOrEmpty ( subjectIdOrDefault ) &&
78
+ await _clientUserRoleService . UserHasLoginByPassRoleInClient (
79
+ subjectIdOrDefault , request . Client ,
80
+ _clientRoleOptions ? . Value
81
+ ? . ValidUserRolesToBypassClientScopeValidation ) ;
82
+
75
83
var scopeNames = parsedScopesResult . ParsedScopes . Select ( x => x . ParsedName ) . Distinct ( ) . ToArray ( ) ;
76
84
var resourcesFromStore = await _store . FindEnabledResourcesByScopeAsync ( scopeNames ) ;
77
85
@@ -88,13 +96,6 @@ public virtual async Task<ResourceValidationResult> ValidateRequestedResourcesAs
88
96
await ValidateRequestRequiredScopeAsync ( request . Client , requiredRequestResourcesFromStore , scope , result ) ;
89
97
}
90
98
91
- var subjectIdOrDefault = request . Subject ? . GetSubjectIdOrDefault ( ) ;
92
-
93
- if ( ! string . IsNullOrEmpty ( subjectIdOrDefault ) && await _clientUserRoleService . UserHasLoginByPassRoleInClient ( subjectIdOrDefault , request . Client , _clientRoleOptions ? . Value ? . ValidUserRolesToBypassClientScopeValidation ) )
94
- {
95
- result . InvalidScopes . Clear ( ) ;
96
- }
97
-
98
99
if ( result . InvalidScopes . Count > 0 )
99
100
{
100
101
result . Resources . IdentityResources . Clear ( ) ;
@@ -119,11 +120,12 @@ protected virtual async Task ValidateScopeAsync(
119
120
Resources resourcesFromStore ,
120
121
ParsedScopeValue requestedScope ,
121
122
ResourceValidationResult result ,
122
- bool forceRequired )
123
+ bool forceRequired ,
124
+ bool userHasLoginByPassRoleInClient )
123
125
{
124
126
if ( requestedScope . ParsedName == IdentityServerConstants . StandardScopes . OfflineAccess )
125
127
{
126
- if ( await IsClientAllowedOfflineAccessAsync ( client ) )
128
+ if ( userHasLoginByPassRoleInClient || await IsClientAllowedOfflineAccessAsync ( client ) )
127
129
{
128
130
result . Resources . OfflineAccess = true ;
129
131
result . ParsedScopes . Add ( new ParsedScopeValue ( IdentityServerConstants . StandardScopes . OfflineAccess ) ) ;
@@ -138,7 +140,7 @@ protected virtual async Task ValidateScopeAsync(
138
140
var identity = resourcesFromStore . FindIdentityResourcesByScope ( requestedScope . ParsedName ) ;
139
141
if ( identity != null )
140
142
{
141
- if ( await IsClientAllowedIdentityResourceAsync ( client , identity ) )
143
+ if ( userHasLoginByPassRoleInClient || await IsClientAllowedIdentityResourceAsync ( client , identity ) )
142
144
{
143
145
result . ParsedScopes . Add ( requestedScope ) ;
144
146
result . Resources . IdentityResources . Add ( identity ) ;
@@ -153,7 +155,7 @@ protected virtual async Task ValidateScopeAsync(
153
155
var apiScope = resourcesFromStore . FindApiScope ( requestedScope . ParsedName ) ;
154
156
if ( apiScope != null )
155
157
{
156
- if ( await IsClientAllowedApiScopeAsync ( client , apiScope ) )
158
+ if ( userHasLoginByPassRoleInClient || await IsClientAllowedApiScopeAsync ( client , apiScope ) )
157
159
{
158
160
result . ParsedScopes . Add ( requestedScope ) ;
159
161
@@ -182,7 +184,8 @@ protected virtual async Task ValidateScopeAsync(
182
184
}
183
185
}
184
186
185
- protected virtual async Task ValidateRequestRequiredScopeAsync ( Client client , Resources resourcesFromStore , ParsedScopeValue requestedScope , ResourceValidationResult result )
187
+ protected virtual async Task ValidateRequestRequiredScopeAsync ( Client client , Resources resourcesFromStore , ParsedScopeValue requestedScope , ResourceValidationResult result ,
188
+ bool userHasLoginByPassRoleInClient )
186
189
{
187
190
if ( requestedScope . ParsedName == IdentityServerConstants . StandardScopes . OfflineAccess )
188
191
{
@@ -193,7 +196,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
193
196
var identity = resourcesFromStore . FindIdentityResourcesByScope ( requestedScope . ParsedName ) ;
194
197
if ( identity != null )
195
198
{
196
- if ( ! ( await IsClientAllowedIdentityResourceAsync ( client , identity ) ) )
199
+ if ( ! userHasLoginByPassRoleInClient && ! ( await IsClientAllowedIdentityResourceAsync ( client , identity ) ) )
197
200
{
198
201
result . InvalidScopes . Add ( requestedScope . RawValue ) ;
199
202
}
@@ -203,7 +206,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
203
206
var apiScope = resourcesFromStore . FindApiScope ( requestedScope . ParsedName ) ;
204
207
if ( apiScope != null )
205
208
{
206
- if ( ! ( await IsClientAllowedApiScopeAsync ( client , apiScope ) ) )
209
+ if ( ! userHasLoginByPassRoleInClient && ! ( await IsClientAllowedApiScopeAsync ( client , apiScope ) ) )
207
210
{
208
211
result . InvalidScopes . Add ( requestedScope . RawValue ) ;
209
212
}
0 commit comments