2015-12-08 22:57:38 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using Bit.Core.Enums;
|
2016-05-21 17:16:22 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2017-06-07 14:14:34 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2018-03-20 15:00:56 -04:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
2017-03-08 21:45:08 -05:00
|
|
|
|
namespace Bit.Core.Models.Table
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2020-07-07 12:01:34 -04:00
|
|
|
|
public class User : ITableObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable, ITwoFactorProvidersUser, IReferenceable
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-06-07 14:14:34 -04:00
|
|
|
|
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
|
|
|
|
|
|
|
2016-05-21 17:16:22 -04:00
|
|
|
|
public Guid Id { get; set; }
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
public string Email { get; set; }
|
2016-02-21 00:50:53 -05:00
|
|
|
|
public bool EmailVerified { get; set; }
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string MasterPassword { get; set; }
|
|
|
|
|
|
public string MasterPasswordHint { get; set; }
|
2016-01-07 23:01:01 -05:00
|
|
|
|
public string Culture { get; set; } = "en-US";
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string SecurityStamp { get; set; }
|
2017-06-06 17:15:19 -04:00
|
|
|
|
public string TwoFactorProviders { get; set; }
|
2016-11-14 21:13:53 -05:00
|
|
|
|
public string TwoFactorRecoveryCode { get; set; }
|
2017-01-09 22:20:34 -05:00
|
|
|
|
public string EquivalentDomains { get; set; }
|
|
|
|
|
|
public string ExcludedGlobalEquivalentDomains { get; set; }
|
2017-01-14 10:02:37 -05:00
|
|
|
|
public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow;
|
2017-05-31 09:54:32 -04:00
|
|
|
|
public string Key { get; set; }
|
2017-02-11 23:00:55 -05:00
|
|
|
|
public string PublicKey { get; set; }
|
|
|
|
|
|
public string PrivateKey { get; set; }
|
2017-06-30 14:41:57 -04:00
|
|
|
|
public bool Premium { get; set; }
|
2017-08-12 10:43:52 -04:00
|
|
|
|
public DateTime? PremiumExpirationDate { get; set; }
|
2018-07-12 17:35:01 -04:00
|
|
|
|
public DateTime? RenewalReminderDate { get; set; }
|
2017-06-30 14:41:57 -04:00
|
|
|
|
public long? Storage { get; set; }
|
|
|
|
|
|
public short? MaxStorageGb { get; set; }
|
2017-07-28 14:24:07 -04:00
|
|
|
|
public GatewayType? Gateway { get; set; }
|
|
|
|
|
|
public string GatewayCustomerId { get; set; }
|
|
|
|
|
|
public string GatewaySubscriptionId { get; set; }
|
2020-07-20 15:19:46 -04:00
|
|
|
|
public string ReferenceData { get; set; }
|
2017-08-09 17:01:37 -04:00
|
|
|
|
public string LicenseKey { get; set; }
|
2020-11-10 15:15:29 -05:00
|
|
|
|
public string ApiKey { get; set; }
|
2018-08-27 19:57:45 -04:00
|
|
|
|
public KdfType Kdf { get; set; } = KdfType.PBKDF2_SHA256;
|
2018-08-14 15:30:04 -04:00
|
|
|
|
public int KdfIterations { get; set; } = 5000;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
2016-02-20 23:25:44 -05:00
|
|
|
|
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
2016-05-21 17:16:22 -04:00
|
|
|
|
|
|
|
|
|
|
public void SetNewId()
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = CoreHelpers.GenerateComb();
|
|
|
|
|
|
}
|
2017-06-07 14:14:34 -04:00
|
|
|
|
|
2017-07-06 14:55:58 -04:00
|
|
|
|
public string BillingEmailAddress()
|
|
|
|
|
|
{
|
2019-06-12 22:08:53 -04:00
|
|
|
|
return Email?.ToLowerInvariant()?.Trim();
|
2017-07-06 14:55:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string BillingName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-31 12:11:30 -05:00
|
|
|
|
public string BraintreeCustomerIdPrefix()
|
|
|
|
|
|
{
|
|
|
|
|
|
return "u";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-01 09:18:34 -05:00
|
|
|
|
public string BraintreeIdField()
|
|
|
|
|
|
{
|
|
|
|
|
|
return "user_id";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GatewayIdField()
|
|
|
|
|
|
{
|
|
|
|
|
|
return "userId";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-17 16:55:40 -04:00
|
|
|
|
public bool IsUser()
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-07 14:14:34 -04:00
|
|
|
|
public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders()
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(TwoFactorProviders))
|
2017-06-07 14:14:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (_twoFactorProviders == null)
|
2017-06-07 14:14:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
_twoFactorProviders =
|
2018-04-02 14:53:19 -04:00
|
|
|
|
JsonConvert.DeserializeObject<Dictionary<TwoFactorProviderType, TwoFactorProvider>>(
|
|
|
|
|
|
TwoFactorProviders);
|
2017-06-07 14:14:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _twoFactorProviders;
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
catch (JsonSerializationException)
|
2017-06-07 14:14:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-19 11:48:36 -05:00
|
|
|
|
public Guid? GetUserId()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool GetPremium()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Premium;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-07 14:14:34 -04:00
|
|
|
|
public void SetTwoFactorProviders(Dictionary<TwoFactorProviderType, TwoFactorProvider> providers)
|
|
|
|
|
|
{
|
|
|
|
|
|
TwoFactorProviders = JsonConvert.SerializeObject(providers, new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new EnumKeyResolver<byte>()
|
|
|
|
|
|
});
|
|
|
|
|
|
_twoFactorProviders = providers;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TwoFactorProvider GetTwoFactorProvider(TwoFactorProviderType provider)
|
|
|
|
|
|
{
|
|
|
|
|
|
var providers = GetTwoFactorProviders();
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (providers == null || !providers.ContainsKey(provider))
|
2017-06-07 14:14:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return providers[provider];
|
|
|
|
|
|
}
|
2017-06-30 14:41:57 -04:00
|
|
|
|
|
|
|
|
|
|
public long StorageBytesRemaining()
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!MaxStorageGb.HasValue)
|
2017-06-30 14:41:57 -04:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-06 14:55:58 -04:00
|
|
|
|
return StorageBytesRemaining(MaxStorageGb.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long StorageBytesRemaining(short maxStorageGb)
|
|
|
|
|
|
{
|
|
|
|
|
|
var maxStorageBytes = maxStorageGb * 1073741824L;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!Storage.HasValue)
|
2017-06-30 14:41:57 -04:00
|
|
|
|
{
|
|
|
|
|
|
return maxStorageBytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return maxStorageBytes - Storage.Value;
|
|
|
|
|
|
}
|
2017-07-28 12:09:12 -04:00
|
|
|
|
|
2018-08-28 17:40:08 -04:00
|
|
|
|
public IdentityUser ToIdentityUser(bool twoFactorEnabled)
|
2018-03-20 15:00:56 -04:00
|
|
|
|
{
|
|
|
|
|
|
return new IdentityUser
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = Id.ToString(),
|
|
|
|
|
|
Email = Email,
|
|
|
|
|
|
NormalizedEmail = Email,
|
|
|
|
|
|
EmailConfirmed = EmailVerified,
|
|
|
|
|
|
UserName = Email,
|
|
|
|
|
|
NormalizedUserName = Email,
|
2018-08-28 17:40:08 -04:00
|
|
|
|
TwoFactorEnabled = twoFactorEnabled,
|
2018-03-20 15:00:56 -04:00
|
|
|
|
SecurityStamp = SecurityStamp
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|