-
Notifications
You must be signed in to change notification settings - Fork 70
/
CanonicalizationHelpers.cs
201 lines (170 loc) · 9.03 KB
/
CanonicalizationHelpers.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
// Copyright (c) Barry Dorrans. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
namespace idunno.Authentication.SharedKey
{
internal static class CanonicalizationHelpers
{
public static string CanonicalizeHeaders(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.Headers == null)
{
throw new NullReferenceException("Request has no headers.");
}
var canonicalizedHeaderBuilder = new CanonicalizedStringBuilder();
canonicalizedHeaderBuilder.Append(request.Method.ToString().ToUpperInvariant());
if (request.Content == null || request.Content.Headers == null)
{
canonicalizedHeaderBuilder.Append(string.Empty); // Encoding
canonicalizedHeaderBuilder.Append(string.Empty); // Language
canonicalizedHeaderBuilder.Append(0); // Length
canonicalizedHeaderBuilder.Append(string.Empty); // MD5
canonicalizedHeaderBuilder.Append(string.Empty); // Content-Type
}
else
{
canonicalizedHeaderBuilder.Append(request.Content.Headers?.ContentEncoding);
canonicalizedHeaderBuilder.Append(request.Content.Headers?.ContentLanguage);
canonicalizedHeaderBuilder.Append(request.Content.Headers == null ? "0" :
request.Content.Headers.ContentLength == null ? "0" : ((long)request.Content.Headers.ContentLength).ToString(CultureInfo.InvariantCulture));
canonicalizedHeaderBuilder.Append(request.Content.Headers == null ? string.Empty :
request.Content.Headers.ContentMD5 == null ? string.Empty : Convert.ToBase64String(request.Content.Headers.ContentMD5));
canonicalizedHeaderBuilder.Append(request.Content.Headers?.ContentType);
}
canonicalizedHeaderBuilder.Append(request.Headers.Date.HasValue ? request.Headers.Date.Value.ToString("R", CultureInfo.InvariantCulture) : null);
canonicalizedHeaderBuilder.Append(request.Headers.IfModifiedSince.HasValue ? request.Headers.IfModifiedSince.Value.ToString("R", CultureInfo.InvariantCulture) : null);
canonicalizedHeaderBuilder.Append(request.Headers.IfMatch);
canonicalizedHeaderBuilder.Append(request.Headers.IfNoneMatch);
canonicalizedHeaderBuilder.Append(request.Headers.IfUnmodifiedSince.HasValue ? request.Headers.IfUnmodifiedSince.Value.ToString("R", CultureInfo.InvariantCulture) : null);
canonicalizedHeaderBuilder.Append(request.Headers.Range);
return canonicalizedHeaderBuilder.ToString();
}
public static string CanonicalizeResource(this HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.RequestUri == null)
{
throw new ArgumentException("request has no URI.");
}
if (request.RequestUri.AbsolutePath == null)
{
throw new ArgumentException("RequestURI has no absolute path.");
}
var canonicalizedResourceBuilder = new StringBuilder();
canonicalizedResourceBuilder.Append(request.RequestUri.AbsolutePath);
if (request.RequestUri.Query.Length > 0 )
{
// We have query parameters
canonicalizedResourceBuilder.Append(CanonicalizeQueryString(request.RequestUri.Query));
}
return canonicalizedResourceBuilder.ToString();
}
public static string CanonicalizeHeaders(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.GetTypedHeaders().Date == null)
{
throw new ArgumentException("Request has no date header.");
}
var canonicalizedHeaderBuilder = new CanonicalizedStringBuilder();
canonicalizedHeaderBuilder.Append(request.Method.ToString().ToUpperInvariant());
if (request.Headers == null)
{
canonicalizedHeaderBuilder.Append(string.Empty); // Encoding
canonicalizedHeaderBuilder.Append(string.Empty); // Language
canonicalizedHeaderBuilder.Append(0); // Length
canonicalizedHeaderBuilder.Append(string.Empty); // MD5
canonicalizedHeaderBuilder.Append(string.Empty); // Content-Type
}
else
{
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.ContentEncoding].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.ContentLanguage].ToString());
canonicalizedHeaderBuilder.Append(request.ContentLength == null ? "0" : ((long)request.ContentLength).ToString(CultureInfo.InvariantCulture));
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.ContentMD5].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.ContentType].ToString());
}
canonicalizedHeaderBuilder.Append(request.GetTypedHeaders().Date.HasValue ? request.GetTypedHeaders().Date!.Value.ToString("R", CultureInfo.InvariantCulture) : null);
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.IfModifiedSince].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.IfMatch].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.IfNoneMatch].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.IfUnmodifiedSince].ToString());
canonicalizedHeaderBuilder.Append(request.Headers?[HeaderNames.Range].ToString());
return canonicalizedHeaderBuilder.ToString();
}
public static string CanonicalizeResource(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var canonicalizedResourceBuilder = new StringBuilder();
canonicalizedResourceBuilder.Append(request.Path);
if (request.QueryString.Value != null && request.Query.Any())
{
canonicalizedResourceBuilder.Append(CanonicalizeQueryString(request.QueryString.Value));
}
return canonicalizedResourceBuilder.ToString();
}
private static string CanonicalizeQueryString(string queryString)
{
var queryStringBuilder = new StringBuilder();
NameValueCollection queryNameValueCollection = HttpUtility.ParseQueryString(queryString);
var sortedQueryNameValueList = new SortedList<string, string>(queryNameValueCollection.AllKeys.ToDictionary(k => k ?? string.Empty, k => queryNameValueCollection[k] ?? string.Empty));
foreach (var keyValuePair in sortedQueryNameValueList)
{
queryStringBuilder.Append('\n');
queryStringBuilder.Append(keyValuePair.Key.ToLowerInvariant());
queryStringBuilder.Append(':');
if (keyValuePair.Value == null || !keyValuePair.Value.Contains(',', StringComparison.InvariantCulture))
{
queryStringBuilder.Append(keyValuePair.Value);
}
else
{
// We have multiple values, all nicely separated by commas, but they're not sorted.
var values = new List<string?>(keyValuePair.Value.Split(',', StringSplitOptions.None));
values.Sort(new InvariantComparer());
queryStringBuilder.Append(string.Join(",", values));
}
}
return queryStringBuilder.ToString();
}
}
internal class InvariantComparer : IComparer<string?>
{
internal static readonly InvariantComparer Default = new InvariantComparer();
private readonly CompareInfo _compareInfo;
internal InvariantComparer()
{
_compareInfo = CultureInfo.InvariantCulture.CompareInfo;
}
public int Compare(string? a, string? b)
{
if (a is string sa && b is string sb)
return _compareInfo.Compare(sa, sb);
else
return Comparer.Default.Compare(a, b);
}
}
}