From abf68c1cea37baa6307b1db75a203de5078e9159 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 14 Aug 2017 22:16:30 -0400 Subject: [PATCH] api get generating organization license --- .../Controllers/OrganizationsController.cs | 21 +++++++++++++++++++ .../Api/Response/OrganizationResponseModel.cs | 2 -- .../Models/Business/OrganizationLicense.cs | 11 ++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index 86ea51a6c..f6105c18f 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -24,6 +24,7 @@ namespace Bit.Api.Controllers private readonly IOrganizationUserRepository _organizationUserRepository; private readonly IOrganizationService _organizationService; private readonly IUserService _userService; + private readonly ILicensingService _licensingService; private readonly CurrentContext _currentContext; private readonly GlobalSettings _globalSettings; private readonly UserManager _userManager; @@ -33,6 +34,7 @@ namespace Bit.Api.Controllers IOrganizationUserRepository organizationUserRepository, IOrganizationService organizationService, IUserService userService, + ILicensingService licensingService, CurrentContext currentContext, GlobalSettings globalSettings, UserManager userManager) @@ -43,6 +45,7 @@ namespace Bit.Api.Controllers _userService = userService; _currentContext = currentContext; _userManager = userManager; + _licensingService = licensingService; _globalSettings = globalSettings; } @@ -95,6 +98,24 @@ namespace Bit.Api.Controllers } } + [HttpGet("{id}/license")] + public async Task GetLicense(string id, [FromQuery]Guid installationId) + { + var orgIdGuid = new Guid(id); + if(!_currentContext.OrganizationOwner(orgIdGuid)) + { + throw new NotFoundException(); + } + + var organization = await _organizationRepository.GetByIdAsync(orgIdGuid); + if(organization == null) + { + throw new NotFoundException(); + } + + return new OrganizationLicense(organization, installationId, _licensingService); + } + [HttpGet("")] public async Task> GetUser() { diff --git a/src/Core/Models/Api/Response/OrganizationResponseModel.cs b/src/Core/Models/Api/Response/OrganizationResponseModel.cs index 9d43d522b..83d44045e 100644 --- a/src/Core/Models/Api/Response/OrganizationResponseModel.cs +++ b/src/Core/Models/Api/Response/OrganizationResponseModel.cs @@ -55,7 +55,6 @@ namespace Bit.Core.Models.Api Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null; StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D) : 0; // 1 GB MaxStorageGb = organization.MaxStorageGb; - // License = ... Expiration = DateTime.UtcNow.AddYears(1); } @@ -76,7 +75,6 @@ namespace Bit.Core.Models.Api public BillingSubscription Subscription { get; set; } public BillingInvoice UpcomingInvoice { get; set; } public IEnumerable Charges { get; set; } - public OrganizationLicense License { get; set; } public DateTime? Expiration { get; set; } } } diff --git a/src/Core/Models/Business/OrganizationLicense.cs b/src/Core/Models/Business/OrganizationLicense.cs index 2baecb85f..0478dc66f 100644 --- a/src/Core/Models/Business/OrganizationLicense.cs +++ b/src/Core/Models/Business/OrganizationLicense.cs @@ -1,5 +1,6 @@ using Bit.Core.Enums; using Bit.Core.Models.Table; +using Bit.Core.Services; using System; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; @@ -12,13 +13,15 @@ namespace Bit.Core.Models.Business public OrganizationLicense() { } - public OrganizationLicense(Organization org, Guid installationId) + public OrganizationLicense(Organization org, Guid installationId, ILicensingService licenseService) { - LicenseKey = ""; + LicenseKey = org.LicenseKey; InstallationId = installationId; Id = org.Id; Name = org.Name; Enabled = org.Enabled; + Plan = org.Plan; + PlanType = org.PlanType; Seats = org.Seats; MaxCollections = org.MaxCollections; UseGroups = org.UseGroups; @@ -27,6 +30,10 @@ namespace Bit.Core.Models.Business MaxStorageGb = org.MaxStorageGb; SelfHost = org.SelfHost; Version = 1; + Issued = DateTime.UtcNow; + Expires = Issued.AddYears(1); + Trial = false; + Signature = Convert.ToBase64String(licenseService.SignLicense(this)); } public string LicenseKey { get; set; }