1616using System . Collections . Generic ;
1717using System . Web ;
1818using RestSharp . Authenticators . OAuth . Extensions ;
19+ using RestSharp . Extensions ;
20+ using RestSharp . Validation ;
1921
2022namespace RestSharp . Authenticators . OAuth
2123{
2224 /// <summary>
2325 /// A class to encapsulate OAuth authentication flow.
2426 /// </summary>
25- internal sealed class OAuthWorkflow
27+ sealed class OAuthWorkflow
2628 {
2729 public string Version { get ; set ; }
2830
@@ -66,8 +68,7 @@ public string BuildRequestTokenInfo(string method, WebPairCollection parameters)
6668 {
6769 ValidateTokenRequestState ( ) ;
6870
69- if ( parameters == null )
70- parameters = new WebPairCollection ( ) ;
71+ parameters ??= new WebPairCollection ( ) ;
7172
7273 var timestamp = OAuthTools . GetTimestamp ( ) ;
7374 var nonce = OAuthTools . GetNonce ( ) ;
@@ -89,8 +90,7 @@ public string BuildAccessTokenSignature(string method, WebPairCollection paramet
8990 {
9091 ValidateAccessRequestState ( ) ;
9192
92- if ( parameters == null )
93- parameters = new WebPairCollection ( ) ;
93+ parameters ??= new WebPairCollection ( ) ;
9494
9595 var uri = new Uri ( AccessTokenUrl ) ;
9696 var timestamp = OAuthTools . GetTimestamp ( ) ;
@@ -138,8 +138,7 @@ public string BuildProtectedResourceSignature(string method, WebPairCollection p
138138 {
139139 ValidateProtectedResourceState ( ) ;
140140
141- if ( parameters == null )
142- parameters = new WebPairCollection ( ) ;
141+ parameters ??= new WebPairCollection ( ) ;
143142
144143 // Include url parameters in query pool
145144 var uri = new Uri ( url ) ;
@@ -163,53 +162,31 @@ public string BuildProtectedResourceSignature(string method, WebPairCollection p
163162
164163 void ValidateTokenRequestState ( )
165164 {
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 ) ) ;
174168 }
175169
176170 void ValidateAccessRequestState ( )
177171 {
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 ) ) ;
189176 }
190177
191178 void ValidateClientAuthAccessRequestState ( )
192179 {
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 ) ) ;
204184 }
205185
206186 void ValidateProtectedResourceState ( )
207187 {
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 ) ) ;
213190 }
214191
215192 void AddAuthParameters ( ICollection < WebPair > parameters , string timestamp , string nonce )
@@ -223,16 +200,16 @@ void AddAuthParameters(ICollection<WebPair> parameters, string timestamp, string
223200 new WebPair ( "oauth_version" , Version ?? "1.0" )
224201 } ;
225202
226- if ( ! Token . IsNullOrBlank ( ) )
203+ if ( ! Token . IsEmpty ( ) )
227204 authParameters . Add ( new WebPair ( "oauth_token" , Token ) ) ;
228205
229- if ( ! CallbackUrl . IsNullOrBlank ( ) )
206+ if ( ! CallbackUrl . IsEmpty ( ) )
230207 authParameters . Add ( new WebPair ( "oauth_callback" , CallbackUrl ) ) ;
231208
232- if ( ! Verifier . IsNullOrBlank ( ) )
209+ if ( ! Verifier . IsEmpty ( ) )
233210 authParameters . Add ( new WebPair ( "oauth_verifier" , Verifier ) ) ;
234211
235- if ( ! SessionHandle . IsNullOrBlank ( ) )
212+ if ( ! SessionHandle . IsEmpty ( ) )
236213 authParameters . Add ( new WebPair ( "oauth_session_handle" , SessionHandle ) ) ;
237214
238215 foreach ( var authParameter in authParameters )
0 commit comments