1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-22 20:11:04 -05:00

only allow license key one use per installation

This commit is contained in:
Kyle Spearrin 2017-11-06 08:12:36 -05:00
parent 50a4202739
commit dccdef6db5
2 changed files with 34 additions and 12 deletions

View File

@ -62,25 +62,35 @@ namespace Bit.Core.Services
return; return;
} }
var orgs = await _organizationRepository.GetManyByEnabledAsync(); var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
_logger.LogInformation("Validating licenses for {0} organizations.", orgs.Count); _logger.LogInformation("Validating licenses for {0} organizations.", enabledOrgs.Count);
foreach(var org in orgs) foreach(var org in enabledOrgs)
{ {
var license = ReadOrganiztionLicense(org); var license = ReadOrganiztionLicense(org);
if(license == null || !license.VerifyData(org, _globalSettings) || !license.VerifySignature(_certificate)) if(license == null)
{ {
_logger.LogInformation("Organization {0}({1}) has an invalid license and is being disabled.", await DisableOrganizationAsync(org, null);
org.Id, org.Name); continue;
}
org.Enabled = false; var totalLicensedOrgs = enabledOrgs.Count(o => o.LicenseKey.Equals(license.LicenseKey));
org.ExpirationDate = license.Expires; if(totalLicensedOrgs > 1 || !license.VerifyData(org, _globalSettings) || !license.VerifySignature(_certificate))
org.RevisionDate = DateTime.UtcNow; {
await _organizationRepository.ReplaceAsync(org); await DisableOrganizationAsync(org, license);
} }
} }
} }
private async Task DisableOrganizationAsync(Organization org, ILicense license)
{
_logger.LogInformation("Organization {0}({1}) has an invalid license and is being disabled.", org.Id, org.Name);
org.Enabled = false;
org.ExpirationDate = license?.Expires ?? DateTime.UtcNow;
org.RevisionDate = DateTime.UtcNow;
await _organizationRepository.ReplaceAsync(org);
}
public async Task ValidateUsersAsync() public async Task ValidateUsersAsync()
{ {
if(!_globalSettings.SelfHosted) if(!_globalSettings.SelfHosted)
@ -178,7 +188,7 @@ namespace Bit.Core.Services
user.Id, user.Email); user.Id, user.Email);
user.Premium = false; user.Premium = false;
user.PremiumExpirationDate = license.Expires; user.PremiumExpirationDate = license?.Expires ?? DateTime.UtcNow;
user.RevisionDate = DateTime.UtcNow; user.RevisionDate = DateTime.UtcNow;
await _userRepository.ReplaceAsync(user); await _userRepository.ReplaceAsync(user);
} }

View File

@ -561,6 +561,12 @@ namespace Bit.Core.Services
throw new BadRequestException("Plan not found."); throw new BadRequestException("Plan not found.");
} }
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
if(enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey)))
{
throw new BadRequestException("License is already in use by another organization.");
}
var organization = new Organization var organization = new Organization
{ {
Name = license.Name, Name = license.Name,
@ -683,6 +689,12 @@ namespace Bit.Core.Services
"hosting of organizations and that the installation id matches your current installation."); "hosting of organizations and that the installation id matches your current installation.");
} }
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
if(enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey) && o.Id != organizationId))
{
throw new BadRequestException("License is already in use by another organization.");
}
if(license.Seats.HasValue && (!organization.Seats.HasValue || organization.Seats.Value > license.Seats.Value)) if(license.Seats.HasValue && (!organization.Seats.HasValue || organization.Seats.Value > license.Seats.Value))
{ {
var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id); var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id);