|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | + |
| 7 | +#nullable enable |
| 8 | + |
| 9 | +namespace Azure.Core |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A helper class for parsing Authorization challenge headers. |
| 13 | + /// </summary> |
| 14 | + internal static class AuthorizationChallengeParser |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Parses the specified parameter from a challenge hearder found in the specified <see cref="Response"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="response">The <see cref="Response"/> to parse.</param> |
| 20 | + /// <param name="challengeScheme">The challenge scheme containing the <paramref name="challengeParameter"/>. For example: "Bearer"</param> |
| 21 | + /// <param name="challengeParameter">The parameter key name containing the value to return.</param> |
| 22 | + /// <returns>The value of the parameter name specified in <paramref name="challengeParameter"/> if it is found in the specified <paramref name="challengeScheme"/>.</returns> |
| 23 | + public static string? GetChallengeParameterFromResponse(Response response, string challengeScheme, string challengeParameter) |
| 24 | + { |
| 25 | + if (response.Status != (int)HttpStatusCode.Unauthorized || !response.Headers.TryGetValue(HttpHeader.Names.WWWAuthenticate, out string? headerValue)) |
| 26 | + { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + ReadOnlySpan<char> bearer = challengeScheme.AsSpan(); |
| 31 | + ReadOnlySpan<char> claims = challengeParameter.AsSpan(); |
| 32 | + ReadOnlySpan<char> headerSpan = headerValue.AsSpan(); |
| 33 | + |
| 34 | + // Iterate through each challenge value. |
| 35 | + while (TryGetNextChallenge(ref headerSpan, out var challengeKey)) |
| 36 | + { |
| 37 | + // Enumerate each key=value parameter until we find the 'claims' key on the 'Bearer' challenge. |
| 38 | + while (TryGetNextParameter(ref headerSpan, out var key, out var value)) |
| 39 | + { |
| 40 | + if (challengeKey.Equals(bearer, StringComparison.OrdinalIgnoreCase) && key.Equals(claims, StringComparison.OrdinalIgnoreCase)) |
| 41 | + { |
| 42 | + return value.ToString(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Iterates through the challenge schemes present in a challenge header. |
| 52 | + /// </summary> |
| 53 | + /// <param name="headerValue"> |
| 54 | + /// The header value which will be sliced to remove the first parsed <paramref name="challengeKey"/>. |
| 55 | + /// </param> |
| 56 | + /// <param name="challengeKey">The parsed challenge scheme.</param> |
| 57 | + /// <returns> |
| 58 | + /// <c>true</c> if a challenge scheme was successfully parsed. |
| 59 | + /// The value of <paramref name="headerValue"/> should be passed to <see cref="TryGetNextParameter"/> to parse the challenge parameters if <c>true</c>. |
| 60 | + /// </returns> |
| 61 | + internal static bool TryGetNextChallenge(ref ReadOnlySpan<char> headerValue, out ReadOnlySpan<char> challengeKey) |
| 62 | + { |
| 63 | + challengeKey = default; |
| 64 | + |
| 65 | + headerValue = headerValue.TrimStart(' '); |
| 66 | + int endOfChallengeKey = headerValue.IndexOf(' '); |
| 67 | + |
| 68 | + if (endOfChallengeKey < 0) |
| 69 | + { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + challengeKey = headerValue.Slice(0, endOfChallengeKey); |
| 74 | + |
| 75 | + // Slice the challenge key from the headerValue |
| 76 | + headerValue = headerValue.Slice(endOfChallengeKey + 1); |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Iterates through a challenge header value after being parsed by <see cref="TryGetNextChallenge"/>. |
| 83 | + /// </summary> |
| 84 | + /// <param name="headerValue">The header value after being parsed by <see cref="TryGetNextChallenge"/>.</param> |
| 85 | + /// <param name="paramKey">The parsed challenge parameter key.</param> |
| 86 | + /// <param name="paramValue">The parsed challenge parameter value.</param> |
| 87 | + /// <param name="separator">The challenge parameter key / value pair separator. The default is '='.</param> |
| 88 | + /// <returns> |
| 89 | + /// <c>true</c> if the next available challenge parameter was successfully parsed. |
| 90 | + /// <c>false</c> if there are no more parameters for the current challenge scheme or an additional challenge scheme was encountered in the <paramref name="headerValue"/>. |
| 91 | + /// The value of <paramref name="headerValue"/> should be passed again to <see cref="TryGetNextChallenge"/> to attempt to parse any additional challenge schemes if <c>false</c>. |
| 92 | + /// </returns> |
| 93 | + internal static bool TryGetNextParameter(ref ReadOnlySpan<char> headerValue, out ReadOnlySpan<char> paramKey, out ReadOnlySpan<char> paramValue, char separator = '=') |
| 94 | + { |
| 95 | + paramKey = default; |
| 96 | + paramValue = default; |
| 97 | + var spaceOrComma = " ,".AsSpan(); |
| 98 | + |
| 99 | + // Trim any separater prefixes. |
| 100 | + headerValue = headerValue.TrimStart(spaceOrComma); |
| 101 | + |
| 102 | + int nextSpace = headerValue.IndexOf(' '); |
| 103 | + int nextSeparator = headerValue.IndexOf(separator); |
| 104 | + |
| 105 | + if (nextSpace < nextSeparator && nextSpace != -1) |
| 106 | + { |
| 107 | + // we encountered another challenge value. |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (nextSeparator < 0) |
| 112 | + return false; |
| 113 | + |
| 114 | + // Get the paramKey. |
| 115 | + paramKey = headerValue.Slice(0, nextSeparator).Trim(); |
| 116 | + |
| 117 | + // Slice to remove the 'paramKey=' from the parameters. |
| 118 | + headerValue = headerValue.Slice(nextSeparator + 1); |
| 119 | + |
| 120 | + // The start of paramValue will usually be a quoted string. Find the first quote. |
| 121 | + int quoteIndex = headerValue.IndexOf('\"'); |
| 122 | + |
| 123 | + // Get the paramValue, which is delimited by the trailing quote. |
| 124 | + headerValue = headerValue.Slice(quoteIndex + 1); |
| 125 | + if (quoteIndex >= 0) |
| 126 | + { |
| 127 | + // The values are quote wrapped |
| 128 | + paramValue = headerValue.Slice(0, headerValue.IndexOf('\"')); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + //the values are not quote wrapped (storage is one example of this) |
| 133 | + // either find the next space indicating the delimiter to the next value, or go to the end since this is the last value. |
| 134 | + int trailingDelimiterIndex = headerValue.IndexOfAny(spaceOrComma); |
| 135 | + if (trailingDelimiterIndex >= 0) |
| 136 | + { |
| 137 | + paramValue = headerValue.Slice(0, trailingDelimiterIndex); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + paramValue = headerValue; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Slice to remove the '"paramValue"' from the parameters. |
| 146 | + if (headerValue != paramValue) |
| 147 | + headerValue = headerValue.Slice(paramValue.Length + 1); |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments