2022-08-31 13:47:10 +00:00
|
|
|
using Elsa.Common.Features;
|
2022-12-09 11:25:39 +00:00
|
|
|
using Elsa.Extensions;
|
2022-08-31 13:47:10 +00:00
|
|
|
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;
|
|
|
|
|
|
2022-11-23 10:20:35 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Provides identity feature to authenticate & authorize API requests.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
[DependsOn(typeof(SystemClockFeature))]
|
|
|
|
|
public class IdentityFeature : FeatureBase
|
|
|
|
|
{
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <inheritdoc />
|
2022-08-31 13:47:10 +00:00
|
|
|
public IdentityFeature(IModule module) : base(module)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
2023-01-02 21:24:05 +00:00
|
|
|
/// A delegate to configure <see cref="Options.IdentityOptions"/>.
|
2022-12-09 11:25:39 +00:00
|
|
|
/// </summary>
|
2023-01-02 21:24:05 +00:00
|
|
|
public IdentityOptions IdentityOptions { get; set; } = new();
|
2022-12-30 12:11:29 +00:00
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
2023-01-02 21:24:05 +00:00
|
|
|
/// A delegate to configure <see cref="Options.IdentityTokenOptions"/>.
|
2022-12-09 11:25:39 +00:00
|
|
|
/// </summary>
|
2023-01-02 21:24:05 +00:00
|
|
|
public IdentityTokenOptions TokenOptions { get; set; } = new();
|
2022-08-31 13:47:10 +00:00
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IUserStore"/>.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
public Func<IServiceProvider, IUserStore> UserStore { get; set; } = sp => sp.GetRequiredService<MemoryUserStore>();
|
2022-12-09 11:25:39 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate that creates an instance of an implementation of <see cref="IRoleStore"/>.
|
|
|
|
|
/// </summary>
|
2022-08-31 13:47:10 +00:00
|
|
|
public Func<IServiceProvider, IRoleStore> RoleStore { get; set; } = sp => sp.GetRequiredService<MemoryRoleStore>();
|
|
|
|
|
|
2022-12-09 12:44:36 +00:00
|
|
|
/// <inheritdoc />
|
2022-12-09 11:25:39 +00:00
|
|
|
public override void Configure()
|
|
|
|
|
{
|
|
|
|
|
Module.AddFastEndpointsAssembly(GetType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-08-31 13:47:10 +00:00
|
|
|
public override void ConfigureHostedServices()
|
|
|
|
|
{
|
2023-01-02 21:24:05 +00:00
|
|
|
if(IdentityOptions.CreateDefaultAdmin)
|
2022-08-31 13:47:10 +00:00
|
|
|
Module.ConfigureHostedService<SetupDefaultUserHostedService>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
/// <inheritdoc />
|
2022-08-31 13:47:10 +00:00
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2023-01-02 21:24:05 +00:00
|
|
|
Services.Configure<IdentityTokenOptions>(options => options.CopyFrom(TokenOptions));
|
2022-12-30 12:11:29 +00:00
|
|
|
|
2022-08-31 13:47:10 +00:00
|
|
|
Services
|
|
|
|
|
.AddMemoryStore<User, MemoryUserStore>()
|
|
|
|
|
.AddMemoryStore<Role, MemoryRoleStore>()
|
|
|
|
|
.AddSingleton(UserStore)
|
|
|
|
|
.AddSingleton(RoleStore)
|
|
|
|
|
.AddSingleton<IPasswordHasher, DefaultPasswordHasher>()
|
|
|
|
|
.AddSingleton<IAccessTokenIssuer, DefaultAccessTokenIssuer>()
|
|
|
|
|
.AddSingleton<ICredentialsValidator, DefaultCredentialsValidator>()
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
}
|