| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.Text; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage.Sas |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// <see cref="AccountSasBuilder"/> is used to generate an account level |
| | 13 | | /// Shared Access Signature (SAS) for Azure Storage services. |
| | 14 | | /// For more information, see |
| | 15 | | /// <see href="https://docs.microsoft.com/rest/api/storageservices/constructing-an-account-sas"> |
| | 16 | | /// Create an account SAS</see>. |
| | 17 | | /// </summary> |
| | 18 | | public class AccountSasBuilder |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// The storage service version to use to authenticate requests made |
| | 22 | | /// with this shared access signature, and the service version to use |
| | 23 | | /// when handling requests made with this shared access signature. |
| | 24 | | /// </summary> |
| 528 | 25 | | public string Version { get; set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The optional signed protocol field specifies the protocol |
| | 29 | | /// permitted for a request made with the SAS. Possible values are |
| | 30 | | /// <see cref="SasProtocol.HttpsAndHttp"/>, |
| | 31 | | /// <see cref="SasProtocol.Https"/>, and |
| | 32 | | /// <see cref="SasProtocol.None"/>. |
| | 33 | | /// </summary> |
| 362 | 34 | | public SasProtocol Protocol { get; set; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Optionally specify the time at which the shared access signature |
| | 38 | | /// becomes valid. If omitted when DateTimeOffset.MinValue is used, |
| | 39 | | /// start time for this call is assumed to be the time when the |
| | 40 | | /// storage service receives the request. |
| | 41 | | /// </summary> |
| 374 | 42 | | public DateTimeOffset StartsOn { get; set; } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The time at which the shared access signature becomes invalid. |
| | 46 | | /// This field must be omitted if it has been specified in an |
| | 47 | | /// associated stored access policy. |
| | 48 | | /// </summary> |
| 542 | 49 | | public DateTimeOffset ExpiresOn { get; set; } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// The permissions associated with the shared access signature. The |
| | 53 | | /// user is restricted to operations allowed by the permissions. The |
| | 54 | | /// <see cref="AccountSasPermissions"/> type can be used to create the |
| | 55 | | /// permissions string. |
| | 56 | | /// </summary> |
| 528 | 57 | | public string Permissions { get; private set; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Specifies an IP address or a range of IP addresses from which to |
| | 61 | | /// accept requests. If the IP address from which the request |
| | 62 | | /// originates does not match the IP address or address range |
| | 63 | | /// specified on the SAS token, the request is not authenticated. |
| | 64 | | /// When specifying a range of IP addresses, note that the range is |
| | 65 | | /// inclusive. |
| | 66 | | /// </summary> |
| 332 | 67 | | public SasIPRange IPRange { get; set; } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// The services associated with the shared access signature. The |
| | 71 | | /// user is restricted to operations with the specified services. |
| | 72 | | /// </summary> |
| 542 | 73 | | public AccountSasServices Services { get; set; } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// The resource types associated with the shared access signature. The |
| | 77 | | /// user is restricted to operations on the specified resources. |
| | 78 | | /// </summary> |
| 542 | 79 | | public AccountSasResourceTypes ResourceTypes { get; set; } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Sets the permissions for an account SAS. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="permissions"> |
| | 85 | | /// <see cref="AccountSasPermissions"/> containing the allowed permissions. |
| | 86 | | /// </param> |
| | 87 | | public void SetPermissions(AccountSasPermissions permissions) |
| | 88 | | { |
| 104 | 89 | | Permissions = permissions.ToPermissionsString(); |
| 104 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Sets the permissions for the SAS using a raw permissions string. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="rawPermissions">Raw permissions string for the SAS.</param> |
| | 96 | | public void SetPermissions(string rawPermissions) |
| | 97 | | { |
| 42 | 98 | | Permissions = SasExtensions.ValidateAndSanitizeRawPermissions( |
| 42 | 99 | | permissions: rawPermissions, |
| 42 | 100 | | validPermissionsInOrder: s_validPermissionsInOrder); |
| 28 | 101 | | } |
| | 102 | |
|
| 7 | 103 | | private static readonly List<char> s_validPermissionsInOrder = new List<char> |
| 7 | 104 | | { |
| 7 | 105 | | Constants.Sas.Permissions.Read, |
| 7 | 106 | | Constants.Sas.Permissions.Write, |
| 7 | 107 | | Constants.Sas.Permissions.Delete, |
| 7 | 108 | | Constants.Sas.Permissions.DeleteBlobVersion, |
| 7 | 109 | | Constants.Sas.Permissions.List, |
| 7 | 110 | | Constants.Sas.Permissions.Add, |
| 7 | 111 | | Constants.Sas.Permissions.Create, |
| 7 | 112 | | Constants.Sas.Permissions.Update, |
| 7 | 113 | | Constants.Sas.Permissions.Process, |
| 7 | 114 | | Constants.Sas.Permissions.Tag, |
| 7 | 115 | | Constants.Sas.Permissions.FilterByTags, |
| 7 | 116 | | }; |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this |
| | 120 | | /// shared access signature values to produce the proper SAS query |
| | 121 | | /// parameters for authenticating requests. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="sharedKeyCredential"> |
| | 124 | | /// The storage account's <see cref="StorageSharedKeyCredential"/>. |
| | 125 | | /// </param> |
| | 126 | | /// <returns> |
| | 127 | | /// The <see cref="SasQueryParameters"/> used for authenticating |
| | 128 | | /// requests. |
| | 129 | | /// </returns> |
| | 130 | | public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential) |
| | 131 | | { |
| | 132 | | // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS |
| 132 | 133 | | sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential)); |
| | 134 | |
|
| 132 | 135 | | if (ExpiresOn == default || string.IsNullOrEmpty(Permissions) || ResourceTypes == default || Services == def |
| | 136 | | { |
| 0 | 137 | | throw Errors.AccountSasMissingData(); |
| | 138 | | } |
| 132 | 139 | | if (string.IsNullOrEmpty(Version)) |
| | 140 | | { |
| 100 | 141 | | Version = SasQueryParameters.DefaultSasVersion; |
| | 142 | | } |
| 132 | 143 | | var startTime = SasExtensions.FormatTimesForSasSigning(StartsOn); |
| 132 | 144 | | var expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn); |
| | 145 | |
|
| | 146 | | // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx |
| 132 | 147 | | var stringToSign = string.Join("\n", |
| 132 | 148 | | sharedKeyCredential.AccountName, |
| 132 | 149 | | Permissions, |
| 132 | 150 | | Services.ToPermissionsString(), |
| 132 | 151 | | ResourceTypes.ToPermissionsString(), |
| 132 | 152 | | startTime, |
| 132 | 153 | | expiryTime, |
| 132 | 154 | | IPRange.ToString(), |
| 132 | 155 | | Protocol.ToProtocolString(), |
| 132 | 156 | | Version, |
| 132 | 157 | | ""); // That's right, the account SAS requires a terminating extra newline |
| | 158 | |
|
| 132 | 159 | | var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign); |
| 132 | 160 | | var p = SasQueryParametersInternals.Create( |
| 132 | 161 | | Version, |
| 132 | 162 | | Services, |
| 132 | 163 | | ResourceTypes, |
| 132 | 164 | | Protocol, |
| 132 | 165 | | StartsOn, |
| 132 | 166 | | ExpiresOn, |
| 132 | 167 | | IPRange, |
| 132 | 168 | | null, // Identifier |
| 132 | 169 | | null, // Resource |
| 132 | 170 | | Permissions, |
| 132 | 171 | | signature); |
| 132 | 172 | | return p; |
| | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Returns a string that represents the current object. |
| | 177 | | /// </summary> |
| | 178 | | /// <returns>A string that represents the current object.</returns> |
| | 179 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 180 | | public override string ToString() => base.ToString(); |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Check if two <see cref="AccountSasBuilder"/> instances are equal. |
| | 184 | | /// </summary> |
| | 185 | | /// <param name="obj">The instance to compare to.</param> |
| | 186 | | /// <returns>True if they're equal, false otherwise.</returns> |
| | 187 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 188 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Get a hash code for the <see cref="AccountSasBuilder"/>. |
| | 192 | | /// </summary> |
| | 193 | | /// <returns>Hash code for the <see cref="AccountSasBuilder"/>.</returns> |
| | 194 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 195 | | public override int GetHashCode() => base.GetHashCode(); |
| | 196 | | } |
| | 197 | | } |