SasExtensions
Extension methods for Sas.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Azure.Storage.Sas
{
internal static class SasExtensions
{
private const string NoneName = null;
private const string HttpsName = "https";
private const string HttpsAndHttpName = "https,http";
private const string HttpAndHttpsName = "http,https";
internal static string ToPermissionsString(this AccountSasResourceTypes resourceTypes)
{
StringBuilder stringBuilder = new StringBuilder();
if ((resourceTypes & AccountSasResourceTypes.Service) == AccountSasResourceTypes.Service)
stringBuilder.Append('s');
if ((resourceTypes & AccountSasResourceTypes.Container) == AccountSasResourceTypes.Container)
stringBuilder.Append('c');
if ((resourceTypes & AccountSasResourceTypes.Object) == AccountSasResourceTypes.Object)
stringBuilder.Append('o');
return stringBuilder.ToString();
}
internal static AccountSasResourceTypes ParseResourceTypes(string s)
{
AccountSasResourceTypes accountSasResourceTypes = (AccountSasResourceTypes)0;
foreach (char c in s) {
AccountSasResourceTypes accountSasResourceTypes2 = accountSasResourceTypes;
AccountSasResourceTypes accountSasResourceTypes3;
switch (c) {
case 's':
accountSasResourceTypes3 = AccountSasResourceTypes.Service;
break;
case 'c':
accountSasResourceTypes3 = AccountSasResourceTypes.Container;
break;
case 'o':
accountSasResourceTypes3 = AccountSasResourceTypes.Object;
break;
default:
throw Errors.InvalidResourceType(c);
}
accountSasResourceTypes = (accountSasResourceTypes2 | accountSasResourceTypes3);
}
return accountSasResourceTypes;
}
internal static string ToProtocolString(this SasProtocol protocol)
{
switch (protocol) {
case SasProtocol.Https:
return "https";
case SasProtocol.HttpsAndHttp:
return "https,http";
default:
return null;
}
}
public static SasProtocol ParseProtocol(string s)
{
if (s == null || (s != null && s.Length == 0))
return SasProtocol.None;
if (s == "https")
return SasProtocol.Https;
if (s == "https,http" || s == "http,https")
return SasProtocol.HttpsAndHttp;
throw Errors.InvalidSasProtocol("s", "SasProtocol");
}
internal static string ToPermissionsString(this AccountSasServices services)
{
StringBuilder stringBuilder = new StringBuilder();
if ((services & AccountSasServices.Blobs) == AccountSasServices.Blobs)
stringBuilder.Append('b');
if ((services & AccountSasServices.Files) == AccountSasServices.Files)
stringBuilder.Append('f');
if ((services & AccountSasServices.Queues) == AccountSasServices.Queues)
stringBuilder.Append('q');
if ((services & AccountSasServices.Tables) == AccountSasServices.Tables)
stringBuilder.Append('t');
return stringBuilder.ToString();
}
internal static AccountSasServices ParseAccountServices(string s)
{
AccountSasServices accountSasServices = (AccountSasServices)0;
foreach (char c in s) {
AccountSasServices accountSasServices2 = accountSasServices;
AccountSasServices accountSasServices3;
switch (c) {
case 'b':
accountSasServices3 = AccountSasServices.Blobs;
break;
case 'q':
accountSasServices3 = AccountSasServices.Queues;
break;
case 'f':
accountSasServices3 = AccountSasServices.Files;
break;
case 't':
accountSasServices3 = AccountSasServices.Tables;
break;
default:
throw Errors.InvalidService(c);
}
accountSasServices = (accountSasServices2 | accountSasServices3);
}
return accountSasServices;
}
internal static string FormatTimesForSasSigning(DateTimeOffset time)
{
if (!(time == default(DateTimeOffset)))
return time.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
return "";
}
internal static void AddToBuilder(StringBuilder sb, string key, string value)
{
sb.Append((sb.Length > 0) ? "&" : "").Append(key).Append('=')
.Append(value);
}
internal static string ValidateAndSanitizeRawPermissions(string permissions, List<char> validPermissionsInOrder)
{
if (permissions == null)
return null;
permissions = permissions.ToLowerInvariant();
HashSet<char> hashSet = new HashSet<char>(validPermissionsInOrder);
HashSet<char> hashSet2 = new HashSet<char>();
string text = permissions;
foreach (char c in text) {
if (!hashSet.Contains(c))
throw new ArgumentException($"{c}""");
hashSet2.Add(c);
}
StringBuilder stringBuilder = new StringBuilder();
foreach (char item in validPermissionsInOrder) {
if (hashSet2.Contains(item))
stringBuilder.Append(item);
}
return stringBuilder.ToString();
}
}
}