1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

Send APIs (#979)

* send work

* fix sql proj file

* update

* updates

* access id

* delete job

* fix delete job

* local send storage

* update sprocs for null checks
This commit is contained in:
Kyle Spearrin
2020-11-02 15:55:49 -05:00
committed by GitHub
parent a5db233e51
commit 82dd364e65
39 changed files with 1774 additions and 11 deletions

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Models.Api
{
public class SendAccessRequestModel
{
[StringLength(300)]
public string Password { get; set; }
}
}

View File

@ -0,0 +1,94 @@
using System;
using Bit.Core.Utilities;
using Bit.Core.Models.Table;
using Bit.Core.Enums;
using Newtonsoft.Json;
using Bit.Core.Models.Data;
using System.ComponentModel.DataAnnotations;
using Bit.Core.Services;
namespace Bit.Core.Models.Api
{
public class SendRequestModel
{
public SendType Type { get; set; }
[EncryptedString]
[EncryptedStringLength(1000)]
public string Name { get; set; }
[EncryptedString]
[EncryptedStringLength(1000)]
public string Notes { get; set; }
[Required]
[EncryptedString]
[EncryptedStringLength(1000)]
public string Key { get; set; }
public int? MaxAccessCount { get; set; }
public DateTime? ExpirationDate { get; set; }
[Required]
public DateTime? DeletionDate { get; set; }
public SendFileModel File { get; set; }
public SendTextModel Text { get; set; }
[StringLength(1000)]
public string Password { get; set; }
[Required]
public bool? Disabled { get; set; }
public Send ToSend(Guid userId, ISendService sendService)
{
var send = new Send
{
Type = Type,
UserId = (Guid?)userId
};
ToSend(send, sendService);
return send;
}
public (Send, SendFileData) ToSend(Guid userId, string fileName, ISendService sendService)
{
var send = ToSendBase(new Send
{
Type = Type,
UserId = (Guid?)userId
}, sendService);
var data = new SendFileData(this, fileName);
return (send, data);
}
public Send ToSend(Send existingSend, ISendService sendService)
{
existingSend = ToSendBase(existingSend, sendService);
switch (existingSend.Type)
{
case SendType.File:
var fileData = JsonConvert.DeserializeObject<SendFileData>(existingSend.Data);
fileData.Name = Name;
fileData.Notes = Notes;
existingSend.Data = JsonConvert.SerializeObject(fileData,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case SendType.Text:
existingSend.Data = JsonConvert.SerializeObject(new SendTextData(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
default:
throw new ArgumentException("Unsupported type: " + nameof(Type) + ".");
}
return existingSend;
}
private Send ToSendBase(Send existingSend, ISendService sendService)
{
existingSend.Key = Key;
existingSend.ExpirationDate = ExpirationDate;
existingSend.DeletionDate = DeletionDate.Value;
existingSend.MaxAccessCount = MaxAccessCount;
if (!string.IsNullOrWhiteSpace(Password))
{
existingSend.Password = sendService.HashPassword(Password);
}
existingSend.Disabled = Disabled.GetValueOrDefault();
return existingSend;
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
using Newtonsoft.Json;
namespace Bit.Core.Models.Api
{
public class SendAccessResponseModel : ResponseModel
{
public SendAccessResponseModel(Send send, GlobalSettings globalSettings)
: base("send-access")
{
if (send == null)
{
throw new ArgumentNullException(nameof(send));
}
Id = CoreHelpers.Base64UrlEncode(send.Id.ToByteArray());
Type = send.Type;
SendData sendData;
switch (send.Type)
{
case SendType.File:
var fileData = JsonConvert.DeserializeObject<SendFileData>(send.Data);
sendData = fileData;
File = new SendFileModel(fileData, globalSettings);
break;
case SendType.Text:
var textData = JsonConvert.DeserializeObject<SendTextData>(send.Data);
sendData = textData;
Text = new SendTextModel(textData);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
Name = sendData.Name;
}
public string Id { get; set; }
public SendType Type { get; set; }
public string Name { get; set; }
public SendFileModel File { get; set; }
public SendTextModel Text { get; set; }
}
}

View File

@ -0,0 +1,69 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
using Newtonsoft.Json;
namespace Bit.Core.Models.Api
{
public class SendResponseModel : ResponseModel
{
public SendResponseModel(Send send, GlobalSettings globalSettings)
: base("send")
{
if (send == null)
{
throw new ArgumentNullException(nameof(send));
}
Id = send.Id.ToString();
AccessId = CoreHelpers.Base64UrlEncode(send.Id.ToByteArray());
Type = send.Type;
Key = send.Key;
MaxAccessCount = send.MaxAccessCount;
AccessCount = send.AccessCount;
RevisionDate = send.RevisionDate;
ExpirationDate = send.ExpirationDate;
DeletionDate = send.DeletionDate;
Password = send.Password;
Disabled = send.Disabled;
SendData sendData;
switch (send.Type)
{
case SendType.File:
var fileData = JsonConvert.DeserializeObject<SendFileData>(send.Data);
sendData = fileData;
File = new SendFileModel(fileData, globalSettings);
break;
case SendType.Text:
var textData = JsonConvert.DeserializeObject<SendTextData>(send.Data);
sendData = textData;
Text = new SendTextModel(textData);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
Name = sendData.Name;
Notes = sendData.Notes;
}
public string Id { get; set; }
public string AccessId { get; set; }
public SendType Type { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public SendFileModel File { get; set; }
public SendTextModel Text { get; set; }
public string Key { get; set; }
public int? MaxAccessCount { get; set; }
public int AccessCount { get; set; }
public string Password { get; set; }
public bool Disabled { get; set; }
public DateTime RevisionDate { get; set; }
public DateTime? ExpirationDate { get; set; }
public DateTime DeletionDate { get; set; }
}
}

View File

@ -0,0 +1,27 @@
using Bit.Core.Models.Data;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Api
{
public class SendFileModel
{
public SendFileModel() { }
public SendFileModel(SendFileData data, GlobalSettings globalSettings)
{
Id = data.Id;
Url = $"{globalSettings.Send.BaseUrl}/{data.Id}";
FileName = data.FileName;
Size = data.SizeString;
SizeName = CoreHelpers.ReadableBytesSize(data.Size);
}
public string Id { get; set; }
public string Url { get; set; }
[EncryptedString]
[EncryptedStringLength(1000)]
public string FileName { get; set; }
public string Size { get; set; }
public string SizeName { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using Bit.Core.Models.Data;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Api
{
public class SendTextModel
{
public SendTextModel() { }
public SendTextModel(SendTextData data)
{
Text = data.Text;
Hidden = data.Hidden;
}
[EncryptedString]
[EncryptedStringLength(1000)]
public string Text { get; set; }
public bool Hidden { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public abstract class SendData
{
public SendData() { }
public SendData(SendRequestModel send)
{
Name = send.Name;
Notes = send.Notes;
}
public string Name { get; set; }
public string Notes { get; set; }
}
}

View File

@ -0,0 +1,37 @@
using System;
using Bit.Core.Models.Api;
using Newtonsoft.Json;
namespace Bit.Core.Models.Data
{
public class SendFileData : SendData
{
private long _size;
public SendFileData() { }
public SendFileData(SendRequestModel send, string fileName)
: base(send)
{
FileName = fileName;
}
[JsonIgnore]
public long Size
{
get { return _size; }
set { _size = value; }
}
// We serialize Size as a string since JSON (or Javascript) doesn't support full precision for long numbers
[JsonProperty("Size")]
public string SizeString
{
get { return _size.ToString(); }
set { _size = Convert.ToInt64(value); }
}
public string Id { get; set; }
public string FileName { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public class SendTextData : SendData
{
public SendTextData() { }
public SendTextData(SendRequestModel send)
: base(send)
{
Text = send.Text.Text;
Hidden = send.Text.Hidden;
}
public string Text { get; set; }
public bool Hidden { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Table
{
public class Send : ITableObject<Guid>
{
public Guid Id { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public SendType Type { get; set; }
public string Data { get; set; }
public string Key { get; set; }
public string Password { get; set; }
public int? MaxAccessCount { get; set; }
public int AccessCount { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public DateTime? ExpirationDate { get; set; }
public DateTime DeletionDate { get; set; }
public bool Disabled { get; set; }
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
}
}