Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #34

Merged
merged 1 commit into from
Feb 6, 2025
Merged

fix #34

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public virtual async Task<ResourceValidationResult> ValidateRequestedResourcesAs
return result;
}

var subjectIdOrDefault = request.Subject?.GetSubjectIdOrDefault();

var userHasLoginByPassRoleInClient = !string.IsNullOrEmpty(subjectIdOrDefault) &&
await _clientUserRoleService.UserHasLoginByPassRoleInClient(
subjectIdOrDefault, request.Client,
_clientRoleOptions?.Value
?.ValidUserRolesToBypassClientScopeValidation);

var scopeNames = parsedScopesResult.ParsedScopes.Select(x => x.ParsedName).Distinct().ToArray();
var resourcesFromStore = await _store.FindEnabledResourcesByScopeAsync(scopeNames);

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

var subjectIdOrDefault = request.Subject?.GetSubjectIdOrDefault();

if (!string.IsNullOrEmpty(subjectIdOrDefault) && await _clientUserRoleService.UserHasLoginByPassRoleInClient(subjectIdOrDefault, request.Client, _clientRoleOptions?.Value?.ValidUserRolesToBypassClientScopeValidation))
{
result.InvalidScopes.Clear();
}

if (result.InvalidScopes.Count > 0)
{
result.Resources.IdentityResources.Clear();
Expand All @@ -119,11 +120,12 @@ protected virtual async Task ValidateScopeAsync(
Resources resourcesFromStore,
ParsedScopeValue requestedScope,
ResourceValidationResult result,
bool forceRequired)
bool forceRequired,
bool userHasLoginByPassRoleInClient)
{
if (requestedScope.ParsedName == IdentityServerConstants.StandardScopes.OfflineAccess)
{
if (await IsClientAllowedOfflineAccessAsync(client))
if (userHasLoginByPassRoleInClient || await IsClientAllowedOfflineAccessAsync(client))
{
result.Resources.OfflineAccess = true;
result.ParsedScopes.Add(new ParsedScopeValue(IdentityServerConstants.StandardScopes.OfflineAccess));
Expand All @@ -138,7 +140,7 @@ protected virtual async Task ValidateScopeAsync(
var identity = resourcesFromStore.FindIdentityResourcesByScope(requestedScope.ParsedName);
if (identity != null)
{
if (await IsClientAllowedIdentityResourceAsync(client, identity))
if (userHasLoginByPassRoleInClient || await IsClientAllowedIdentityResourceAsync(client, identity))
{
result.ParsedScopes.Add(requestedScope);
result.Resources.IdentityResources.Add(identity);
Expand All @@ -153,7 +155,7 @@ protected virtual async Task ValidateScopeAsync(
var apiScope = resourcesFromStore.FindApiScope(requestedScope.ParsedName);
if (apiScope != null)
{
if (await IsClientAllowedApiScopeAsync(client, apiScope))
if (userHasLoginByPassRoleInClient || await IsClientAllowedApiScopeAsync(client, apiScope))
{
result.ParsedScopes.Add(requestedScope);

Expand Down Expand Up @@ -182,7 +184,8 @@ protected virtual async Task ValidateScopeAsync(
}
}

protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Resources resourcesFromStore, ParsedScopeValue requestedScope, ResourceValidationResult result)
protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Resources resourcesFromStore, ParsedScopeValue requestedScope, ResourceValidationResult result,
bool userHasLoginByPassRoleInClient)
{
if (requestedScope.ParsedName == IdentityServerConstants.StandardScopes.OfflineAccess)
{
Expand All @@ -193,7 +196,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
var identity = resourcesFromStore.FindIdentityResourcesByScope(requestedScope.ParsedName);
if (identity != null)
{
if (!(await IsClientAllowedIdentityResourceAsync(client, identity)))
if (!userHasLoginByPassRoleInClient && !(await IsClientAllowedIdentityResourceAsync(client, identity)))
{
result.InvalidScopes.Add(requestedScope.RawValue);
}
Expand All @@ -203,7 +206,7 @@ protected virtual async Task ValidateRequestRequiredScopeAsync(Client client, Re
var apiScope = resourcesFromStore.FindApiScope(requestedScope.ParsedName);
if (apiScope != null)
{
if (!(await IsClientAllowedApiScopeAsync(client, apiScope)))
if (!userHasLoginByPassRoleInClient && !(await IsClientAllowedApiScopeAsync(client, apiScope)))
{
result.InvalidScopes.Add(requestedScope.RawValue);
}
Expand Down