/
AuthenticationHeaderValue.cs
296 lines (251 loc) · 11.8 KB
/
AuthenticationHeaderValue.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Net.Http.Headers
{
public class AuthenticationHeaderValue : ICloneable
{
private readonly string _scheme;
private readonly string? _parameter;
public string Scheme
{
get { return _scheme; }
}
// We simplify parameters by just considering them one string. The caller is responsible for correctly parsing
// the string.
// The reason is that we can't determine the format of parameters. According to Errata 1959 in RFC 2617
// parameters can be "token", "quoted-string", or "#auth-param" where "auth-param" is defined as
// "token "=" ( token | quoted-string )". E.g. take the following BASIC example:
// Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
// Due to Base64 encoding we have two final "=". The value is neither a token nor a quoted-string, so it must
// be an auth-param according to the RFC definition. But that's also incorrect: auth-param means that we
// consider the value before the first "=" as "name" and the final "=" as "value".
public string? Parameter
{
get { return _parameter; }
}
public AuthenticationHeaderValue(string scheme)
: this(scheme, null)
{
}
public AuthenticationHeaderValue(string scheme, string? parameter)
{
HeaderUtilities.CheckValidToken(scheme, nameof(scheme));
HttpHeaders.CheckContainsNewLine(parameter);
_scheme = scheme;
_parameter = parameter;
}
private AuthenticationHeaderValue(AuthenticationHeaderValue source)
{
Debug.Assert(source != null);
_scheme = source._scheme;
_parameter = source._parameter;
}
public override string ToString()
{
if (string.IsNullOrEmpty(_parameter))
{
return _scheme;
}
return _scheme + " " + _parameter;
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
AuthenticationHeaderValue? other = obj as AuthenticationHeaderValue;
if (other == null)
{
return false;
}
if (string.IsNullOrEmpty(_parameter) && string.IsNullOrEmpty(other._parameter))
{
return (string.Equals(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase));
}
else
{
// Since we can't parse the parameter, we use case-sensitive comparison.
return string.Equals(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase) &&
string.Equals(_parameter, other._parameter, StringComparison.Ordinal);
}
}
public override int GetHashCode()
{
int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_scheme);
if (!string.IsNullOrEmpty(_parameter))
{
result ^= _parameter.GetHashCode();
}
return result;
}
public static AuthenticationHeaderValue Parse(string? input)
{
int index = 0;
return (AuthenticationHeaderValue)GenericHeaderParser.SingleValueAuthenticationParser.ParseValue(
input, null, ref index);
}
public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out AuthenticationHeaderValue? parsedValue)
{
int index = 0;
parsedValue = null;
if (GenericHeaderParser.SingleValueAuthenticationParser.TryParseValue(input, null, ref index, out object? output))
{
parsedValue = (AuthenticationHeaderValue)output!;
return true;
}
return false;
}
internal static int GetAuthenticationLength(string? input, int startIndex, out object? parsedValue)
{
Debug.Assert(startIndex >= 0);
parsedValue = null;
if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
{
return 0;
}
// Parse the scheme string: <scheme> in '<scheme> <parameter>'
int schemeLength = HttpRuleParser.GetTokenLength(input, startIndex);
if (schemeLength == 0)
{
return 0;
}
string? targetScheme = null;
switch (schemeLength)
{
// Avoid allocating a scheme string for the most common cases.
case 5: targetScheme = "Basic"; break;
case 6: targetScheme = "Digest"; break;
case 4: targetScheme = "NTLM"; break;
case 9: targetScheme = "Negotiate"; break;
}
string scheme = targetScheme != null && string.CompareOrdinal(input, startIndex, targetScheme, 0, schemeLength) == 0 ?
targetScheme :
input.Substring(startIndex, schemeLength);
int current = startIndex + schemeLength;
int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
current += whitespaceLength;
if ((current == input.Length) || (input[current] == ','))
{
// If we only have a scheme followed by whitespace, we're done.
parsedValue = new AuthenticationHeaderValue(scheme);
return current - startIndex;
}
// We need at least one space between the scheme and parameters. If there is no whitespace, then we must
// have reached the end of the string (i.e. scheme-only string).
if (whitespaceLength == 0)
{
return 0;
}
// If we get here, we have a <scheme> followed by a whitespace. Now we expect the following:
// '<scheme> <blob>[,<name>=<value>]*[, <otherscheme>...]*': <blob> potentially contains one
// or more '=' characters, optionally followed by additional name/value pairs, optionally followed by
// other schemes. <blob> may be a quoted string.
// We look at the value after ',': if it is <token>=<value> then we have a parameter for <scheme>.
// If we have either a <token>-only or <token><whitespace><blob> then we have another scheme.
int parameterStartIndex = current;
int parameterEndIndex = current;
if (!TrySkipFirstBlob(input, ref current, ref parameterEndIndex))
{
return 0;
}
if (current < input.Length)
{
if (!TryGetParametersEndIndex(input, ref current, ref parameterEndIndex))
{
return 0;
}
}
string parameter = input.Substring(parameterStartIndex, parameterEndIndex - parameterStartIndex + 1);
parsedValue = new AuthenticationHeaderValue(scheme, parameter);
return current - startIndex;
}
private static bool TrySkipFirstBlob(string input, ref int current, ref int parameterEndIndex)
{
// Find the delimiter: Note that <blob> in "<scheme> <blob>" may be a token, quoted string, name/value
// pair or a Base64 encoded string. So make sure that we don't consider ',' characters within a quoted
// string as delimiter.
while ((current < input.Length) && (input[current] != ','))
{
if (input[current] == '"')
{
int quotedStringLength;
if (HttpRuleParser.GetQuotedStringLength(input, current, out quotedStringLength) !=
HttpParseResult.Parsed)
{
// We have a quote but an invalid quoted-string.
return false;
}
current += quotedStringLength;
parameterEndIndex = current - 1; // -1 because 'current' points to the char after the final '"'
}
else
{
int whitespaceLength = HttpRuleParser.GetWhitespaceLength(input, current);
// We don't want trailing whitespace to be considered part of the parameter blob. Increment
// 'parameterEndIndex' only if we don't have a whitespace. E.g. "Basic AbC= , NTLM" should return
// "AbC=" as parameter ignoring the spaces before ','.
if (whitespaceLength == 0)
{
parameterEndIndex = current;
current++;
}
else
{
current += whitespaceLength;
}
}
}
return true;
}
private static bool TryGetParametersEndIndex(string input, ref int parseEndIndex, ref int parameterEndIndex)
{
Debug.Assert(parseEndIndex < input.Length, "Expected string to have at least 1 char");
Debug.Assert(input[parseEndIndex] == ',');
int current = parseEndIndex;
do
{
current++; // skip ',' delimiter
current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(input, current, true, out _);
if (current == input.Length)
{
return true;
}
// Now we have to determine if after ',' we have a list of <name>=<value> pairs that are part of
// the auth scheme parameters OR if we have another auth scheme. Either way, after ',' we expect a
// valid token that is either the <name> in a <name>=<value> pair OR <scheme> of another scheme.
int tokenLength = HttpRuleParser.GetTokenLength(input, current);
if (tokenLength == 0)
{
return false;
}
current += tokenLength;
current += HttpRuleParser.GetWhitespaceLength(input, current);
// If we reached the end of the string or the token is followed by anything but '=', then the parsed
// token is another scheme name. The string representing parameters ends before the token (e.g.
// "Digest a=b, c=d, NTLM": return scheme "Digest" with parameters string "a=b, c=d").
if ((current == input.Length) || (input[current] != '='))
{
return true;
}
current++; // skip '=' delimiter
current += HttpRuleParser.GetWhitespaceLength(input, current);
int valueLength = NameValueHeaderValue.GetValueLength(input, current);
// After '<name>=' we expect a valid <value> (either token or quoted string)
if (valueLength == 0)
{
return false;
}
// Update parameter end index, since we just parsed a valid <name>=<value> pair that is part of the
// parameters string.
current += valueLength;
parameterEndIndex = current - 1; // -1 because 'current' already points to the char after <value>
current += HttpRuleParser.GetWhitespaceLength(input, current);
parseEndIndex = current; // this essentially points to parameterEndIndex + whitespace + next char
} while ((current < input.Length) && (input[current] == ','));
return true;
}
object ICloneable.Clone()
{
return new AuthenticationHeaderValue(this);
}
}
}