< Summary

Information
Class: Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Extensions/ServiceCollectionExtensions.cs
Line coverage
98%
Covered lines: 73
Uncovered lines: 1
Coverable lines: 74
Total lines: 136
Line coverage: 98.6%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddExternalAuthenticationHealthCheck(...)0%620%
AddExternalAuthenticationServices(...)100%22100%
AddExternalAuthenticationExtension(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Extensions;
 3using Elsa.ExternalAuthentication.Contracts;
 4using Elsa.ExternalAuthentication.Options;
 5using Elsa.ExternalAuthentication.Permissions;
 6using Elsa.ExternalAuthentication.Policies;
 7using Elsa.ExternalAuthentication.Providers;
 8using Elsa.ExternalAuthentication.Services;
 9using Elsa.ExternalAuthentication.Stores.InMemory;
 10using Elsa.ExternalAuthentication.Validation;
 11using Elsa.Identity.Contracts;
 12using Microsoft.AspNetCore.Builder;
 13using Microsoft.AspNetCore.RateLimiting;
 14using Microsoft.Extensions.Diagnostics.HealthChecks;
 15using Microsoft.Extensions.DependencyInjection.Extensions;
 16using Microsoft.Extensions.Options;
 17
 18namespace Microsoft.Extensions.DependencyInjection;
 19
 20public static class ServiceCollectionExtensions
 21{
 22    /// <summary>Adds the explicit, non-readiness External Authentication health bridge.</summary>
 23    public static IHealthChecksBuilder AddExternalAuthenticationHealthCheck(this IServiceCollection services, string nam
 024        services.AddHealthChecks().AddCheck<ExternalAuthenticationHealthCheck>(name, HealthStatus.Degraded, tags ?? ["ex
 25
 26    /// <summary>
 27    /// Adds the protocol-neutral External Authentication foundation and its single-node defaults.
 28    /// Hosts requiring durable, multi-node state may replace the store registrations.
 29    /// </summary>
 30    public static IServiceCollection AddExternalAuthenticationServices(this IServiceCollection services, Action<External
 31    {
 632        var options = services.AddOptions<ExternalAuthenticationOptions>().ValidateOnStart();
 633        if (configureOptions != null)
 334            options.Configure(configureOptions);
 35
 36        // The module evaluates permissions outside endpoint authorization -- delegation, the grant boundary,
 37        // and the recovery override -- so it depends on the evaluator whether or not a host wired one up.
 38        // The call is TryAdd-based and idempotent, so a host that already registered one keeps it.
 639        services.AddElsaAuthorization();
 40
 41        // Contributed explicitly rather than left to the host's assembly scan, so the module's resources reach
 42        // the catalog on any host that registers its services, the same reason AddElsaAuthorization is called
 43        // here. Registration is TryAddEnumerable-backed, so a host that also scans this assembly gets one copy.
 644        services.AddPermissionDescriptors<ExternalAuthenticationResourcePermissionsDescriptorProvider>();
 45
 646        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.UnlinkedIdentityPolicy, RejectUn
 647        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.UnlinkedIdentityPolicy, CreateUs
 648        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.UnlinkedIdentityPolicy, MatchExt
 649        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.PermissionGrantSource, ElsaRoleP
 650        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.PermissionGrantSource, ClaimMapp
 651        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.PermissionGrantSource, GroupMapp
 652        services.AddExternalAuthenticationExtension(ExternalAuthenticationExtensionKind.PermissionGrantSource, ClaimPass
 53        // The validator warns about grant-boundary configuration, and ValidateOnStart resolves it on any
 54        // IOptions access, so a logger has to be resolvable even on a bare service collection. AddLogging is
 55        // TryAdd-based, so a host that already configured logging keeps its own.
 656        services.AddLogging();
 657        services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<ExternalAuthenticationOptions>, ExternalA
 658        services.AddDataProtection();
 759        services.AddRateLimiter(_ => { });
 660        services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<RateLimiterOptions>, ConfigureExternalAu
 61
 62        // The module reads the ambient tenant outside the multitenancy feature -- connection scoping and the
 63        // role-deletion contributor's tenant boundary -- so it depends on an accessor whether or not a host
 64        // enabled multitenancy. TryAdd keeps a host's own registration.
 665        services.TryAddSingleton<ITenantAccessor, DefaultTenantAccessor>();
 66
 667        services.TryAddSingleton<ConnectionRevisionCalculator>();
 668        services.TryAddSingleton<FinalLoginPathGuard>();
 669        services.TryAddSingleton<ExternalAuthenticationSecurityNotifier>();
 670        services.TryAddScoped<ConnectionTestService>();
 671        services.TryAddScoped<PreviewSignInService>();
 672        services.TryAddSingleton<ExternalAuthenticationHealthCheck>();
 673        services.TryAddSingleton<IOutboundDnsResolver, SystemOutboundDnsResolver>();
 674        services.TryAddSingleton<OutboundDestinationValidator>();
 675        services.TryAddSingleton<IValidatedAddressConnector, SocketValidatedAddressConnector>();
 676        services.TryAddSingleton<ValidatedOutboundConnectionFactory>();
 677        services.TryAddSingleton<IProviderHttpClientFactory, ProviderHttpClientFactory>();
 678        services.TryAddEnumerable(ServiceDescriptor.Singleton<IIdentityProviderConnectionSource, ConfigurationIdentityPr
 679        services.TryAddSingleton<IIdentityProviderConnectionStore, InMemoryIdentityProviderConnectionStore>();
 680        services.TryAddEnumerable(ServiceDescriptor.Singleton<IIdentityProviderConnectionSource, DatabaseIdentityProvide
 681        services.TryAddSingleton<IIdentityProviderConnectionRegistry, DefaultIdentityProviderConnectionRegistry>();
 682        services.TryAddSingleton<IIdentityProviderConnectionValidityAssessor, IdentityProviderConnectionValidityAssessor
 683        services.TryAddSingleton<ExtensionDescriptorValidator>();
 684        services.TryAddSingleton<IExternalAuthenticationAdapterRegistry, DefaultExternalAuthenticationAdapterRegistry>()
 685        services.TryAddSingleton<IUnlinkedIdentityPolicyRegistry, DefaultUnlinkedIdentityPolicyRegistry>();
 686        services.TryAddSingleton<IExternalUserMatcherRegistry, DefaultExternalUserMatcherRegistry>();
 687        services.TryAddScoped<IPermissionGrantSourceRegistry, DefaultPermissionGrantSourceRegistry>();
 688        services.TryAddSingleton<IAdapterSettingsMigrationService, AdapterSettingsMigrationService>();
 89
 690        services.TryAddSingleton<IExternalAuthenticationStateStore, InMemoryExternalAuthenticationStateStore>();
 691        services.TryAddSingleton<IExternalAuthenticationHandleHasher, HmacExternalAuthenticationHandleHasher>();
 692        services.TryAddSingleton<IAuthorizationGrantStore, InMemoryAuthorizationGrantStore>();
 693        services.TryAddSingleton<IExternalAuthenticationSessionStore, InMemoryExternalAuthenticationSessionStore>();
 694        services.TryAddSingleton<IPreviewResultStore, InMemoryPreviewResultStore>();
 695        services.TryAddSingleton<IConnectionObservationStore, InMemoryConnectionObservationStore>();
 696        services.TryAddSingleton<IConnectionRegistryVersionStore, InMemoryConnectionRegistryVersionStore>();
 697        services.TryAddSingleton<InMemoryExternalIdentityProvisionerState>();
 698        services.TryAddScoped<InMemoryExternalIdentityProvisioner>();
 699        services.TryAddScoped<IExternalIdentityProvisioner>(serviceProvider => serviceProvider.GetRequiredService<InMemo
 6100        services.TryAddScoped<IExternalIdentityLinkManagementStore>(serviceProvider => serviceProvider.GetRequiredServic
 6101        services.TryAddScoped<ExternalIdentityLinkManagementService>();
 6102        services.TryAddScoped<IExternalIdentityResolver, DefaultExternalIdentityResolver>();
 6103        services.TryAddScoped<IPermissionGrantResolver, DefaultPermissionGrantResolver>();
 6104        services.TryAddScoped<IPermissionDelegationAuthorizer, DefaultPermissionDelegationAuthorizer>();
 6105        services.TryAddEnumerable(ServiceDescriptor.Singleton<ISecretBindingResolver, ConfigurationSecretBindingResolver
 6106        services.TryAddEnumerable(ServiceDescriptor.Singleton<IUnlinkedIdentityPolicy, RejectUnlinkedIdentityPolicy>());
 6107        services.TryAddEnumerable(ServiceDescriptor.Singleton<IUnlinkedIdentityPolicy, CreateUserUnlinkedIdentityPolicy>
 6108        services.TryAddEnumerable(ServiceDescriptor.Singleton<IUnlinkedIdentityPolicy, MatchExternalUserUnlinkedIdentity
 6109        services.TryAddEnumerable(ServiceDescriptor.Scoped<IPermissionGrantSource, ElsaRolePermissionGrantSource>());
 6110        services.TryAddEnumerable(ServiceDescriptor.Scoped<IPermissionGrantSource, ClaimMappingPermissionGrantSource>())
 6111        services.TryAddEnumerable(ServiceDescriptor.Scoped<IPermissionGrantSource, GroupMappingPermissionGrantSource>())
 6112        services.TryAddEnumerable(ServiceDescriptor.Scoped<IPermissionGrantSource, ClaimPassThroughPermissionGrantSource
 6113        services.TryAddScoped<IExternalAuthenticationTokenIssuer, DefaultExternalAuthenticationTokenIssuer>();
 6114        services.TryAddScoped<IExternalAuthenticationBroker, ExternalAuthenticationBroker>();
 6115        services.TryAddScoped<IdentityProviderConnectionManagementService>();
 6116        services.TryAddEnumerable(ServiceDescriptor.Scoped<IRoleDeletionDependencyContributor, ExternalAuthenticationRol
 6117        services.TryAddEnumerable(ServiceDescriptor.Scoped<IUserDeletionDependencyContributor, ExternalAuthenticationUse
 118
 6119        return services;
 120    }
 121
 122    /// <summary>
 123    /// Registers the stable identifier of a trusted deployment-installed extension
 124    /// for startup selection validation.
 125    /// </summary>
 126    public static IServiceCollection AddExternalAuthenticationExtension(
 127        this IServiceCollection services,
 128        ExternalAuthenticationExtensionKind kind,
 129        string type)
 130    {
 45131        ArgumentException.ThrowIfNullOrWhiteSpace(type);
 45132        services.Configure<ExternalAuthenticationExtensionOptions>(options =>
 52133            options.Registrations.Add(new(kind, type)));
 45134        return services;
 135    }
 136}