1
0
mirror of https://github.com/bitwarden/server synced 2025-12-22 11:13:27 +00:00

stub out use2fa and twofactorproviders on orgs

This commit is contained in:
Kyle Spearrin
2018-04-02 14:53:19 -04:00
parent 63169e4ecc
commit bcc224c02d
13 changed files with 361 additions and 6 deletions

View File

@@ -19,7 +19,7 @@ namespace Bit.Core.Models.Business
public OrganizationLicense(Organization org, BillingInfo billingInfo, Guid installationId,
ILicensingService licenseService)
{
Version = 3;
Version = 4;
LicenseKey = org.LicenseKey;
InstallationId = installationId;
Id = org.Id;
@@ -35,6 +35,7 @@ namespace Bit.Core.Models.Business
UseEvents = org.UseEvents;
UseDirectory = org.UseDirectory;
UseTotp = org.UseTotp;
Use2fa = org.Use2fa;
MaxStorageGb = org.MaxStorageGb;
SelfHost = org.SelfHost;
UsersGetPremium = org.UsersGetPremium;
@@ -100,6 +101,7 @@ namespace Bit.Core.Models.Business
public bool UseEvents { get; set; }
public bool UseDirectory { get; set; }
public bool UseTotp { get; set; }
public bool Use2fa { get; set; }
public short? MaxStorageGb { get; set; }
public bool SelfHost { get; set; }
public bool UsersGetPremium { get; set; }
@@ -116,7 +118,7 @@ namespace Bit.Core.Models.Business
public byte[] GetDataBytes(bool forHash = false)
{
string data = null;
if(Version >= 1 && Version <= 3)
if(Version >= 1 && Version <= 4)
{
var props = typeof(OrganizationLicense)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
@@ -127,6 +129,8 @@ namespace Bit.Core.Models.Business
(Version >= 2 || !p.Name.Equals(nameof(UsersGetPremium))) &&
// UseEvents was added in Version 3
(Version >= 3 || !p.Name.Equals(nameof(UseEvents))) &&
// Use2fa was added in Version 4
(Version >= 4 || !p.Name.Equals(nameof(Use2fa))) &&
(
!forHash ||
(
@@ -163,7 +167,7 @@ namespace Bit.Core.Models.Business
return false;
}
if(Version >= 1 && Version <= 3)
if(Version >= 1 && Version <= 4)
{
return InstallationId == globalSettings.Installation.Id && SelfHost;
}
@@ -180,7 +184,7 @@ namespace Bit.Core.Models.Business
return false;
}
if(Version >= 1 && Version <= 3)
if(Version >= 1 && Version <= 4)
{
var valid =
globalSettings.Installation.Id == InstallationId &&
@@ -205,6 +209,11 @@ namespace Bit.Core.Models.Business
valid = organization.UseEvents == UseEvents;
}
if(valid && Version >= 4)
{
valid = organization.Use2fa == Use2fa;
}
return valid;
}
else

View File

@@ -11,11 +11,13 @@ namespace Bit.Core.Models.Data
{
Id = organization.Id;
UseEvents = organization.UseEvents;
Use2fa = organization.Use2fa;
Enabled = organization.Enabled;
}
public Guid Id { get; set; }
public bool UseEvents { get; set; }
public bool Use2fa { get; set; }
public bool Enabled { get; set; }
}
}

View File

@@ -16,6 +16,7 @@ namespace Bit.Core.Models.StaticStore
public bool UseDirectory { get; set; }
public bool UseEvents { get; set; }
public bool UseTotp { get; set; }
public bool Use2fa { get; set; }
public short? MaxStorageGb { get; set; }
public decimal BasePrice { get; set; }
public decimal SeatPrice { get; set; }

View File

@@ -3,11 +3,16 @@ using Bit.Core.Utilities;
using Bit.Core.Enums;
using Bit.Core.Services;
using Bit.Core.Exceptions;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
namespace Bit.Core.Models.Table
{
public class Organization : ITableObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable
{
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
public Guid Id { get; set; }
public string Name { get; set; }
public string BusinessName { get; set; }
@@ -25,6 +30,7 @@ namespace Bit.Core.Models.Table
public bool UseDirectory { get; set; }
public bool UseEvents { get; set; }
public bool UseTotp { get; set; }
public bool Use2fa { get; set; }
public bool SelfHost { get; set; }
public bool UsersGetPremium { get; set; }
public long? Storage { get; set; }
@@ -34,6 +40,7 @@ namespace Bit.Core.Models.Table
public string GatewaySubscriptionId { get; set; }
public bool Enabled { get; set; } = true;
public string LicenseKey { get; set; }
public string TwoFactorProviders { get; set; }
public DateTime? ExpirationDate { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
@@ -99,5 +106,71 @@ namespace Bit.Core.Models.Table
return paymentService;
}
public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders()
{
if(string.IsNullOrWhiteSpace(TwoFactorProviders))
{
return null;
}
try
{
if(_twoFactorProviders == null)
{
_twoFactorProviders =
JsonConvert.DeserializeObject<Dictionary<TwoFactorProviderType, TwoFactorProvider>>(
TwoFactorProviders);
}
return _twoFactorProviders;
}
catch(JsonSerializationException)
{
return null;
}
}
public void SetTwoFactorProviders(Dictionary<TwoFactorProviderType, TwoFactorProvider> providers)
{
TwoFactorProviders = JsonConvert.SerializeObject(providers, new JsonSerializerSettings
{
ContractResolver = new EnumKeyResolver<byte>()
});
_twoFactorProviders = providers;
}
public bool TwoFactorProviderIsEnabled(TwoFactorProviderType provider)
{
var providers = GetTwoFactorProviders();
if(providers == null || !providers.ContainsKey(provider))
{
return false;
}
return providers[provider].Enabled && Use2fa;
}
public bool TwoFactorIsEnabled()
{
var providers = GetTwoFactorProviders();
if(providers == null)
{
return false;
}
return providers.Any(p => (p.Value?.Enabled ?? false) && Use2fa);
}
public TwoFactorProvider GetTwoFactorProvider(TwoFactorProviderType provider)
{
var providers = GetTwoFactorProviders();
if(providers == null || !providers.ContainsKey(provider))
{
return null;
}
return providers[provider];
}
}
}

View File

@@ -68,7 +68,8 @@ namespace Bit.Core.Models.Table
if(_twoFactorProviders == null)
{
_twoFactorProviders =
JsonConvert.DeserializeObject<Dictionary<TwoFactorProviderType, TwoFactorProvider>>(TwoFactorProviders);
JsonConvert.DeserializeObject<Dictionary<TwoFactorProviderType, TwoFactorProvider>>(
TwoFactorProviders);
}
return _twoFactorProviders;
@@ -107,7 +108,8 @@ namespace Bit.Core.Models.Table
return false;
}
return providers.Any(p => (p.Value?.Enabled ?? false) && (Premium || !TwoFactorProvider.RequiresPremium(p.Key)));
return providers.Any(p => (p.Value?.Enabled ?? false) &&
(Premium || !TwoFactorProvider.RequiresPremium(p.Key)));
}
public TwoFactorProvider GetTwoFactorProvider(TwoFactorProviderType provider)