-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Signer.cs
171 lines (143 loc) · 6.98 KB
/
Signer.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.Util;
namespace AwsSignatureVersion4.Private
{
public static class Signer
{
/// <remarks>This method has a synchronous alternative.</remarks>
public static async Task<Result> SignAsync(
HttpRequestMessage request,
Uri? baseAddress,
IEnumerable<KeyValuePair<string, IEnumerable<string>>> defaultRequestHeaders,
DateTime now,
string regionName,
string serviceName,
ImmutableCredentials credentials)
{
ValidateArguments(request, regionName, serviceName, credentials);
UpdateRequestUri(request, baseAddress);
var contentHash = await ContentHash.CalculateAsync(request.Content).ConfigureAwait(false);
AddHeaders(request, now, serviceName, credentials, contentHash);
var (canonicalRequest, signedHeaders) = CanonicalRequest.Build(
serviceName,
request,
defaultRequestHeaders,
contentHash);
var (stringToSign, credentialScope) = StringToSign.Build(
now,
regionName,
serviceName,
canonicalRequest);
var authorizationHeader = AuthorizationHeader.Build(
now,
regionName,
serviceName,
credentials,
signedHeaders,
credentialScope,
stringToSign);
// Add the authorization header
request.Headers.TryAddWithoutValidation(HeaderKeys.AuthorizationHeader, authorizationHeader);
return new Result(canonicalRequest, stringToSign, authorizationHeader);
}
#if NET5_0_OR_GREATER
/// <remarks>This method has a asynchronous alternative.</remarks>
public static Result Sign(
HttpRequestMessage request,
Uri? baseAddress,
IEnumerable<KeyValuePair<string, IEnumerable<string>>> defaultRequestHeaders,
DateTime now,
string regionName,
string serviceName,
ImmutableCredentials credentials)
{
ValidateArguments(request, regionName, serviceName, credentials);
UpdateRequestUri(request, baseAddress);
var contentHash = ContentHash.Calculate(request.Content);
AddHeaders(request, now, serviceName, credentials, contentHash);
var (canonicalRequest, signedHeaders) = CanonicalRequest.Build(
serviceName,
request,
defaultRequestHeaders,
contentHash);
var (stringToSign, credentialScope) = StringToSign.Build(
now,
regionName,
serviceName,
canonicalRequest);
var authorizationHeader = AuthorizationHeader.Build(
now,
regionName,
serviceName,
credentials,
signedHeaders,
credentialScope,
stringToSign);
// Add the authorization header
request.Headers.TryAddWithoutValidation(HeaderKeys.AuthorizationHeader, authorizationHeader);
return new Result(canonicalRequest, stringToSign, authorizationHeader);
}
#endif
private static void ValidateArguments(
HttpRequestMessage request,
string regionName,
string serviceName,
ImmutableCredentials credentials)
{
if (request == null) throw new ArgumentNullException(nameof(request));
if (request.Headers.Contains(HeaderKeys.XAmzDateHeader)) throw new ArgumentException(ErrorMessages.XAmzDateHeaderExists, nameof(request));
if (request.Headers.Authorization != null) throw new ArgumentException(ErrorMessages.AuthorizationHeaderExists, nameof(request));
if (request.Headers.Contains(HeaderKeys.AuthorizationHeader)) throw new ArgumentException(ErrorMessages.AuthorizationHeaderExists, nameof(request));
if (regionName == null) throw new ArgumentNullException(nameof(regionName));
if (regionName == string.Empty) throw new ArgumentException(ErrorMessages.InvalidRegionName, nameof(regionName));
if (serviceName == null) throw new ArgumentNullException(nameof(serviceName));
if (serviceName == string.Empty) throw new ArgumentException(ErrorMessages.InvalidServiceName, nameof(serviceName));
if (serviceName == ServiceName.S3 && request.Method == PatchAsyncExtensions.PatchMethod) throw new NotSupportedException(ErrorMessages.S3DoesNotSupportPatch);
if (serviceName == ServiceName.S3 && request.Method == HttpMethod.Post) throw new NotSupportedException(ErrorMessages.S3DoesNotSupportPost);
if (credentials == null) throw new ArgumentNullException(nameof(credentials));
}
private static void AddHeaders(
HttpRequestMessage request,
DateTime now,
string serviceName,
ImmutableCredentials credentials,
string contentHash)
{
// Add required headers
request.AddHeader(HeaderKeys.XAmzDateHeader, now.ToIso8601BasicDateTime());
// Add conditional headers
request.AddHeaderIf(credentials.UseToken, HeaderKeys.XAmzSecurityTokenHeader, credentials.Token);
request.AddHeaderIf(!request.Headers.Contains(HeaderKeys.HostHeader), HeaderKeys.HostHeader, request.RequestUri!.Host);
request.AddHeaderIf(serviceName == ServiceName.OpenSearchServerless, HeaderKeys.XAmzContentSha256Header, contentHash);
request.AddHeaderIf(serviceName == ServiceName.S3, HeaderKeys.XAmzContentSha256Header, contentHash);
}
private static void UpdateRequestUri(HttpRequestMessage request, Uri? baseAddress)
{
if (request.RequestUri == null && baseAddress == null) throw new InvalidOperationException(ErrorMessages.InvalidRequestUri);
Uri? requestUri = null;
if (request.RequestUri == null)
{
requestUri = baseAddress;
}
else
{
// If the request Uri is an absolute Uri, just use it. Otherwise try to combine it
// with the base Uri.
if (!request.RequestUri.IsAbsoluteUri)
{
if (baseAddress == null) throw new InvalidOperationException(ErrorMessages.InvalidRequestUri);
requestUri = new Uri(baseAddress, request.RequestUri);
}
}
// We modified the original request Uri. Assign the new Uri to the request message.
if (requestUri != null)
{
request.RequestUri = requestUri;
}
}
}
}