Example #1
0
        public TreeMatcher(
            IInlineConstraintResolver constraintFactory,
            ILogger logger,
            EndpointDataSource dataSource,
            EndpointSelector endpointSelector)
        {
            if (constraintFactory == null)
            {
                throw new ArgumentNullException(nameof(constraintFactory));
            }

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

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

            _constraintFactory = constraintFactory;
            _logger            = logger;
            _endpointSelector  = endpointSelector;
            _cache             = new DataSourceDependantCache <UrlMatchingTree[]>(dataSource, CreateTrees);
            _cache.EnsureInitialized();
        }
        /// <summary>
        /// Returns a filtered and sorted list of the available OP endpoints for a discovered Identifier.
        /// </summary>
        private static List <ServiceEndpoint> filterAndSortEndpoints(IEnumerable <ServiceEndpoint> endpoints,
                                                                     OpenIdRelyingParty relyingParty)
        {
            if (endpoints == null)
            {
                throw new ArgumentNullException("endpoints");
            }
            if (relyingParty == null)
            {
                throw new ArgumentNullException("relyingParty");
            }

            // Construct the endpoints filters based on criteria given by the host web site.
            EndpointSelector versionFilter     = ep => ((ServiceEndpoint)ep).Protocol.Version >= Protocol.Lookup(relyingParty.Settings.MinimumRequiredOpenIdVersion).Version;
            EndpointSelector hostingSiteFilter = relyingParty.EndpointFilter ?? (ep => true);

            bool anyFilteredOut    = false;
            var  filteredEndpoints = new List <IXrdsProviderEndpoint>();

            foreach (ServiceEndpoint endpoint in endpoints)
            {
                if (versionFilter(endpoint) && hostingSiteFilter(endpoint))
                {
                    filteredEndpoints.Add(endpoint);
                }
                else
                {
                    anyFilteredOut = true;
                }
            }

            // Sort endpoints so that the first one in the list is the most preferred one.
            filteredEndpoints.Sort(relyingParty.EndpointOrder);

            List <ServiceEndpoint> endpointList = new List <ServiceEndpoint>(filteredEndpoints.Count);

            foreach (ServiceEndpoint endpoint in filteredEndpoints)
            {
                endpointList.Add(endpoint);
            }

            if (anyFilteredOut)
            {
                Logger.DebugFormat("Some endpoints were filtered out.  Total endpoints remaining: {0}", filteredEndpoints.Count);
            }
            if (Logger.IsDebugEnabled)
            {
                if (Util.AreSequencesEquivalent(endpoints, endpointList))
                {
                    Logger.Debug("Filtering and sorting of endpoints did not affect the list.");
                }
                else
                {
                    Logger.Debug("After filtering and sorting service endpoints, this is the new prioritized list:");
                    Logger.Debug(Util.ToString(filteredEndpoints, true));
                }
            }

            return(endpointList);
        }
        public SelectorRouter(EndpointSelector selector, RouteEndpoint[] candidates)
        {
            _selector   = selector;
            _candidates = candidates;

            _values = new RouteValueDictionary[_candidates.Length];
            _scores = new int[_candidates.Length];
        }
Example #4
0
 public DfaMatcher(ILogger <DfaMatcher> logger, EndpointSelector selector, DfaState[] states, int maxSegmentCount)
 {
     _logger                    = logger;
     _selector                  = selector;
     _states                    = states;
     _maxSegmentCount           = maxSegmentCount;
     _isDefaultEndpointSelector = selector is DefaultEndpointSelector;
 }
Example #5
0
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 {
     app.UseRouting();
     app.UseAuthorization();
     app.UseEndpoints(EndpointSelector =>
     {
         EndpointSelector.MapControllers();
     });
 }
Example #6
0
        private TreeMatcher CreateTreeMatcher(EndpointDataSource endpointDataSource)
        {
            var compositeDataSource             = new CompositeEndpointDataSource(new[] { endpointDataSource });
            var defaultInlineConstraintResolver = new DefaultInlineConstraintResolver(Options.Create(new RouteOptions()));
            var endpointSelector = new EndpointSelector(
                compositeDataSource,
                new EndpointConstraintCache(compositeDataSource, new IEndpointConstraintProvider[] { new DefaultEndpointConstraintProvider() }),
                NullLoggerFactory.Instance);

            return(new TreeMatcher(defaultInlineConstraintResolver, NullLogger.Instance, endpointDataSource, endpointSelector));
        }
Example #7
0
        /// <summary>
        /// Returns a filtered and sorted list of the available OP endpoints for a discovered Identifier.
        /// </summary>
        /// <param name="endpoints">The endpoints.</param>
        /// <param name="relyingParty">The relying party.</param>
        /// <returns>A filtered and sorted list of endpoints; may be empty if the input was empty or the filter removed all endpoints.</returns>
        private static List <IdentifierDiscoveryResult> FilterAndSortEndpoints(IEnumerable <IdentifierDiscoveryResult> endpoints, OpenIdRelyingParty relyingParty)
        {
            Contract.Requires <ArgumentNullException>(endpoints != null);
            Contract.Requires <ArgumentNullException>(relyingParty != null);

            // Construct the endpoints filters based on criteria given by the host web site.
            EndpointSelector versionFilter     = ep => ep.Version >= Protocol.Lookup(relyingParty.SecuritySettings.MinimumRequiredOpenIdVersion).Version;
            EndpointSelector hostingSiteFilter = relyingParty.EndpointFilter ?? (ep => true);

            bool anyFilteredOut    = false;
            var  filteredEndpoints = new List <IdentifierDiscoveryResult>();

            foreach (var endpoint in endpoints)
            {
                if (versionFilter(endpoint) && hostingSiteFilter(endpoint))
                {
                    filteredEndpoints.Add(endpoint);
                }
                else
                {
                    anyFilteredOut = true;
                }
            }

            // Sort endpoints so that the first one in the list is the most preferred one.
            filteredEndpoints.OrderBy(ep => ep, relyingParty.EndpointOrder);

            var endpointList = new List <IdentifierDiscoveryResult>(filteredEndpoints.Count);

            foreach (var endpoint in filteredEndpoints)
            {
                endpointList.Add(endpoint);
            }

            if (anyFilteredOut)
            {
                Logger.Yadis.DebugFormat("Some endpoints were filtered out.  Total endpoints remaining: {0}", filteredEndpoints.Count);
            }
            if (Logger.Yadis.IsDebugEnabled)
            {
                if (MessagingUtilities.AreEquivalent(endpoints, endpointList))
                {
                    Logger.Yadis.Debug("Filtering and sorting of endpoints did not affect the list.");
                }
                else
                {
                    Logger.Yadis.Debug("After filtering and sorting service endpoints, this is the new prioritized list:");
                    Logger.Yadis.Debug(Util.ToStringDeferred(filteredEndpoints, true));
                }
            }

            return(endpointList);
        }
Example #8
0
        public void ReturnsNullIfNoEndpointsRespond()
        {
            const string nowhere = "http://nowhere.com";

            var mockEndpointDetector = new Mock <IEndpointDetector>();

            mockEndpointDetector
            .Setup(m => m.GetEndpointStatus(It.IsAny <int>(), It.IsAny <string>()))
            .ReturnsAsync(new EndpointStatus(false, nowhere));

            var endpointSelector = new EndpointSelector(mockEndpointDetector.Object);

            var address = endpointSelector.GetFirstToRespond(nowhere, nowhere).Result;

            Assert.That(address, Is.Null);
        }
Example #9
0
        public void ReturnsTheFirstEndpointToRespond()
        {
            const string nowhere = "http://nowhere.com";
            const string google  = "http://google.com";

            var mockEndpointDetector = new Mock <IEndpointDetector>();

            mockEndpointDetector
            .Setup(m => m.GetEndpointStatus(It.IsAny <int>(), It.Is <string>(x => x != google)))
            .ReturnsAsync(new EndpointStatus(false, nowhere));

            mockEndpointDetector
            .Setup(m => m.GetEndpointStatus(It.IsAny <int>(), It.Is <string>(x => x == google)))
            .ReturnsAsync(new EndpointStatus(true, google, HttpStatusCode.OK));

            var endpointSelector = new EndpointSelector(mockEndpointDetector.Object);

            var address = endpointSelector.GetFirstToRespond(nowhere, google).Result;

            Assert.That(address, Is.EqualTo(google));
        }
Example #10
0
    private DataSourceDependentMatcher CreateDfaMatcher(
        EndpointDataSource dataSource,
        MatcherPolicy[] policies          = null,
        EndpointSelector endpointSelector = null,
        ILoggerFactory loggerFactory      = null)
    {
        var serviceCollection = new ServiceCollection()
                                .AddLogging()
                                .AddOptions()
                                .AddRouting(options =>
        {
            options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
        });

        if (policies != null)
        {
            for (var i = 0; i < policies.Length; i++)
            {
                serviceCollection.AddSingleton <MatcherPolicy>(policies[i]);
            }
        }

        if (endpointSelector != null)
        {
            serviceCollection.AddSingleton <EndpointSelector>(endpointSelector);
        }

        if (loggerFactory != null)
        {
            serviceCollection.AddSingleton <ILoggerFactory>(loggerFactory);
        }

        var services = serviceCollection.BuildServiceProvider();

        var factory = services.GetRequiredService <MatcherFactory>();

        return(Assert.IsType <DataSourceDependentMatcher>(factory.CreateMatcher(dataSource)));
    }
Example #11
0
        public TreeMatcherFactory(
            IInlineConstraintResolver constraintFactory,
            ILogger <TreeMatcher> logger,
            EndpointSelector endpointSelector)
        {
            if (constraintFactory == null)
            {
                throw new ArgumentNullException(nameof(constraintFactory));
            }

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

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

            _constraintFactory = constraintFactory;
            _logger            = logger;
            _endpointSelector  = endpointSelector;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ODataEndpointSelector"/> class.
 /// </summary>
 /// <param name="selector">The inner Endpoint selector</param>
 public ODataEndpointSelector(EndpointSelector selector)
 {
     _innerSelector = selector;
 }
Example #13
0
 public static void SetPreferredEndpoint(string endpoint)
 {
     EndpointSelector.SetPreferredEndpoint(endpoint);
 }