Skip to content

Commit 6ffe099

Browse files
authored
Merge pull request #34 from bornlogic/fix/user-role-scope-bypass-validation
fix
2 parents be64f70 + 9f2b913 commit 6ffe099

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

Bornlogic.IdentityServer/Validation/Default/DefaultResourceValidator.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public virtual async Task<ResourceValidationResult> ValidateRequestedResourcesAs
7272
return result;
7373
}
7474

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+
7583
var scopeNames = parsedScopesResult.ParsedScopes.Select(x => x.ParsedName).Distinct().ToArray();
7684
var resourcesFromStore = await _store.FindEnabledResourcesByScopeAsync(scopeNames);
7785

@@ -88,13 +96,6 @@ public virtual async Task<ResourceValidationResult> ValidateRequestedResourcesAs
8896
await ValidateRequestRequiredScopeAsync(request.Client, requiredRequestResourcesFromStore, scope, result);
8997
}
9098

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-
9899
if (result.InvalidScopes.Count > 0)
99100
{
100101
result.Resources.IdentityResources.Clear();
@@ -119,11 +120,12 @@ protected virtual async Task ValidateScopeAsync(
119120
Resources resourcesFromStore,
120121
ParsedScopeValue requestedScope,
121122
ResourceValidationResult result,
122-
bool forceRequired)
123+
bool forceRequired,
124+
bool userHasLoginByPassRoleInClient)
123125
{
124126
if (requestedScope.ParsedName == IdentityServerConstants.StandardScopes.OfflineAccess)
125127
{
126-
if (await IsClientAllowedOfflineAccessAsync(client))
128+
if (userHasLoginByPassRoleInClient || await IsClientAllowedOfflineAccessAsync(client))
127129
{
128130
result.Resources.OfflineAccess = true;
129131
result.ParsedScopes.Add(new ParsedScopeValue(IdentityServerConstants.StandardScopes.OfflineAccess));
@@ -138,7 +140,7 @@ protected virtual async Task ValidateScopeAsync(
138140
var identity = resourcesFromStore.FindIdentityResourcesByScope(requestedScope.ParsedName);
139141
if (identity != null)
140142
{
141-
if (await IsClientAllowedIdentityResourceAsync(client, identity))
143+
if (userHasLoginByPassRoleInClient || await IsClientAllowedIdentityResourceAsync(client, identity))
142144
{
143145
result.ParsedScopes.Add(requestedScope);
144146
result.Resources.IdentityResources.Add(identity);
@@ -153,7 +155,7 @@ protected virtual async Task ValidateScopeAsync(
153155
var apiScope = resourcesFromStore.FindApiScope(requestedScope.ParsedName);
154156
if (apiScope != null)
155157
{
156-
if (await IsClientAllowedApiScopeAsync(client, apiScope))
158+
if (userHasLoginByPassRoleInClient || await IsClientAllowedApiScopeAsync(client, apiScope))
157159
{
158160
result.ParsedScopes.Add(requestedScope);
159161

@@ -182,7 +184,8 @@ protected virtual async Task ValidateScopeAsync(
182184
}
183185
}
184186

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)
186189
{
187190
if (requestedScope.ParsedName == IdentityServerConstants.StandardScopes.OfflineAccess)
188191
{
@@ -193,7 +196,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
193196
var identity = resourcesFromStore.FindIdentityResourcesByScope(requestedScope.ParsedName);
194197
if (identity != null)
195198
{
196-
if (!(await IsClientAllowedIdentityResourceAsync(client, identity)))
199+
if (!userHasLoginByPassRoleInClient && !(await IsClientAllowedIdentityResourceAsync(client, identity)))
197200
{
198201
result.InvalidScopes.Add(requestedScope.RawValue);
199202
}
@@ -203,7 +206,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
203206
var apiScope = resourcesFromStore.FindApiScope(requestedScope.ParsedName);
204207
if (apiScope != null)
205208
{
206-
if (!(await IsClientAllowedApiScopeAsync(client, apiScope)))
209+
if (!userHasLoginByPassRoleInClient && !(await IsClientAllowedApiScopeAsync(client, apiScope)))
207210
{
208211
result.InvalidScopes.Add(requestedScope.RawValue);
209212
}

0 commit comments

Comments
 (0)