-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
SharedAccessAuthorizationRule.cs
346 lines (302 loc) · 14.2 KB
/
SharedAccessAuthorizationRule.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using Azure.Core;
using Azure.Messaging.ServiceBus.Authorization;
namespace Azure.Messaging.ServiceBus.Administration
{
/// <summary>
/// Defines the authorization rule for an entity using SAS.
/// </summary>
public class SharedAccessAuthorizationRule : AuthorizationRule
{
private const int SupportedSASKeyLength = 44;
private const string FixedClaimType = "SharedAccessKey";
private string internalKeyName;
private string internalPrimaryKey;
private string internalSecondaryKey;
private List<AccessRights> internalRights;
internal SharedAccessAuthorizationRule()
{
}
/// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
/// <param name="keyName">The authorization rule key name.</param>
/// <param name="rights">The list of rights.</param>
public SharedAccessAuthorizationRule(string keyName, IEnumerable<AccessRights> rights)
: this(keyName, SharedAccessAuthorizationRule.GenerateRandomKey(), SharedAccessAuthorizationRule.GenerateRandomKey(), rights)
{
}
/// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
/// <param name="keyName">The authorization rule key name.</param>
/// <param name="primaryKey">The primary key for the authorization rule.</param>
/// <param name="rights">The list of rights.</param>
public SharedAccessAuthorizationRule(string keyName, string primaryKey, IEnumerable<AccessRights> rights)
: this(keyName, primaryKey, SharedAccessAuthorizationRule.GenerateRandomKey(), rights)
{
}
/// <summary>Initializes a new instance of the <see cref="SharedAccessAuthorizationRule" /> class.</summary>
/// <param name="keyName">The authorization rule key name.</param>
/// <param name="primaryKey">The primary key for the authorization rule.</param>
/// <param name="secondaryKey">The secondary key for the authorization rule.</param>
/// <param name="rights">The list of rights.</param>
public SharedAccessAuthorizationRule(string keyName, string primaryKey, string secondaryKey, IEnumerable<AccessRights> rights)
{
PrimaryKey = primaryKey;
SecondaryKey = secondaryKey;
Rights = new List<AccessRights>(rights);
KeyName = keyName;
}
internal override AuthorizationRule Clone() =>
new SharedAccessAuthorizationRule(KeyName, PrimaryKey, SecondaryKey, Rights);
/// <inheritdoc/>
public override string ClaimType => FixedClaimType;
internal override string ClaimValue => "None";
/// <summary>Gets or sets the authorization rule key name.</summary>
/// <value>The authorization rule key name.</value>
public sealed override string KeyName
{
get { return internalKeyName; }
set
{
Argument.AssertNotNullOrWhiteSpace(value, nameof(KeyName));
Argument.AssertNotTooLong(
value,
SharedAccessSignature.MaximumKeyNameLength,
nameof(KeyName));
if (!string.Equals(value, HttpUtility.UrlEncode(value), StringComparison.InvariantCulture))
{
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentException(
"The key name specified contains invalid characters",
nameof(KeyName));
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
internalKeyName = value;
}
}
/// <summary>Gets or sets the primary key for the authorization rule.</summary>
/// <value>The primary key for the authorization rule.</value>
public string PrimaryKey
{
get { return internalPrimaryKey; }
set
{
Argument.AssertNotNullOrWhiteSpace(value, nameof(PrimaryKey));
if (Encoding.ASCII.GetByteCount(value) != SupportedSASKeyLength)
{
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentOutOfRangeException(
nameof(PrimaryKey),
$"{nameof(SharedAccessAuthorizationRule)} only supports keys of size {SupportedSASKeyLength} bytes");
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
if (!CheckBase64(value))
{
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentException($"{nameof(SharedAccessAuthorizationRule)} only supports base64 keys.",
nameof(PrimaryKey));
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
internalPrimaryKey = value;
}
}
/// <summary>Gets or sets the secondary key for the authorization rule.</summary>
/// <value>The secondary key for the authorization rule.</value>
public string SecondaryKey
{
get { return internalSecondaryKey; }
set
{
Argument.AssertNotNullOrWhiteSpace(value, nameof(SecondaryKey));
if (Encoding.ASCII.GetByteCount(value) != SupportedSASKeyLength)
{
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentOutOfRangeException(
nameof(SecondaryKey),
$"{nameof(SharedAccessAuthorizationRule)} only supports keys of size {SupportedSASKeyLength} bytes");
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
if (!CheckBase64(value))
{
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentException(
$"{nameof(SharedAccessAuthorizationRule)} only supports base64 keys.",
nameof(SecondaryKey));
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
internalSecondaryKey = value;
}
}
/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "<Pending>")]
public override List<AccessRights> Rights
{
get => internalRights;
set
{
if (value == null || value.Count < 0 || value.Count > AdministrationClientConstants.SupportedClaimsCount)
{
throw new ArgumentException($"Rights cannot be null, empty or greater than {AdministrationClientConstants.SupportedClaimsCount}");
}
HashSet<AccessRights> dedupedAccessRights = new HashSet<AccessRights>(value);
if (value.Count != dedupedAccessRights.Count)
{
throw new ArgumentException("Access rights on an authorization rule must be unique");
}
if (value.Contains(AccessRights.Manage) && value.Count != 3)
{
throw new ArgumentException("Manage permission should also include Send and Listen");
}
internalRights = value;
}
}
/// <summary>Returns the hash code for this instance.</summary>
public override int GetHashCode()
{
int hash = 13;
unchecked
{
hash = (hash * 7) + KeyName?.GetHashCode() ?? 0;
hash = (hash * 7) + PrimaryKey?.GetHashCode() ?? 0;
hash = (hash * 7) + SecondaryKey?.GetHashCode() ?? 0;
hash = (hash * 7) + Rights.GetHashCode();
}
return hash;
}
/// <summary>Determines whether the specified object is equal to the current object.</summary>
public override bool Equals(object obj)
{
var other = obj as AuthorizationRule;
return Equals(other);
}
/// <summary>Determines whether the specified object is equal to the current object.</summary>
/// <param name="other">The object to compare with the current object.</param>
/// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
public override bool Equals(AuthorizationRule other)
{
if (!(other is SharedAccessAuthorizationRule))
{
return false;
}
SharedAccessAuthorizationRule comparand = (SharedAccessAuthorizationRule)other;
if (!string.Equals(KeyName, comparand.KeyName, StringComparison.OrdinalIgnoreCase) ||
!string.Equals(PrimaryKey, comparand.PrimaryKey, StringComparison.Ordinal) ||
!string.Equals(SecondaryKey, comparand.SecondaryKey, StringComparison.Ordinal))
{
return false;
}
if ((Rights != null && comparand.Rights == null) ||
(Rights == null && comparand.Rights != null))
{
return false;
}
if (Rights != null && comparand.Rights != null)
{
HashSet<AccessRights> thisRights = new HashSet<AccessRights>(Rights);
if (comparand.Rights.Count != thisRights.Count)
{
return false;
}
return thisRights.SetEquals(comparand.Rights);
}
return true;
}
/// <summary></summary>
public static bool operator ==(SharedAccessAuthorizationRule left, SharedAccessAuthorizationRule right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
{
return false;
}
return left.Equals(right);
}
/// <summary></summary>
public static bool operator !=(SharedAccessAuthorizationRule left, SharedAccessAuthorizationRule right)
{
return !(left == right);
}
/// <summary>Generates the random key for the authorization rule.</summary>
private static string GenerateRandomKey()
{
byte[] key256 = new byte[32];
using (var rngCryptoServiceProvider = new RNGCryptoServiceProvider())
{
rngCryptoServiceProvider.GetBytes(key256);
}
return Convert.ToBase64String(key256);
}
private static bool CheckBase64(string base64EncodedString)
{
try
{
Convert.FromBase64String(base64EncodedString);
return true;
}
catch (Exception)
{
return false;
}
}
internal static new SharedAccessAuthorizationRule ParseFromXElement(XElement xElement)
{
var rule = new SharedAccessAuthorizationRule();
foreach (var element in xElement.Elements())
{
switch (element.Name.LocalName)
{
case "CreatedTime":
rule.CreatedTime = XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
break;
case "ModifiedTime":
rule.ModifiedTime = XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
break;
case "KeyName":
rule.KeyName = element.Value;
break;
case "PrimaryKey":
rule.PrimaryKey = element.Value;
break;
case "SecondaryKey":
rule.SecondaryKey = element.Value;
break;
case "Rights":
var rights = new List<AccessRights>();
var xRights = element.Elements(XName.Get("AccessRights", AdministrationClientConstants.ServiceBusNamespace));
foreach (var xRight in xRights)
{
rights.Add((AccessRights)Enum.Parse(typeof(AccessRights), xRight.Value));
}
rule.Rights = rights;
break;
}
}
return rule;
}
internal override XElement Serialize()
{
XElement rule = new XElement(
XName.Get("AuthorizationRule", AdministrationClientConstants.ServiceBusNamespace),
new XAttribute(XName.Get("type", AdministrationClientConstants.XmlSchemaInstanceNamespace), nameof(SharedAccessAuthorizationRule)),
new XElement(XName.Get("ClaimType", AdministrationClientConstants.ServiceBusNamespace), ClaimType),
new XElement(XName.Get("ClaimValue", AdministrationClientConstants.ServiceBusNamespace), ClaimValue),
new XElement(XName.Get("Rights", AdministrationClientConstants.ServiceBusNamespace),
Rights.Select(right => new XElement(XName.Get("AccessRights", AdministrationClientConstants.ServiceBusNamespace), right.ToString()))),
new XElement(XName.Get("KeyName", AdministrationClientConstants.ServiceBusNamespace), KeyName),
new XElement(XName.Get("PrimaryKey", AdministrationClientConstants.ServiceBusNamespace), PrimaryKey),
new XElement(XName.Get("SecondaryKey", AdministrationClientConstants.ServiceBusNamespace), SecondaryKey)
);
return rule;
}
}
}