/
StorageCredentials.cs
351 lines (314 loc) · 13.6 KB
/
StorageCredentials.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//-----------------------------------------------------------------------
// <copyright file="StorageCredentials.cs" company="Microsoft">
// Copyright 2012 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.WindowsAzure.Storage.Auth
{
using Microsoft.WindowsAzure.Storage.Core;
using Microsoft.WindowsAzure.Storage.Core.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
/// <summary>
/// Represents a set of credentials used to authenticate access to a Windows Azure storage account.
/// </summary>
public sealed class StorageCredentials
{
private UriQueryBuilder queryBuilder;
/// <summary>
/// Gets the associated shared access signature token for the credentials.
/// </summary>
/// <value>The shared access signature token.</value>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SAS", Justification = "Back compatibility.")]
public string SASToken { get; private set; }
/// <summary>
/// Gets the associated account name for the credentials.
/// </summary>
/// <value>The account name.</value>
public string AccountName { get; private set; }
/// <summary>
/// Gets the associated key name for the credentials.
/// </summary>
/// <value>The key name.</value>
public string KeyName
{
get
{
return this.Key.KeyName;
}
}
internal StorageAccountKey Key { get; private set; }
/// <summary>
/// Gets a value indicating whether the credentials are for anonymous access.
/// </summary>
/// <value><c>true</c> if the credentials are for anonymous access; otherwise, <c>false</c>.</value>
public bool IsAnonymous
{
get
{
return (this.SASToken == null) && (this.AccountName == null);
}
}
/// <summary>
/// Gets a value indicating whether the credentials are a shared access signature token.
/// </summary>
/// <value><c>true</c> if the credentials are a shared access signature token; otherwise, <c>false</c>.</value>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SAS", Justification = "Back compatibility.")]
public bool IsSAS
{
get
{
return (this.SASToken != null) && (this.AccountName == null);
}
}
/// <summary>
/// Gets a value indicating whether the credentials are a shared key.
/// </summary>
/// <value><c>true</c> if the credentials are a shared key; otherwise, <c>false</c>.</value>
public bool IsSharedKey
{
get
{
return (this.SASToken == null) && (this.AccountName != null);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class.
/// </summary>
public StorageCredentials()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class with the specified account name and key value.
/// </summary>
/// <param name="accountName">A string that represents the name of the storage account.</param>
/// <param name="keyValue">A string that represents the Base-64-encoded account access key.</param>
public StorageCredentials(string accountName, string keyValue)
: this(accountName, keyValue, null)
{
}
#if WINDOWS_DESKTOP
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class with the specified account name and key value.
/// </summary>
/// <param name="accountName">A string that represents the name of the storage account.</param>
/// <param name="keyValue">An array of bytes that represent the account access key.</param>
public StorageCredentials(string accountName, byte[] keyValue)
: this(accountName, keyValue, null)
{
}
#endif
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class with the specified account name, key value, and key name.
/// </summary>
/// <param name="accountName">A string that represents the name of the storage account.</param>
/// <param name="keyValue">A string that represents the Base-64-encoded account access key.</param>
/// <param name="keyName">A string that represents the name of the key.</param>
public StorageCredentials(string accountName, string keyValue, string keyName)
{
if (string.IsNullOrEmpty(accountName))
{
throw new ArgumentNullException("accountName");
}
this.AccountName = accountName;
this.UpdateKey(keyValue, keyName);
}
#if WINDOWS_DESKTOP
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class with the specified account name, key value, and key name.
/// </summary>
/// <param name="accountName">A string that represents the name of the storage account.</param>
/// <param name="keyValue">An array of bytes that represent the account access key.</param>
/// <param name="keyName">A string that represents the name of the key.</param>
public StorageCredentials(string accountName, byte[] keyValue, string keyName)
{
if (string.IsNullOrEmpty(accountName))
{
throw new ArgumentNullException("accountName");
}
this.AccountName = accountName;
this.UpdateKey(keyValue, keyName);
}
#endif
/// <summary>
/// Initializes a new instance of the <see cref="StorageCredentials"/> class with the specified shared access signature token.
/// </summary>
/// <param name="sasToken">A string representing the shared access signature token.</param>
public StorageCredentials(string sasToken)
{
if (string.IsNullOrEmpty(sasToken))
{
throw new ArgumentNullException("sasToken");
}
this.SASToken = sasToken;
this.UpdateQueryBuilder();
}
/// <summary>
/// Updates the key value for the credentials.
/// </summary>
/// <param name="keyValue">The key value, as a Base-64 encoded string, to update.</param>
public void UpdateKey(string keyValue)
{
this.UpdateKey(keyValue, null);
}
#if WINDOWS_DESKTOP
/// <summary>
/// Updates the key value for the credentials.
/// </summary>
/// <param name="keyValue">The key value, as an array of bytes, to update.</param>
public void UpdateKey(byte[] keyValue)
{
this.UpdateKey(keyValue, null);
}
#endif
/// <summary>
/// Updates the key value and key name for the credentials.
/// </summary>
/// <param name="keyValue">The key value, as a Base-64 encoded string, to update.</param>
/// <param name="keyName">The key name to update.</param>
public void UpdateKey(string keyValue, string keyName)
{
if (!this.IsSharedKey)
{
string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.CannotUpdateKeyWithoutAccountKeyCreds);
throw new InvalidOperationException(errorMessage);
}
if (keyValue == null)
{
throw new ArgumentNullException("keyValue");
}
this.Key = new StorageAccountKey(keyName, Convert.FromBase64String(keyValue));
}
#if WINDOWS_DESKTOP
/// <summary>
/// Updates the key value and key name for the credentials.
/// </summary>
/// <param name="keyValue">The key value, as an array of bytes, to update.</param>
/// <param name="keyName">The key name to update.</param>
public void UpdateKey(byte[] keyValue, string keyName)
{
if (!this.IsSharedKey)
{
string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.CannotUpdateKeyWithoutAccountKeyCreds);
throw new InvalidOperationException(errorMessage);
}
if (keyValue == null)
{
throw new ArgumentNullException("keyValue");
}
this.Key = new StorageAccountKey(keyName, keyValue);
}
#endif
/// <summary>
/// Updates the sas token value for the credentials.
/// </summary>
/// <param name="sasToken">The sasToken value,as a string, to update.</param>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SAS", Justification = "Back compatibility.")]
public void UpdateSASToken(string sasToken)
{
if (!this.IsSAS)
{
string errorMessage = string.Format(CultureInfo.CurrentCulture, SR.CannotUpdateSasWithoutSasCreds);
throw new InvalidOperationException(errorMessage);
}
if (string.IsNullOrEmpty(sasToken))
{
throw new ArgumentNullException("sasToken");
}
this.SASToken = sasToken;
this.UpdateQueryBuilder();
}
/// <summary>
/// Returns the key for the credentials.
/// </summary>
/// <returns>An array of bytes that contains the key.</returns>
public byte[] ExportKey()
{
return (byte[])this.Key.KeyValue.Clone();
}
/// <summary>
/// Transforms a resource URI into a shared access signature URI, by appending a shared access token.
/// </summary>
/// <param name="resourceUri">A <see cref="Uri"/> object that represents the resource URI to be transformed.</param>
/// <returns>A <see cref="Uri"/> object that represents the signature, including the resource URI and the shared access token.</returns>
public Uri TransformUri(Uri resourceUri)
{
if (this.IsSAS)
{
return this.queryBuilder.AddToUri(resourceUri);
}
else
{
return resourceUri;
}
}
/// <summary>
/// Exports the value of the access key to a Base64-encoded string.
/// </summary>
/// <returns>The account access key.</returns>
public string ExportBase64EncodedKey()
{
StorageAccountKey localKey = this.Key;
return (localKey.KeyValue == null) ? null : Convert.ToBase64String(localKey.KeyValue);
}
internal string ToString(bool exportSecrets)
{
if (this.IsSharedKey)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0}={1};{2}={3}",
CloudStorageAccount.AccountNameSettingString,
this.AccountName,
CloudStorageAccount.AccountKeySettingString,
exportSecrets ? this.ExportBase64EncodedKey() : "[key hidden]");
}
if (this.IsSAS)
{
return string.Format(CultureInfo.InvariantCulture, "{0}={1}", CloudStorageAccount.SharedAccessSignatureSettingString, exportSecrets ? this.SASToken : "[signature hidden]");
}
return string.Empty;
}
/// <summary>
/// Determines whether an other <see cref="StorageCredentials"/> object is equal to this one by comparing their SAS tokens, account names, key names, and key values.
/// </summary>
/// <param name="other">The <see cref="StorageCredentials"/> object to compare to this one.</param>
/// <returns><c>true</c> if the two <see cref="StorageCredentials"/> objects are equal; otherwise, <c>false</c>.</returns>
public bool Equals(StorageCredentials other)
{
if (other == null)
{
return false;
}
else
{
return string.Equals(this.SASToken, other.SASToken) &&
string.Equals(this.AccountName, other.AccountName) &&
string.Equals(this.KeyName, other.KeyName) &&
string.Equals(this.ExportBase64EncodedKey(), other.ExportBase64EncodedKey());
}
}
private void UpdateQueryBuilder()
{
this.queryBuilder = new UriQueryBuilder();
IDictionary<string, string> parameters = HttpWebUtility.ParseQueryString(this.SASToken);
foreach (KeyValuePair<string, string> parameter in parameters)
{
this.queryBuilder.Add(parameter.Key, parameter.Value);
}
}
}
}