22
22
namespace RestSharp . Authenticators ;
23
23
24
24
/// <seealso href="http://tools.ietf.org/html/rfc5849">RFC: The OAuth 1.0 Protocol</seealso>
25
- public class OAuth1Auth : IAuthenticator {
25
+ public class OAuth1Authenticator : IAuthenticator {
26
26
public virtual string ? Realm { get ; set ; }
27
27
public virtual OAuthParameterHandling ParameterHandling { get ; set ; }
28
28
public virtual OAuthSignatureMethod SignatureMethod { get ; set ; }
@@ -56,12 +56,19 @@ public ValueTask Authenticate(IRestClient client, RestRequest request) {
56
56
ClientPassword = ClientPassword
57
57
} ;
58
58
59
- AddOAuthData ( client , request , workflow ) ;
59
+ AddOAuthData ( client , request , workflow , Type , Realm ) ;
60
60
return default ;
61
61
}
62
62
63
+ /// <summary>
64
+ /// Creates an authenticator to retrieve a request token.
65
+ /// </summary>
66
+ /// <param name="consumerKey">Consumer or API key</param>
67
+ /// <param name="consumerSecret">Consumer or API secret</param>
68
+ /// <param name="signatureMethod">Signature method, default is HMAC SHA1</param>
69
+ /// <returns>Authenticator instance</returns>
63
70
[ PublicAPI ]
64
- public static OAuth1Auth ForRequestToken (
71
+ public static OAuth1Authenticator ForRequestToken (
65
72
string consumerKey ,
66
73
string ? consumerSecret ,
67
74
OAuthSignatureMethod signatureMethod = OAuthSignatureMethod . HmacSha1
@@ -75,17 +82,33 @@ public static OAuth1Auth ForRequestToken(
75
82
Type = OAuthType . RequestToken
76
83
} ;
77
84
85
+ /// <summary>
86
+ /// Creates an authenticator to retrieve a request token with custom callback.
87
+ /// </summary>
88
+ /// <param name="consumerKey">Consumer or API key</param>
89
+ /// <param name="consumerSecret">Consumer or API secret</param>
90
+ /// <param name="callbackUrl">URL to where the user will be redirected to after authhentication</param>
91
+ /// <returns>Authenticator instance</returns>
78
92
[ PublicAPI ]
79
- public static OAuth1Auth ForRequestToken ( string consumerKey , string ? consumerSecret , string callbackUrl ) {
93
+ public static OAuth1Authenticator ForRequestToken ( string consumerKey , string ? consumerSecret , string callbackUrl ) {
80
94
var authenticator = ForRequestToken ( consumerKey , consumerSecret ) ;
81
95
82
96
authenticator . CallbackUrl = callbackUrl ;
83
97
84
98
return authenticator ;
85
99
}
86
100
101
+ /// <summary>
102
+ /// Creates an authenticator to retrieve an access token using the request token.
103
+ /// </summary>
104
+ /// <param name="consumerKey">Consumer or API key</param>
105
+ /// <param name="consumerSecret">Consumer or API secret</param>
106
+ /// <param name="token">Request token</param>
107
+ /// <param name="tokenSecret">Request token secret</param>
108
+ /// <param name="signatureMethod">Signature method, default is HMAC SHA1</param>
109
+ /// <returns>Authenticator instance</returns>
87
110
[ PublicAPI ]
88
- public static OAuth1Auth ForAccessToken (
111
+ public static OAuth1Authenticator ForAccessToken (
89
112
string consumerKey ,
90
113
string ? consumerSecret ,
91
114
string token ,
@@ -103,8 +126,17 @@ public static OAuth1Auth ForAccessToken(
103
126
Type = OAuthType . AccessToken
104
127
} ;
105
128
129
+ /// <summary>
130
+ /// Creates an authenticator to retrieve an access token using the request token and a verifier.
131
+ /// </summary>
132
+ /// <param name="consumerKey">Consumer or API key</param>
133
+ /// <param name="consumerSecret">Consumer or API secret</param>
134
+ /// <param name="token">Request token</param>
135
+ /// <param name="tokenSecret">Request token secret</param>
136
+ /// <param name="verifier">Verifier received from the API server</param>
137
+ /// <returns>Authenticator instance</returns>
106
138
[ PublicAPI ]
107
- public static OAuth1Auth ForAccessToken (
139
+ public static OAuth1Authenticator ForAccessToken (
108
140
string consumerKey ,
109
141
string ? consumerSecret ,
110
142
string token ,
@@ -119,7 +151,7 @@ string verifier
119
151
}
120
152
121
153
[ PublicAPI ]
122
- public static OAuth1Auth ForAccessTokenRefresh (
154
+ public static OAuth1Authenticator ForAccessTokenRefresh (
123
155
string consumerKey ,
124
156
string ? consumerSecret ,
125
157
string token ,
@@ -134,7 +166,7 @@ string sessionHandle
134
166
}
135
167
136
168
[ PublicAPI ]
137
- public static OAuth1Auth ForAccessTokenRefresh (
169
+ public static OAuth1Authenticator ForAccessTokenRefresh (
138
170
string consumerKey ,
139
171
string ? consumerSecret ,
140
172
string token ,
@@ -151,7 +183,7 @@ string sessionHandle
151
183
}
152
184
153
185
[ PublicAPI ]
154
- public static OAuth1Auth ForClientAuthentication (
186
+ public static OAuth1Authenticator ForClientAuthentication (
155
187
string consumerKey ,
156
188
string ? consumerSecret ,
157
189
string username ,
@@ -169,8 +201,17 @@ public static OAuth1Auth ForClientAuthentication(
169
201
Type = OAuthType . ClientAuthentication
170
202
} ;
171
203
204
+ /// <summary>
205
+ /// Creates an authenticator to make calls to protected resources using the access token.
206
+ /// </summary>
207
+ /// <param name="consumerKey">Consumer or API key</param>
208
+ /// <param name="consumerSecret">Consumer or API secret</param>
209
+ /// <param name="accessToken">Access token</param>
210
+ /// <param name="accessTokenSecret">Access token secret</param>
211
+ /// <param name="signatureMethod">Signature method, default is HMAC SHA1</param>
212
+ /// <returns>Authenticator instance</returns>
172
213
[ PublicAPI ]
173
- public static OAuth1Auth ForProtectedResource (
214
+ public static OAuth1Authenticator ForProtectedResource (
174
215
string consumerKey ,
175
216
string ? consumerSecret ,
176
217
string accessToken ,
@@ -188,7 +229,13 @@ public static OAuth1Auth ForProtectedResource(
188
229
TokenSecret = accessTokenSecret
189
230
} ;
190
231
191
- void AddOAuthData ( IRestClient client , RestRequest request , OAuthWorkflow workflow ) {
232
+ internal static void AddOAuthData (
233
+ IRestClient client ,
234
+ RestRequest request ,
235
+ OAuthWorkflow workflow ,
236
+ OAuthType type ,
237
+ string ? realm
238
+ ) {
192
239
var requestUrl = client . BuildUriWithoutQueryParameters ( request ) . AbsoluteUri ;
193
240
194
241
if ( requestUrl . Contains ( '?' ) )
@@ -204,13 +251,6 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
204
251
var method = request . Method . ToString ( ) . ToUpperInvariant ( ) ;
205
252
var parameters = new WebPairCollection ( ) ;
206
253
207
- // include all GET and POST parameters before generating the signature
208
- // according to the RFC 5849 - The OAuth 1.0 Protocol
209
- // http://tools.ietf.org/html/rfc5849#section-3.4.1
210
- // if this change causes trouble we need to introduce a flag indicating the specific OAuth implementation level,
211
- // or implement a separate class for each OAuth version
212
- static bool BaseQuery ( Parameter x ) => x . Type is ParameterType . GetOrPost or ParameterType . QueryString ;
213
-
214
254
var query =
215
255
request . AlwaysMultipartFormData || request . Files . Count > 0
216
256
? x => BaseQuery ( x ) && x . Name != null && x . Name . StartsWith ( "oauth_" )
@@ -219,22 +259,19 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
219
259
parameters . AddRange ( client . DefaultParameters . Where ( query ) . ToWebParameters ( ) ) ;
220
260
parameters . AddRange ( request . Parameters . Where ( query ) . ToWebParameters ( ) ) ;
221
261
222
- if ( Type == OAuthType . RequestToken )
223
- workflow . RequestTokenUrl = url ;
224
- else
225
- workflow . AccessTokenUrl = url ;
262
+ workflow . RequestUrl = url ;
226
263
227
- var oauth = Type switch {
228
- OAuthType . RequestToken => workflow . BuildRequestTokenInfo ( method , parameters ) ,
264
+ var oauth = type switch {
265
+ OAuthType . RequestToken => workflow . BuildRequestTokenSignature ( method , parameters ) ,
229
266
OAuthType . AccessToken => workflow . BuildAccessTokenSignature ( method , parameters ) ,
230
267
OAuthType . ClientAuthentication => workflow . BuildClientAuthAccessTokenSignature ( method , parameters ) ,
231
- OAuthType . ProtectedResource => workflow . BuildProtectedResourceSignature ( method , parameters , url ) ,
268
+ OAuthType . ProtectedResource => workflow . BuildProtectedResourceSignature ( method , parameters ) ,
232
269
_ => throw new ArgumentOutOfRangeException ( nameof ( Type ) )
233
270
} ;
234
271
235
272
oauth . Parameters . Add ( "oauth_signature" , oauth . Signature ) ;
236
273
237
- var oauthParameters = ParameterHandling switch {
274
+ var oauthParameters = workflow . ParameterHandling switch {
238
275
OAuthParameterHandling . HttpAuthorizationHeader => CreateHeaderParameters ( ) ,
239
276
OAuthParameterHandling . UrlOrPostParameters => CreateUrlParameters ( ) ,
240
277
_ => throw new ArgumentOutOfRangeException ( nameof ( ParameterHandling ) )
@@ -243,7 +280,14 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
243
280
request . AddOrUpdateParameters ( oauthParameters ) ;
244
281
return ;
245
282
246
- IEnumerable < Parameter > CreateHeaderParameters ( ) => new [ ] { new HeaderParameter ( KnownHeaders . Authorization , GetAuthorizationHeader ( ) ) } ;
283
+ // include all GET and POST parameters before generating the signature
284
+ // according to the RFC 5849 - The OAuth 1.0 Protocol
285
+ // http://tools.ietf.org/html/rfc5849#section-3.4.1
286
+ // if this change causes trouble we need to introduce a flag indicating the specific OAuth implementation level,
287
+ // or implement a separate class for each OAuth version
288
+ static bool BaseQuery ( Parameter x ) => x . Type is ParameterType . GetOrPost or ParameterType . QueryString ;
289
+
290
+ IEnumerable < Parameter > CreateHeaderParameters ( ) => [ new HeaderParameter ( KnownHeaders . Authorization , GetAuthorizationHeader ( ) ) ] ;
247
291
248
292
IEnumerable < Parameter > CreateUrlParameters ( ) => oauth . Parameters . Select ( p => new GetOrPostParameter ( p . Name , HttpUtility . UrlDecode ( p . Value ) ) ) ;
249
293
@@ -254,7 +298,7 @@ string GetAuthorizationHeader() {
254
298
. Select ( x => x . GetQueryParameter ( true ) )
255
299
. ToList ( ) ;
256
300
257
- if ( ! Realm . IsEmpty ( ) ) oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( Realm ) } \" ") ;
301
+ if ( ! realm . IsEmpty ( ) ) oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( realm ) } \" ") ;
258
302
259
303
return $ "OAuth { string . Join ( "," , oathParameters ) } ";
260
304
}
0 commit comments