/
OAuthWorkflow.cs
243 lines (189 loc) · 9.45 KB
/
OAuthWorkflow.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
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Linq;
using System.Web;
using RestSharp.Authenticators.OAuth.Extensions;
using RestSharp.Extensions;
using RestSharp.Validation;
namespace RestSharp.Authenticators.OAuth
{
/// <summary>
/// A class to encapsulate OAuth authentication flow.
/// </summary>
sealed class OAuthWorkflow
{
public string Version { get; set; }
public string ConsumerKey { get; set; }
public string? ConsumerSecret { get; set; }
public string Token { get; set; }
public string TokenSecret { get; set; }
public string CallbackUrl { get; set; }
public string Verifier { get; set; }
public string SessionHandle { get; set; }
public OAuthSignatureMethod SignatureMethod { get; set; }
public OAuthSignatureTreatment SignatureTreatment { get; set; }
public OAuthParameterHandling ParameterHandling { get; set; }
public string ClientUsername { get; set; }
public string ClientPassword { get; set; }
public string RequestTokenUrl { get; set; }
public string AccessTokenUrl { get; set; }
/// <summary>
/// Generates an OAuth signature to pass to an
/// <see cref="IAuthenticator" /> for the purpose of requesting an
/// unauthorized request token.
/// </summary>
/// <param name="method">The HTTP method for the intended request</param>
/// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
/// <returns></returns>
public OAuthParameters BuildRequestTokenInfo(string method, WebPairCollection parameters)
{
ValidateTokenRequestState();
var allParameters = new WebPairCollection();
allParameters.AddRange(parameters);
var timestamp = OAuthTools.GetTimestamp();
var nonce = OAuthTools.GetNonce();
var authParameters = GenerateAuthParameters(timestamp, nonce);
allParameters.AddRange(authParameters);
var signatureBase = OAuthTools.ConcatenateRequestElements(method, RequestTokenUrl, allParameters);
return new OAuthParameters
{
Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret),
Parameters = authParameters
};
}
/// <summary>
/// Generates an OAuth signature to pass to the
/// <see cref="IAuthenticator" /> for the purpose of exchanging a request token
/// for an access token authorized by the user at the Service Provider site.
/// </summary>
/// <param name="method">The HTTP method for the intended request</param>
/// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
public OAuthParameters BuildAccessTokenSignature(string method, WebPairCollection parameters)
{
ValidateAccessRequestState();
var allParameters = new WebPairCollection();
allParameters.AddRange(parameters);
var uri = new Uri(AccessTokenUrl);
var timestamp = OAuthTools.GetTimestamp();
var nonce = OAuthTools.GetNonce();
var authParameters = GenerateAuthParameters(timestamp, nonce);
allParameters.AddRange(authParameters);
var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), allParameters);
return new OAuthParameters
{
Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret),
Parameters = authParameters
};
}
/// <summary>
/// Generates an OAuth signature to pass to an
/// <see cref="IAuthenticator" /> for the purpose of exchanging user credentials
/// for an access token authorized by the user at the Service Provider site.
/// </summary>
/// <param name="method">The HTTP method for the intended request</param>
/// <param name="parameters">Any existing, non-OAuth query parameters desired in the request</param>
public OAuthParameters BuildClientAuthAccessTokenSignature(string method, WebPairCollection parameters)
{
ValidateClientAuthAccessRequestState();
var allParameters = new WebPairCollection();
allParameters.AddRange(parameters);
var uri = new Uri(AccessTokenUrl);
var timestamp = OAuthTools.GetTimestamp();
var nonce = OAuthTools.GetNonce();
var authParameters = GenerateXAuthParameters(timestamp, nonce);
allParameters.AddRange(authParameters);
var signatureBase = OAuthTools.ConcatenateRequestElements(method, uri.ToString(), allParameters);
return new OAuthParameters
{
Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret),
Parameters = authParameters
};
}
public OAuthParameters BuildProtectedResourceSignature(string method, WebPairCollection parameters, string url)
{
ValidateProtectedResourceState();
var allParameters = new WebPairCollection();
allParameters.AddRange(parameters);
// Include url parameters in query pool
var uri = new Uri(url);
var urlParameters = HttpUtility.ParseQueryString(uri.Query);
allParameters.AddRange(urlParameters.AllKeys.Select(x => new WebPair(x, urlParameters[x])));
var timestamp = OAuthTools.GetTimestamp();
var nonce = OAuthTools.GetNonce();
var authParameters = GenerateAuthParameters(timestamp, nonce);
allParameters.AddRange(authParameters);
var signatureBase = OAuthTools.ConcatenateRequestElements(method, url, allParameters);
return new OAuthParameters
{
Signature = OAuthTools.GetSignature(SignatureMethod, SignatureTreatment, signatureBase, ConsumerSecret, TokenSecret),
Parameters = authParameters
};
}
void ValidateTokenRequestState()
{
Ensure.NotEmpty(RequestTokenUrl, nameof(RequestTokenUrl));
Ensure.NotEmpty(ConsumerKey, nameof(ConsumerKey));
}
void ValidateAccessRequestState()
{
Ensure.NotEmpty(AccessTokenUrl, nameof(AccessTokenUrl));
Ensure.NotEmpty(ConsumerKey, nameof(ConsumerKey));
Ensure.NotEmpty(Token, nameof(Token));
}
void ValidateClientAuthAccessRequestState()
{
Ensure.NotEmpty(AccessTokenUrl, nameof(AccessTokenUrl));
Ensure.NotEmpty(ConsumerKey, nameof(ConsumerKey));
Ensure.NotEmpty(ClientUsername, nameof(ClientUsername));
}
void ValidateProtectedResourceState()
{
Ensure.NotEmpty(ConsumerKey, nameof(ConsumerKey));
}
WebPairCollection GenerateAuthParameters(string timestamp, string nonce)
{
var authParameters = new WebPairCollection
{
new WebPair("oauth_consumer_key", ConsumerKey),
new WebPair("oauth_nonce", nonce),
new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()),
new WebPair("oauth_timestamp", timestamp),
new WebPair("oauth_version", Version ?? "1.0")
};
if (!Token.IsEmpty()) authParameters.Add(new WebPair("oauth_token", Token, true));
if (!CallbackUrl.IsEmpty()) authParameters.Add(new WebPair("oauth_callback", CallbackUrl, true));
if (!Verifier.IsEmpty()) authParameters.Add(new WebPair("oauth_verifier", Verifier));
if (!SessionHandle.IsEmpty()) authParameters.Add(new WebPair("oauth_session_handle", SessionHandle));
return authParameters;
}
WebPairCollection GenerateXAuthParameters(string timestamp, string nonce)
=> new WebPairCollection
{
new WebPair("x_auth_username", ClientUsername),
new WebPair("x_auth_password", ClientPassword),
new WebPair("x_auth_mode", "client_auth"),
new WebPair("oauth_consumer_key", ConsumerKey),
new WebPair("oauth_signature_method", SignatureMethod.ToRequestValue()),
new WebPair("oauth_timestamp", timestamp),
new WebPair("oauth_nonce", nonce),
new WebPair("oauth_version", Version ?? "1.0")
};
internal class OAuthParameters
{
public WebPairCollection Parameters { get; set; }
public string Signature { get; set; }
}
}
}