16
16
using RestSharp . Extensions ;
17
17
using System . Web ;
18
18
19
+ // ReSharper disable NotResolvedInText
19
20
// ReSharper disable CheckNamespace
20
21
21
22
namespace RestSharp . Authenticators ;
@@ -38,7 +39,7 @@ public class OAuth1Authenticator : IAuthenticator {
38
39
public virtual string ? ClientUsername { get ; set ; }
39
40
public virtual string ? ClientPassword { get ; set ; }
40
41
41
- public ValueTask Authenticate ( RestClient client , RestRequest request ) {
42
+ public ValueTask Authenticate ( IRestClient client , RestRequest request ) {
42
43
var workflow = new OAuthWorkflow {
43
44
ConsumerKey = ConsumerKey ,
44
45
ConsumerSecret = ConsumerSecret ,
@@ -64,8 +65,8 @@ public static OAuth1Authenticator ForRequestToken(
64
65
string consumerKey ,
65
66
string ? consumerSecret ,
66
67
OAuthSignatureMethod signatureMethod = OAuthSignatureMethod . HmacSha1
67
- ) {
68
- var authenticator = new OAuth1Authenticator {
68
+ )
69
+ => new ( ) {
69
70
ParameterHandling = OAuthParameterHandling . HttpAuthorizationHeader ,
70
71
SignatureMethod = signatureMethod ,
71
72
SignatureTreatment = OAuthSignatureTreatment . Escaped ,
@@ -74,10 +75,6 @@ public static OAuth1Authenticator ForRequestToken(
74
75
Type = OAuthType . RequestToken
75
76
} ;
76
77
77
- return authenticator ;
78
- }
79
-
80
- [ PublicAPI ]
81
78
public static OAuth1Authenticator ForRequestToken ( string consumerKey , string ? consumerSecret , string callbackUrl ) {
82
79
var authenticator = ForRequestToken ( consumerKey , consumerSecret ) ;
83
80
@@ -105,7 +102,6 @@ public static OAuth1Authenticator ForAccessToken(
105
102
Type = OAuthType . AccessToken
106
103
} ;
107
104
108
- [ PublicAPI ]
109
105
public static OAuth1Authenticator ForAccessToken (
110
106
string consumerKey ,
111
107
string ? consumerSecret ,
@@ -171,7 +167,6 @@ public static OAuth1Authenticator ForClientAuthentication(
171
167
Type = OAuthType . ClientAuthentication
172
168
} ;
173
169
174
- [ PublicAPI ]
175
170
public static OAuth1Authenticator ForProtectedResource (
176
171
string consumerKey ,
177
172
string ? consumerSecret ,
@@ -190,7 +185,7 @@ public static OAuth1Authenticator ForProtectedResource(
190
185
TokenSecret = accessTokenSecret
191
186
} ;
192
187
193
- void AddOAuthData ( RestClient client , RestRequest request , OAuthWorkflow workflow ) {
188
+ void AddOAuthData ( IRestClient client , RestRequest request , OAuthWorkflow workflow ) {
194
189
var requestUrl = client . BuildUriWithoutQueryParameters ( request ) . AbsoluteUri ;
195
190
196
191
if ( requestUrl . Contains ( '?' ) )
@@ -201,11 +196,9 @@ void AddOAuthData(RestClient client, RestRequest request, OAuthWorkflow workflow
201
196
var url = client . BuildUri ( request ) . ToString ( ) ;
202
197
var queryStringStart = url . IndexOf ( '?' ) ;
203
198
204
- if ( queryStringStart != - 1 )
205
- url = url . Substring ( 0 , queryStringStart ) ;
206
-
207
- var method = request . Method . ToString ( ) . ToUpperInvariant ( ) ;
199
+ if ( queryStringStart != - 1 ) url = url . Substring ( 0 , queryStringStart ) ;
208
200
201
+ var method = request . Method . ToString ( ) . ToUpperInvariant ( ) ;
209
202
var parameters = new WebPairCollection ( ) ;
210
203
211
204
// include all GET and POST parameters before generating the signature
@@ -247,21 +240,18 @@ void AddOAuthData(RestClient client, RestRequest request, OAuthWorkflow workflow
247
240
248
241
request . AddOrUpdateParameters ( oauthParameters ) ;
249
242
250
- IEnumerable < Parameter > CreateHeaderParameters ( )
251
- => new [ ] { new HeaderParameter ( KnownHeaders . Authorization , GetAuthorizationHeader ( ) ) } ;
243
+ IEnumerable < Parameter > CreateHeaderParameters ( ) => new [ ] { new HeaderParameter ( KnownHeaders . Authorization , GetAuthorizationHeader ( ) ) } ;
252
244
253
- IEnumerable < Parameter > CreateUrlParameters ( )
254
- => oauth . Parameters . Select ( p => new GetOrPostParameter ( p . Name , HttpUtility . UrlDecode ( p . Value ) ) ) ;
245
+ IEnumerable < Parameter > CreateUrlParameters ( ) => oauth . Parameters . Select ( p => new GetOrPostParameter ( p . Name , HttpUtility . UrlDecode ( p . Value ) ) ) ;
255
246
256
247
string GetAuthorizationHeader ( ) {
257
248
var oathParameters =
258
249
oauth . Parameters
259
250
. OrderBy ( x => x , WebPair . Comparer )
260
- . Select ( x => $ " { x . Name } = \" { x . WebValue } \" " )
251
+ . Select ( x => x . GetQueryParameter ( true ) )
261
252
. ToList ( ) ;
262
253
263
- if ( ! Realm . IsEmpty ( ) )
264
- oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( Realm ! ) } \" ") ;
254
+ if ( ! Realm . IsEmpty ( ) ) oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( Realm ) } \" ") ;
265
255
266
256
return $ "OAuth { string . Join ( "," , oathParameters ) } ";
267
257
}
@@ -270,5 +260,5 @@ string GetAuthorizationHeader() {
270
260
271
261
static class ParametersExtensions {
272
262
internal static IEnumerable < WebPair > ToWebParameters ( this IEnumerable < Parameter > p )
273
- => p . Select ( x => new WebPair ( Ensure . NotNull ( x . Name , "Parameter name" ) , Ensure . NotNull ( x . Value , "Parameter value" ) . ToString ( ) ! ) ) ;
274
- }
263
+ => p . Select ( x => new WebPair ( Ensure . NotNull ( x . Name , "Parameter name" ) , x . Value ? . ToString ( ) ) ) ;
264
+ }
0 commit comments