16
16
using System . Collections . Generic ;
17
17
using System . Web ;
18
18
using RestSharp . Authenticators . OAuth . Extensions ;
19
+ using RestSharp . Extensions ;
20
+ using RestSharp . Validation ;
19
21
20
22
namespace RestSharp . Authenticators . OAuth
21
23
{
22
24
/// <summary>
23
25
/// A class to encapsulate OAuth authentication flow.
24
26
/// </summary>
25
- internal sealed class OAuthWorkflow
27
+ sealed class OAuthWorkflow
26
28
{
27
29
public string Version { get ; set ; }
28
30
@@ -66,8 +68,7 @@ public string BuildRequestTokenInfo(string method, WebPairCollection parameters)
66
68
{
67
69
ValidateTokenRequestState ( ) ;
68
70
69
- if ( parameters == null )
70
- parameters = new WebPairCollection ( ) ;
71
+ parameters ??= new WebPairCollection ( ) ;
71
72
72
73
var timestamp = OAuthTools . GetTimestamp ( ) ;
73
74
var nonce = OAuthTools . GetNonce ( ) ;
@@ -89,8 +90,7 @@ public string BuildAccessTokenSignature(string method, WebPairCollection paramet
89
90
{
90
91
ValidateAccessRequestState ( ) ;
91
92
92
- if ( parameters == null )
93
- parameters = new WebPairCollection ( ) ;
93
+ parameters ??= new WebPairCollection ( ) ;
94
94
95
95
var uri = new Uri ( AccessTokenUrl ) ;
96
96
var timestamp = OAuthTools . GetTimestamp ( ) ;
@@ -138,8 +138,7 @@ public string BuildProtectedResourceSignature(string method, WebPairCollection p
138
138
{
139
139
ValidateProtectedResourceState ( ) ;
140
140
141
- if ( parameters == null )
142
- parameters = new WebPairCollection ( ) ;
141
+ parameters ??= new WebPairCollection ( ) ;
143
142
144
143
// Include url parameters in query pool
145
144
var uri = new Uri ( url ) ;
@@ -163,53 +162,31 @@ public string BuildProtectedResourceSignature(string method, WebPairCollection p
163
162
164
163
void ValidateTokenRequestState ( )
165
164
{
166
- if ( RequestTokenUrl . IsNullOrBlank ( ) )
167
- throw new ArgumentException ( "You must specify a request token URL" ) ;
168
-
169
- if ( ConsumerKey . IsNullOrBlank ( ) )
170
- throw new ArgumentException ( "You must specify a consumer key" ) ;
171
-
172
- if ( ConsumerSecret . IsNullOrBlank ( ) )
173
- throw new ArgumentException ( "You must specify a consumer secret" ) ;
165
+ Ensure . NotEmpty ( RequestTokenUrl , nameof ( RequestTokenUrl ) ) ;
166
+ Ensure . NotEmpty ( ConsumerKey , nameof ( ConsumerKey ) ) ;
167
+ Ensure . NotEmpty ( ConsumerSecret , nameof ( ConsumerSecret ) ) ;
174
168
}
175
169
176
170
void ValidateAccessRequestState ( )
177
171
{
178
- if ( AccessTokenUrl . IsNullOrBlank ( ) )
179
- throw new ArgumentException ( "You must specify an access token URL" ) ;
180
-
181
- if ( ConsumerKey . IsNullOrBlank ( ) )
182
- throw new ArgumentException ( "You must specify a consumer key" ) ;
183
-
184
- if ( ConsumerSecret . IsNullOrBlank ( ) )
185
- throw new ArgumentException ( "You must specify a consumer secret" ) ;
186
-
187
- if ( Token . IsNullOrBlank ( ) )
188
- throw new ArgumentException ( "You must specify a token" ) ;
172
+ Ensure . NotEmpty ( AccessTokenUrl , nameof ( AccessTokenUrl ) ) ;
173
+ Ensure . NotEmpty ( ConsumerKey , nameof ( ConsumerKey ) ) ;
174
+ Ensure . NotEmpty ( ConsumerSecret , nameof ( ConsumerSecret ) ) ;
175
+ Ensure . NotEmpty ( Token , nameof ( Token ) ) ;
189
176
}
190
177
191
178
void ValidateClientAuthAccessRequestState ( )
192
179
{
193
- if ( AccessTokenUrl . IsNullOrBlank ( ) )
194
- throw new ArgumentException ( "You must specify an access token URL" ) ;
195
-
196
- if ( ConsumerKey . IsNullOrBlank ( ) )
197
- throw new ArgumentException ( "You must specify a consumer key" ) ;
198
-
199
- if ( ConsumerSecret . IsNullOrBlank ( ) )
200
- throw new ArgumentException ( "You must specify a consumer secret" ) ;
201
-
202
- if ( ClientUsername . IsNullOrBlank ( ) || ClientPassword . IsNullOrBlank ( ) )
203
- throw new ArgumentException ( "You must specify user credentials" ) ;
180
+ Ensure . NotEmpty ( AccessTokenUrl , nameof ( AccessTokenUrl ) ) ;
181
+ Ensure . NotEmpty ( ConsumerKey , nameof ( ConsumerKey ) ) ;
182
+ Ensure . NotEmpty ( ConsumerSecret , nameof ( ConsumerSecret ) ) ;
183
+ Ensure . NotEmpty ( ClientUsername , nameof ( ClientUsername ) ) ;
204
184
}
205
185
206
186
void ValidateProtectedResourceState ( )
207
187
{
208
- if ( ConsumerKey . IsNullOrBlank ( ) )
209
- throw new ArgumentException ( "You must specify a consumer key" ) ;
210
-
211
- if ( ConsumerSecret . IsNullOrBlank ( ) )
212
- throw new ArgumentException ( "You must specify a consumer secret" ) ;
188
+ Ensure . NotEmpty ( ConsumerKey , nameof ( ConsumerKey ) ) ;
189
+ Ensure . NotEmpty ( ConsumerSecret , nameof ( ConsumerSecret ) ) ;
213
190
}
214
191
215
192
void AddAuthParameters ( ICollection < WebPair > parameters , string timestamp , string nonce )
@@ -223,16 +200,16 @@ void AddAuthParameters(ICollection<WebPair> parameters, string timestamp, string
223
200
new WebPair ( "oauth_version" , Version ?? "1.0" )
224
201
} ;
225
202
226
- if ( ! Token . IsNullOrBlank ( ) )
203
+ if ( ! Token . IsEmpty ( ) )
227
204
authParameters . Add ( new WebPair ( "oauth_token" , Token ) ) ;
228
205
229
- if ( ! CallbackUrl . IsNullOrBlank ( ) )
206
+ if ( ! CallbackUrl . IsEmpty ( ) )
230
207
authParameters . Add ( new WebPair ( "oauth_callback" , CallbackUrl ) ) ;
231
208
232
- if ( ! Verifier . IsNullOrBlank ( ) )
209
+ if ( ! Verifier . IsEmpty ( ) )
233
210
authParameters . Add ( new WebPair ( "oauth_verifier" , Verifier ) ) ;
234
211
235
- if ( ! SessionHandle . IsNullOrBlank ( ) )
212
+ if ( ! SessionHandle . IsEmpty ( ) )
236
213
authParameters . Add ( new WebPair ( "oauth_session_handle" , SessionHandle ) ) ;
237
214
238
215
foreach ( var authParameter in authParameters )
0 commit comments