2222namespace RestSharp . Authenticators ;
2323
2424/// <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 {
2626 public virtual string ? Realm { get ; set ; }
2727 public virtual OAuthParameterHandling ParameterHandling { get ; set ; }
2828 public virtual OAuthSignatureMethod SignatureMethod { get ; set ; }
@@ -56,12 +56,19 @@ public ValueTask Authenticate(IRestClient client, RestRequest request) {
5656 ClientPassword = ClientPassword
5757 } ;
5858
59- AddOAuthData ( client , request , workflow ) ;
59+ AddOAuthData ( client , request , workflow , Type , Realm ) ;
6060 return default ;
6161 }
6262
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>
6370 [ PublicAPI ]
64- public static OAuth1Auth ForRequestToken (
71+ public static OAuth1Authenticator ForRequestToken (
6572 string consumerKey ,
6673 string ? consumerSecret ,
6774 OAuthSignatureMethod signatureMethod = OAuthSignatureMethod . HmacSha1
@@ -75,17 +82,33 @@ public static OAuth1Auth ForRequestToken(
7582 Type = OAuthType . RequestToken
7683 } ;
7784
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>
7892 [ PublicAPI ]
79- public static OAuth1Auth ForRequestToken ( string consumerKey , string ? consumerSecret , string callbackUrl ) {
93+ public static OAuth1Authenticator ForRequestToken ( string consumerKey , string ? consumerSecret , string callbackUrl ) {
8094 var authenticator = ForRequestToken ( consumerKey , consumerSecret ) ;
8195
8296 authenticator . CallbackUrl = callbackUrl ;
8397
8498 return authenticator ;
8599 }
86100
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>
87110 [ PublicAPI ]
88- public static OAuth1Auth ForAccessToken (
111+ public static OAuth1Authenticator ForAccessToken (
89112 string consumerKey ,
90113 string ? consumerSecret ,
91114 string token ,
@@ -103,8 +126,17 @@ public static OAuth1Auth ForAccessToken(
103126 Type = OAuthType . AccessToken
104127 } ;
105128
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>
106138 [ PublicAPI ]
107- public static OAuth1Auth ForAccessToken (
139+ public static OAuth1Authenticator ForAccessToken (
108140 string consumerKey ,
109141 string ? consumerSecret ,
110142 string token ,
@@ -119,7 +151,7 @@ string verifier
119151 }
120152
121153 [ PublicAPI ]
122- public static OAuth1Auth ForAccessTokenRefresh (
154+ public static OAuth1Authenticator ForAccessTokenRefresh (
123155 string consumerKey ,
124156 string ? consumerSecret ,
125157 string token ,
@@ -134,7 +166,7 @@ string sessionHandle
134166 }
135167
136168 [ PublicAPI ]
137- public static OAuth1Auth ForAccessTokenRefresh (
169+ public static OAuth1Authenticator ForAccessTokenRefresh (
138170 string consumerKey ,
139171 string ? consumerSecret ,
140172 string token ,
@@ -151,7 +183,7 @@ string sessionHandle
151183 }
152184
153185 [ PublicAPI ]
154- public static OAuth1Auth ForClientAuthentication (
186+ public static OAuth1Authenticator ForClientAuthentication (
155187 string consumerKey ,
156188 string ? consumerSecret ,
157189 string username ,
@@ -169,8 +201,17 @@ public static OAuth1Auth ForClientAuthentication(
169201 Type = OAuthType . ClientAuthentication
170202 } ;
171203
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>
172213 [ PublicAPI ]
173- public static OAuth1Auth ForProtectedResource (
214+ public static OAuth1Authenticator ForProtectedResource (
174215 string consumerKey ,
175216 string ? consumerSecret ,
176217 string accessToken ,
@@ -188,7 +229,13 @@ public static OAuth1Auth ForProtectedResource(
188229 TokenSecret = accessTokenSecret
189230 } ;
190231
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+ ) {
192239 var requestUrl = client . BuildUriWithoutQueryParameters ( request ) . AbsoluteUri ;
193240
194241 if ( requestUrl . Contains ( '?' ) )
@@ -204,13 +251,6 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
204251 var method = request . Method . ToString ( ) . ToUpperInvariant ( ) ;
205252 var parameters = new WebPairCollection ( ) ;
206253
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-
214254 var query =
215255 request . AlwaysMultipartFormData || request . Files . Count > 0
216256 ? x => BaseQuery ( x ) && x . Name != null && x . Name . StartsWith ( "oauth_" )
@@ -219,22 +259,19 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
219259 parameters . AddRange ( client . DefaultParameters . Where ( query ) . ToWebParameters ( ) ) ;
220260 parameters . AddRange ( request . Parameters . Where ( query ) . ToWebParameters ( ) ) ;
221261
222- if ( Type == OAuthType . RequestToken )
223- workflow . RequestTokenUrl = url ;
224- else
225- workflow . AccessTokenUrl = url ;
262+ workflow . RequestUrl = url ;
226263
227- var oauth = Type switch {
228- OAuthType . RequestToken => workflow . BuildRequestTokenInfo ( method , parameters ) ,
264+ var oauth = type switch {
265+ OAuthType . RequestToken => workflow . BuildRequestTokenSignature ( method , parameters ) ,
229266 OAuthType . AccessToken => workflow . BuildAccessTokenSignature ( method , parameters ) ,
230267 OAuthType . ClientAuthentication => workflow . BuildClientAuthAccessTokenSignature ( method , parameters ) ,
231- OAuthType . ProtectedResource => workflow . BuildProtectedResourceSignature ( method , parameters , url ) ,
268+ OAuthType . ProtectedResource => workflow . BuildProtectedResourceSignature ( method , parameters ) ,
232269 _ => throw new ArgumentOutOfRangeException ( nameof ( Type ) )
233270 } ;
234271
235272 oauth . Parameters . Add ( "oauth_signature" , oauth . Signature ) ;
236273
237- var oauthParameters = ParameterHandling switch {
274+ var oauthParameters = workflow . ParameterHandling switch {
238275 OAuthParameterHandling . HttpAuthorizationHeader => CreateHeaderParameters ( ) ,
239276 OAuthParameterHandling . UrlOrPostParameters => CreateUrlParameters ( ) ,
240277 _ => throw new ArgumentOutOfRangeException ( nameof ( ParameterHandling ) )
@@ -243,7 +280,14 @@ void AddOAuthData(IRestClient client, RestRequest request, OAuthWorkflow workflo
243280 request . AddOrUpdateParameters ( oauthParameters ) ;
244281 return ;
245282
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 ( ) ) ] ;
247291
248292 IEnumerable < Parameter > CreateUrlParameters ( ) => oauth . Parameters . Select ( p => new GetOrPostParameter ( p . Name , HttpUtility . UrlDecode ( p . Value ) ) ) ;
249293
@@ -254,7 +298,7 @@ string GetAuthorizationHeader() {
254298 . Select ( x => x . GetQueryParameter ( true ) )
255299 . ToList ( ) ;
256300
257- if ( ! Realm . IsEmpty ( ) ) oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( Realm ) } \" ") ;
301+ if ( ! realm . IsEmpty ( ) ) oathParameters . Insert ( 0 , $ "realm=\" { OAuthTools . UrlEncodeRelaxed ( realm ) } \" ") ;
258302
259303 return $ "OAuth { string . Join ( "," , oathParameters ) } ";
260304 }
0 commit comments