Added Amazon S3 signed payload option

This commit is contained in:
Jaex 2018-12-04 19:10:01 +03:00
parent 445b61e06a
commit 15cb8d3830
6 changed files with 2744 additions and 9684 deletions

View file

@ -1242,6 +1242,16 @@ public static byte[] ComputeSHA256(byte[] data)
}
}
public static byte[] ComputeSHA256(Stream stream, int bufferSize = 1024 * 32)
{
BufferedStream bufferedStream = new BufferedStream(stream, bufferSize);
using (SHA256Managed hashAlgorithm = new SHA256Managed())
{
return hashAlgorithm.ComputeHash(bufferedStream);
}
}
public static byte[] ComputeSHA256(string data)
{
return ComputeSHA256(Encoding.UTF8.GetBytes(data));

View file

@ -121,7 +121,16 @@ public override UploadResult Upload(Stream stream, string fileName)
string credential = URLHelpers.CombineURL(Settings.AccessKeyID, scope);
string timeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);
string contentType = UploadHelpers.GetMimeType(fileName);
string hashedPayload = "UNSIGNED-PAYLOAD";
string hashedPayload;
if (Settings.SignedPayload)
{
hashedPayload = Helpers.BytesToHex(Helpers.ComputeSHA256(stream));
}
else
{
hashedPayload = "UNSIGNED-PAYLOAD";
}
if ((Settings.RemoveExtensionImage && Helpers.IsImageFile(fileName)) ||
(Settings.RemoveExtensionText && Helpers.IsTextFile(fileName)) ||
@ -129,6 +138,7 @@ public override UploadResult Upload(Stream stream, string fileName)
{
fileName = Path.GetFileNameWithoutExtension(fileName);
}
string uploadPath = GetUploadPath(fileName);
NameValueCollection headers = new NameValueCollection
@ -140,7 +150,11 @@ public override UploadResult Upload(Stream stream, string fileName)
["x-amz-content-sha256"] = hashedPayload,
["x-amz-storage-class"] = Settings.StorageClass.ToString()
};
if (Settings.SetPublicACL) headers["x-amz-acl"] = "public-read";
if (Settings.SetPublicACL)
{
headers["x-amz-acl"] = "public-read";
}
string canonicalURI = uploadPath;
if (isPathStyleRequest) canonicalURI = URLHelpers.CombineURL(Settings.Bucket, canonicalURI);
@ -290,4 +304,4 @@ private string GetSignedHeaders(NameValueCollection headers)
return string.Join(";", headers.AllKeys.OrderBy(key => key).Select(key => key.ToLowerInvariant()));
}
}
}
}

View file

@ -36,10 +36,11 @@ public class AmazonS3Settings
public string ObjectPrefix { get; set; }
public bool UseCustomCNAME { get; set; }
public string CustomDomain { get; set; }
public bool SetPublicACL { get; set; } = true;
public AmazonS3StorageClass StorageClass { get; set; }
public bool RemoveExtensionImage { get; set; } = false;
public bool RemoveExtensionVideo { get; set; } = false;
public bool RemoveExtensionText { get; set; } = false;
public bool SetPublicACL { get; set; } = true;
public bool SignedPayload { get; set; }
public bool RemoveExtensionImage { get; set; }
public bool RemoveExtensionVideo { get; set; }
public bool RemoveExtensionText { get; set; }
}
}

View file

@ -719,6 +719,7 @@ private void InitializeComponent()
this.lblWidthHint = new System.Windows.Forms.Label();
this.ttlvMain = new ShareX.HelpersLib.TabToListView();
this.actRapidShareAccountType = new ShareX.UploadersLib.AccountTypeControl();
this.cbAmazonS3SignedPayload = new System.Windows.Forms.CheckBox();
this.tpOtherUploaders.SuspendLayout();
this.tcOtherUploaders.SuspendLayout();
this.tpTwitter.SuspendLayout();
@ -2877,6 +2878,7 @@ private void InitializeComponent()
//
// gbAmazonS3Advanced
//
this.gbAmazonS3Advanced.Controls.Add(this.cbAmazonS3SignedPayload);
this.gbAmazonS3Advanced.Controls.Add(this.lblAmazonS3StripExtension);
this.gbAmazonS3Advanced.Controls.Add(this.cbAmazonS3StripExtensionText);
this.gbAmazonS3Advanced.Controls.Add(this.cbAmazonS3StorageClass);
@ -5729,6 +5731,13 @@ private void InitializeComponent()
this.actRapidShareAccountType.Name = "actRapidShareAccountType";
this.actRapidShareAccountType.SelectedAccountType = ShareX.UploadersLib.AccountType.Anonymous;
//
// cbAmazonS3SignedPayload
//
resources.ApplyResources(this.cbAmazonS3SignedPayload, "cbAmazonS3SignedPayload");
this.cbAmazonS3SignedPayload.Name = "cbAmazonS3SignedPayload";
this.cbAmazonS3SignedPayload.UseVisualStyleBackColor = true;
this.cbAmazonS3SignedPayload.CheckedChanged += new System.EventHandler(this.cbAmazonS3SignedPayload_CheckedChanged);
//
// UploadersConfigForm
//
resources.ApplyResources(this, "$this");
@ -6588,5 +6597,6 @@ private void InitializeComponent()
private System.Windows.Forms.Label lblCustomUploaderRequestFormat;
private System.Windows.Forms.TabPage tpCustomUploaderFile;
private System.Windows.Forms.Button btnCustomUploaderDataMinify;
private System.Windows.Forms.CheckBox cbAmazonS3SignedPayload;
}
}

View file

@ -597,6 +597,7 @@ private void LoadFileUploaderSettings()
cbAmazonS3StorageClass.Items.AddRange(Helpers.GetLocalizedEnumDescriptions<AmazonS3StorageClass>());
cbAmazonS3StorageClass.SelectedIndex = (int)Config.AmazonS3Settings.StorageClass;
cbAmazonS3PublicACL.Checked = Config.AmazonS3Settings.SetPublicACL;
cbAmazonS3SignedPayload.Checked = Config.AmazonS3Settings.SignedPayload;
cbAmazonS3StripExtensionImage.Checked = Config.AmazonS3Settings.RemoveExtensionImage;
cbAmazonS3StripExtensionVideo.Checked = Config.AmazonS3Settings.RemoveExtensionVideo;
cbAmazonS3StripExtensionText.Checked = Config.AmazonS3Settings.RemoveExtensionText;
@ -2313,6 +2314,11 @@ private void cbAmazonS3PublicACL_CheckedChanged(object sender, EventArgs e)
Config.AmazonS3Settings.SetPublicACL = cbAmazonS3PublicACL.Checked;
}
private void cbAmazonS3SignedPayload_CheckedChanged(object sender, EventArgs e)
{
Config.AmazonS3Settings.SignedPayload = cbAmazonS3SignedPayload.Checked;
}
private void cbAmazonS3StripExtensionImage_CheckedChanged(object sender, EventArgs e)
{
Config.AmazonS3Settings.RemoveExtensionImage = cbAmazonS3StripExtensionImage.Checked;

File diff suppressed because it is too large Load diff