public virtual async Task <ResourceValidationResult> ValidateRequestedResourcesAsync(ResourceValidationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            ParsedScopesResult       parsedScopesResult = _scopeParser.ParseScopeValues(request.Scopes);
            ResourceValidationResult result             = new ResourceValidationResult();

            if (!parsedScopesResult.Succeeded)
            {
                foreach (ParsedScopeValidationError error in parsedScopesResult.Errors)
                {
                    _logger.LogError("Invalid parsed scope {scope}, message: {error}", error.RawValue, error.Error);
                    result.InvalidScopes.Add(error.RawValue);
                }
                return(result);
            }
            string[]  scopeNames         = parsedScopesResult.ParsedScopes.Select((ParsedScopeValue x) => x.ParsedName).Distinct().ToArray();
            Resources resourcesFromStore = await _store.FindEnabledResourcesByScopeAsync(scopeNames);

            foreach (ParsedScopeValue parsedScope in parsedScopesResult.ParsedScopes)
            {
                await ValidateScopeAsync(request.Client, resourcesFromStore, parsedScope, result);
            }
            if (result.InvalidScopes.Count > 0)
            {
                result.Resources.IdentityResources.Clear();
                result.Resources.ApiResources.Clear();
                result.Resources.ApiScopes.Clear();
                result.ParsedScopes.Clear();
            }
            return(result);
        }
Example #2
0
    /// <inheritdoc/>
    public ParsedScopesResult ParseScopeValues(IEnumerable <string> scopeValues)
    {
        using var activity = Tracing.ValidationActivitySource.StartActivity("DefaultScopeParser.ParseScopeValues");
        activity?.SetTag(Tracing.Properties.Scope, scopeValues.ToSpaceSeparatedString());

        if (scopeValues == null)
        {
            throw new ArgumentNullException(nameof(scopeValues));
        }

        var result = new ParsedScopesResult();

        foreach (var scopeValue in scopeValues)
        {
            var ctx = new ParseScopeContext(scopeValue);
            ParseScopeValue(ctx);

            if (ctx.Succeeded)
            {
                var parsedScope = ctx.ParsedName != null ?
                                  new ParsedScopeValue(ctx.RawValue, ctx.ParsedName, ctx.ParsedParameter) :
                                  new ParsedScopeValue(ctx.RawValue);

                result.ParsedScopes.Add(parsedScope);
            }
            else if (!ctx.Ignore)
            {
                result.Errors.Add(new ParsedScopeValidationError(scopeValue, ctx.Error));
            }
            else
            {
                _logger.LogDebug("Scope parsing ignoring scope {scope}", scopeValue);
            }
        }

        return(result);
    }
        /// <summary>
        /// Creates a resource validation result.
        /// </summary>
        /// <param name="store">The store.</param>
        /// <param name="parsedScopesResult">The parsed scopes.</param>
        /// <returns></returns>
        public static async Task <ResourceValidationResult> CreateResourceValidationResult(this IResourceStore store, ParsedScopesResult parsedScopesResult)
        {
            var validScopeValues = parsedScopesResult.ParsedScopes;
            var scopes           = validScopeValues.Select(x => x.ParsedName).ToArray();
            var resources        = await store.FindEnabledResourcesByScopeAsync(scopes);

            return(new ResourceValidationResult(resources, validScopeValues));
        }