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

create org with license file

This commit is contained in:
Kyle Spearrin
2017-08-14 20:57:45 -04:00
parent e4ec09fd0c
commit 5259b07889
9 changed files with 146 additions and 60 deletions

View File

@ -395,35 +395,9 @@ namespace Bit.Api.Controllers
var valid = model.Validate(_globalSettings);
UserLicense license = null;
if(valid && _globalSettings.SelfHosted && model.License != null)
if(valid && _globalSettings.SelfHosted)
{
if(!HttpContext.Request.ContentLength.HasValue || HttpContext.Request.ContentLength.Value > 51200) // 50 KB
{
valid = false;
}
else
{
try
{
using(var stream = model.License.OpenReadStream())
using(var reader = new StreamReader(stream))
{
var s = await reader.ReadToEndAsync();
if(string.IsNullOrWhiteSpace(s))
{
valid = false;
}
else
{
license = JsonConvert.DeserializeObject<UserLicense>(s);
}
}
}
catch
{
valid = false;
}
}
license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
}
if(!valid || (_globalSettings.SelfHosted && license == null))
@ -488,7 +462,7 @@ namespace Bit.Api.Controllers
[HttpPut("license")]
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task PutLicense(UpdateLicenseRequestModel model)
public async Task PutLicense(LicenseRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
@ -496,24 +470,7 @@ namespace Bit.Api.Controllers
throw new UnauthorizedAccessException();
}
UserLicense license = null;
if(HttpContext.Request.ContentLength.HasValue && HttpContext.Request.ContentLength.Value <= 51200) // 50 KB
{
try
{
using(var stream = model.License.OpenReadStream())
using(var reader = new StreamReader(stream))
{
var s = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(s))
{
license = JsonConvert.DeserializeObject<UserLicense>(s);
}
}
}
catch { }
}
var license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
if(license == null)
{
throw new BadRequestException("Invalid license");

View File

@ -11,6 +11,8 @@ using Bit.Core;
using Microsoft.AspNetCore.Identity;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
using Bit.Api.Utilities;
using Bit.Core.Models.Business;
namespace Bit.Api.Controllers
{
@ -94,6 +96,7 @@ namespace Bit.Api.Controllers
}
[HttpPost("")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<OrganizationResponseModel> Post([FromBody]OrganizationCreateRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
@ -107,6 +110,26 @@ namespace Bit.Api.Controllers
return new OrganizationResponseModel(result.Item1);
}
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task<OrganizationResponseModel> PostLicense(OrganizationCreateLicenseRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
var license = await ApiHelpers.ReadJsonFileFromBody<OrganizationLicense>(HttpContext, model.License);
if(license == null)
{
throw new BadRequestException("Invalid license");
}
var result = await _organizationService.SignUpAsync(license, user, model.Key);
return new OrganizationResponseModel(result.Item1);
}
[HttpPut("{id}")]
[HttpPost("{id}")]
public async Task<OrganizationResponseModel> Put(string id, [FromBody]OrganizationUpdateRequestModel model)
@ -132,6 +155,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/payment")]
[HttpPost("{id}/payment")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutPayment(string id, [FromBody]PaymentRequestModel model)
{
var orgIdGuid = new Guid(id);
@ -145,6 +169,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/upgrade")]
[HttpPost("{id}/upgrade")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutUpgrade(string id, [FromBody]OrganizationUpgradeRequestModel model)
{
var orgIdGuid = new Guid(id);
@ -158,6 +183,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/seat")]
[HttpPost("{id}/seat")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutSeat(string id, [FromBody]OrganizationSeatRequestModel model)
{
var orgIdGuid = new Guid(id);
@ -171,6 +197,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/storage")]
[HttpPost("{id}/storage")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutStorage(string id, [FromBody]StorageRequestModel model)
{
var orgIdGuid = new Guid(id);
@ -183,6 +210,7 @@ namespace Bit.Api.Controllers
}
[HttpPost("{id}/verify-bank")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostVerifyBank(string id, [FromBody]OrganizationVerifyBankRequestModel model)
{
var orgIdGuid = new Guid(id);
@ -196,6 +224,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/cancel")]
[HttpPost("{id}/cancel")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutCancel(string id)
{
var orgIdGuid = new Guid(id);
@ -209,6 +238,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/reinstate")]
[HttpPost("{id}/reinstate")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutReinstate(string id)
{
var orgIdGuid = new Guid(id);

View File

@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;
namespace Bit.Api.Utilities
{
public static class ApiHelpers
{
public async static Task<T> ReadJsonFileFromBody<T>(HttpContext httpContext, IFormFile file, long maxSize = 51200)
{
T obj = default(T);
if(file != null && httpContext.Request.ContentLength.HasValue && httpContext.Request.ContentLength.Value <= maxSize)
{
try
{
using(var stream = file.OpenReadStream())
using(var reader = new StreamReader(stream))
{
var s = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(s))
{
obj = JsonConvert.DeserializeObject<T>(s);
}
}
}
catch { }
}
return obj;
}
}
}