using Elsa.Common.Features;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Identity.Entities;
using Elsa.Identity.HostedServices;
using Elsa.Identity.Implementations;
using Elsa.Identity.Options;
using Elsa.Identity.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Identity.Features;
///
/// Provides identity feature to authenticate & authorize API requests.
///
[DependsOn(typeof(SystemClockFeature))]
public class IdentityFeature : FeatureBase
{
///
public IdentityFeature(IModule module) : base(module)
{
}
///
/// A delegate to configure .
///
public IdentityOptions IdentityOptions { get; set; } = new();
///
/// A delegate to configure .
///
public IdentityTokenOptions TokenOptions { get; set; } = new();
///
/// A delegate that creates an instance of an implementation of .
///
public Func UserStore { get; set; } = sp => sp.GetRequiredService();
///
/// A delegate that creates an instance of an implementation of .
///
public Func RoleStore { get; set; } = sp => sp.GetRequiredService();
///
public override void Configure()
{
Module.AddFastEndpointsAssembly(GetType());
}
///
public override void ConfigureHostedServices()
{
if(IdentityOptions.CreateDefaultAdmin)
Module.ConfigureHostedService();
}
///
public override void Apply()
{
Services.Configure(options => options.CopyFrom(TokenOptions));
Services
.AddMemoryStore()
.AddMemoryStore()
.AddSingleton(UserStore)
.AddSingleton(RoleStore)
.AddSingleton()
.AddSingleton()
.AddSingleton()
;
}
}