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

update premium license and self host attr checks

This commit is contained in:
Kyle Spearrin
2017-08-14 12:08:57 -04:00
parent 9e566e90a9
commit 18cbc79dd2
6 changed files with 107 additions and 0 deletions

View File

@ -15,6 +15,7 @@ using Bit.Core;
using System.IO;
using Newtonsoft.Json;
using Bit.Core.Models.Business;
using Bit.Api.Utilities;
namespace Bit.Api.Controllers
{
@ -458,6 +459,7 @@ namespace Bit.Api.Controllers
[HttpPut("payment")]
[HttpPost("payment")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutPayment([FromBody]PaymentRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
@ -471,6 +473,7 @@ namespace Bit.Api.Controllers
[HttpPut("storage")]
[HttpPost("storage")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutStorage([FromBody]StorageRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
@ -482,8 +485,46 @@ namespace Bit.Api.Controllers
await _userService.AdjustStorageAsync(user, model.StorageGbAdjustment.Value);
}
[HttpPut("license")]
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task PutLicense(UpdateLicenseRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
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 { }
}
if(license == null)
{
throw new BadRequestException("Invalid license");
}
await _userService.UpdateLicenseAsync(user, license);
}
[HttpPut("cancel-premium")]
[HttpPost("cancel-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutCancel()
{
var user = await _userService.GetUserByPrincipalAsync(User);
@ -497,6 +538,7 @@ namespace Bit.Api.Controllers
[HttpPut("reinstate-premium")]
[HttpPost("reinstate-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutReinstate()
{
var user = await _userService.GetUserByPrincipalAsync(User);