| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Net; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// A WCF SecurityToken that wraps a Shared Access Signature |
| | 13 | | /// </summary> |
| | 14 | | internal class SharedAccessSignatureToken : SecurityToken |
| | 15 | | { |
| | 16 | | internal const string SharedAccessSignature = "SharedAccessSignature"; |
| | 17 | | internal const string SignedResource = "sr"; |
| | 18 | | internal const string Signature = "sig"; |
| | 19 | | internal const string SignedKeyName = "skn"; |
| | 20 | | internal const string SignedExpiry = "se"; |
| | 21 | | internal const int MaxKeyNameLength = 256; |
| | 22 | | internal const int MaxKeyLength = 256; |
| | 23 | |
|
| | 24 | | const string SignedResourceFullFieldName = SharedAccessSignature + " " + SignedResource; |
| | 25 | | const string SasPairSeparator = "&"; |
| | 26 | | const string SasKeyValueSeparator = "="; |
| | 27 | |
|
| 0 | 28 | | static readonly Func<string, string> Decoder = WebUtility.UrlDecode; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Creates a new instance of the <see cref="SharedAccessSignatureToken"/> class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="tokenString">The token</param> |
| | 34 | | public SharedAccessSignatureToken(string tokenString) |
| 0 | 35 | | : base(tokenString, GetExpirationDateTimeUtcFromToken(tokenString), GetAudienceFromToken(tokenString), Const |
| | 36 | | { |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | internal static void Validate(string sharedAccessSignature) |
| | 40 | | { |
| 0 | 41 | | if (string.IsNullOrEmpty(sharedAccessSignature)) |
| | 42 | | { |
| 0 | 43 | | throw new ArgumentNullException(nameof(sharedAccessSignature)); |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | IDictionary<string, string> parsedFields = ExtractFieldValues(sharedAccessSignature); |
| | 47 | |
|
| | 48 | | string signature; |
| 0 | 49 | | if (!parsedFields.TryGetValue(Signature, out signature)) |
| | 50 | | { |
| 0 | 51 | | throw new ArgumentNullException(Signature); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | string expiry; |
| 0 | 55 | | if (!parsedFields.TryGetValue(SignedExpiry, out expiry)) |
| | 56 | | { |
| 0 | 57 | | throw new ArgumentNullException(SignedExpiry); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | string keyName; |
| 0 | 61 | | if (!parsedFields.TryGetValue(SignedKeyName, out keyName)) |
| | 62 | | { |
| 0 | 63 | | throw new ArgumentNullException(SignedKeyName); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | string encodedAudience; |
| 0 | 67 | | if (!parsedFields.TryGetValue(SignedResource, out encodedAudience)) |
| | 68 | | { |
| 0 | 69 | | throw new ArgumentNullException(SignedResource); |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | static IDictionary<string, string> ExtractFieldValues(string sharedAccessSignature) |
| | 74 | | { |
| 0 | 75 | | string[] tokenLines = sharedAccessSignature.Split(); |
| | 76 | |
|
| 0 | 77 | | if (!string.Equals(tokenLines[0].Trim(), SharedAccessSignature, StringComparison.OrdinalIgnoreCase) || token |
| | 78 | | { |
| 0 | 79 | | throw new ArgumentNullException(nameof(sharedAccessSignature)); |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | IDictionary<string, string> parsedFields = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 0 | 83 | | string[] tokenFields = tokenLines[1].Trim().Split(new[] { SasPairSeparator }, StringSplitOptions.None); |
| | 84 | |
|
| 0 | 85 | | foreach (string tokenField in tokenFields) |
| | 86 | | { |
| 0 | 87 | | if (tokenField != string.Empty) |
| | 88 | | { |
| 0 | 89 | | string[] fieldParts = tokenField.Split(new[] { SasKeyValueSeparator }, StringSplitOptions.None); |
| 0 | 90 | | if (string.Equals(fieldParts[0], SignedResource, StringComparison.OrdinalIgnoreCase)) |
| | 91 | | { |
| | 92 | | // We need to preserve the casing of the escape characters in the audience, |
| | 93 | | // so defer decoding the URL until later. |
| 0 | 94 | | parsedFields.Add(fieldParts[0], fieldParts[1]); |
| | 95 | | } |
| | 96 | | else |
| | 97 | | { |
| 0 | 98 | | parsedFields.Add(fieldParts[0], WebUtility.UrlDecode(fieldParts[1])); |
| | 99 | | } |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | return parsedFields; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | static string GetAudienceFromToken(string token) |
| | 107 | | { |
| 0 | 108 | | IDictionary<string, string> decodedToken = Decode(token, Decoder, Decoder, SasKeyValueSeparator, SasPairSepa |
| 0 | 109 | | if (!decodedToken.TryGetValue(SignedResourceFullFieldName, out var audience)) |
| | 110 | | { |
| 0 | 111 | | throw new FormatException(Resources.TokenMissingAudience); |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | return audience; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | static DateTime GetExpirationDateTimeUtcFromToken(string token) |
| | 118 | | { |
| 0 | 119 | | IDictionary<string, string> decodedToken = Decode(token, Decoder, Decoder, SasKeyValueSeparator, SasPairSepa |
| 0 | 120 | | if (!decodedToken.TryGetValue(SignedExpiry, out var expiresIn)) |
| | 121 | | { |
| 0 | 122 | | throw new FormatException(Resources.TokenMissingExpiresOn); |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | var expiresOn = (Constants.EpochTime + TimeSpan.FromSeconds(double.Parse(expiresIn, CultureInfo.InvariantCul |
| | 126 | |
|
| 0 | 127 | | return expiresOn; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | static IDictionary<string, string> Decode(string encodedString, Func<string, string> keyDecoder, Func<string, st |
| | 131 | | { |
| 0 | 132 | | IDictionary<string, string> dictionary = new Dictionary<string, string>(); |
| 0 | 133 | | IEnumerable<string> valueEncodedPairs = encodedString.Split(new[] { pairSeparator }, StringSplitOptions.None |
| 0 | 134 | | foreach (string valueEncodedPair in valueEncodedPairs) |
| | 135 | | { |
| 0 | 136 | | string[] pair = valueEncodedPair.Split(new[] { keyValueSeparator }, StringSplitOptions.None); |
| 0 | 137 | | if (pair.Length != 2) |
| | 138 | | { |
| 0 | 139 | | throw new FormatException(Resources.InvalidEncoding); |
| | 140 | | } |
| | 141 | |
|
| 0 | 142 | | dictionary.Add(keyDecoder(pair[0]), valueDecoder(pair[1])); |
| | 143 | | } |
| | 144 | |
|
| 0 | 145 | | return dictionary; |
| | 146 | | } |
| | 147 | | } |
| | 148 | | } |