mirror of https://github.com/abpframework/abp.git
committed by
GitHub
15 changed files with 316 additions and 33 deletions
@ -0,0 +1,9 @@ |
|||
namespace Volo.Abp.Identity.Features |
|||
{ |
|||
public class IdentityFeature |
|||
{ |
|||
public const string GroupName = "Identity"; |
|||
|
|||
public const string TwoFactor = GroupName + ".TwoFactor"; |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Features; |
|||
|
|||
namespace Volo.Abp.Identity.Features |
|||
{ |
|||
public static class IdentityFeatureCheckerExtensions |
|||
{ |
|||
public static async Task<IdentityTwoFactorBehaviour> GetIdentityTwoFactorBehaviour([NotNull] this IFeatureChecker featureChecker) |
|||
{ |
|||
Check.NotNull(featureChecker, nameof(featureChecker)); |
|||
|
|||
var value = await featureChecker.GetOrNullAsync(IdentityFeature.TwoFactor); |
|||
if (value.IsNullOrWhiteSpace() || !Enum.TryParse<IdentityTwoFactorBehaviour>(value, out var behaviour)) |
|||
{ |
|||
throw new AbpException($"{IdentityFeature.TwoFactor} feature value is invalid"); |
|||
} |
|||
|
|||
return behaviour; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
using System; |
|||
using Volo.Abp.Features; |
|||
using Volo.Abp.Identity.Localization; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.Validation.StringValues; |
|||
|
|||
namespace Volo.Abp.Identity.Features |
|||
{ |
|||
public class IdentityFeatureDefinitionProvider : FeatureDefinitionProvider |
|||
{ |
|||
public override void Define(IFeatureDefinitionContext context) |
|||
{ |
|||
var group = context.AddGroup(IdentityFeature.GroupName, L("Feature:IdentityGroup")); |
|||
|
|||
group.AddFeature(IdentityFeature.TwoFactor, |
|||
IdentityTwoFactorBehaviour.Optional.ToString(), |
|||
L("Feature:TwoFactor"), |
|||
L("Feature:TwoFactorDescription"), |
|||
new SelectionStringValueType |
|||
{ |
|||
ItemSource = new StaticSelectionStringValueItemSource( |
|||
new LocalizableSelectionStringValueItem |
|||
{ |
|||
Value = IdentityTwoFactorBehaviour.Optional.ToString(), |
|||
DisplayText = GetTwoFactorBehaviourLocalizableStringInfo("Feature:TwoFactor.Optional") |
|||
}, |
|||
new LocalizableSelectionStringValueItem |
|||
{ |
|||
Value = IdentityTwoFactorBehaviour.Disabled.ToString(), |
|||
DisplayText = GetTwoFactorBehaviourLocalizableStringInfo("Feature:TwoFactor.Disabled") |
|||
}, |
|||
new LocalizableSelectionStringValueItem |
|||
{ |
|||
Value = IdentityTwoFactorBehaviour.Forced.ToString(), |
|||
DisplayText = GetTwoFactorBehaviourLocalizableStringInfo("Feature:TwoFactor.Forced") |
|||
} |
|||
) |
|||
}); |
|||
} |
|||
|
|||
private static LocalizableString L(string name) |
|||
{ |
|||
return LocalizableString.Create<IdentityResource>(name); |
|||
} |
|||
|
|||
private static LocalizableStringInfo GetTwoFactorBehaviourLocalizableStringInfo(string key) |
|||
{ |
|||
return new LocalizableStringInfo(LocalizationResourceNameAttribute.GetName(typeof(IdentityResource)), key); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
namespace Volo.Abp.Identity.Features |
|||
{ |
|||
public enum IdentityTwoFactorBehaviour |
|||
{ |
|||
Optional, |
|||
|
|||
Disabled, |
|||
|
|||
Forced |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Identity.Features; |
|||
using Volo.Abp.Identity.Settings; |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public static class IdentitySettingProviderExtensions |
|||
{ |
|||
public static async Task<IdentityTwoFactorBehaviour> GetIdentityTwoFactorBehaviour([NotNull] this ISettingProvider settingProvider) |
|||
{ |
|||
Check.NotNull(settingProvider, nameof(settingProvider)); |
|||
|
|||
var value = await settingProvider.GetOrNullAsync(IdentitySettingNames.TwoFactor.Behaviour); |
|||
if (value.IsNullOrWhiteSpace() || !Enum.TryParse<IdentityTwoFactorBehaviour>(value, out var behaviour)) |
|||
{ |
|||
throw new AbpException($"{IdentitySettingNames.TwoFactor.Behaviour} setting value is invalid"); |
|||
} |
|||
|
|||
return behaviour; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.Abp.Features; |
|||
using Volo.Abp.Identity.Features; |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityTwoFactorManager : IDomainService |
|||
{ |
|||
protected IFeatureChecker FeatureChecker { get; } |
|||
|
|||
protected ISettingProvider SettingProvider { get; } |
|||
|
|||
public IdentityTwoFactorManager(IFeatureChecker featureChecker, ISettingProvider settingProvider) |
|||
{ |
|||
FeatureChecker = featureChecker; |
|||
SettingProvider = settingProvider; |
|||
} |
|||
|
|||
public virtual async Task<bool> IsOptionalAsync() |
|||
{ |
|||
var feature = await FeatureChecker.GetIdentityTwoFactorBehaviour(); |
|||
if (feature == IdentityTwoFactorBehaviour.Optional) |
|||
{ |
|||
var setting = await SettingProvider.GetIdentityTwoFactorBehaviour(); |
|||
if (setting == IdentityTwoFactorBehaviour.Optional) |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public virtual async Task<bool> IsForcedEnableAsync() |
|||
{ |
|||
var feature = await FeatureChecker.GetIdentityTwoFactorBehaviour(); |
|||
if (feature == IdentityTwoFactorBehaviour.Forced) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
var setting = await SettingProvider.GetIdentityTwoFactorBehaviour(); |
|||
if (setting == IdentityTwoFactorBehaviour.Forced) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public virtual async Task<bool> IsForcedDisableAsync() |
|||
{ |
|||
var feature = await FeatureChecker.GetIdentityTwoFactorBehaviour(); |
|||
if (feature == IdentityTwoFactorBehaviour.Disabled) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
var setting = await SettingProvider.GetIdentityTwoFactorBehaviour(); |
|||
if (setting == IdentityTwoFactorBehaviour.Disabled) |
|||
{ |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue