/RestSharp/Authenticators/OAuth2Authenticator.cs

https://bitbucket.org/ryexley/f1-people · C# · 106 lines · 45 code · 9 blank · 52 comment · 1 complexity · f09b4e3dd8a4bf3e5f10db9436cf2729 MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. namespace RestSharp
  4. {
  5. /// <summary>
  6. /// Base class for OAuth 2 Authenticators.
  7. /// </summary>
  8. /// <remarks>
  9. /// Since there are many ways to authenticate in OAuth2,
  10. /// this is used as a base class to differentiate between
  11. /// other authenticators.
  12. ///
  13. /// Any other OAuth2 authenticators must derive from this
  14. /// abstract class.
  15. /// </remarks>
  16. public abstract class OAuth2Authenticator : IAuthenticator
  17. {
  18. /// <summary>
  19. /// Access token to be used when authenticating.
  20. /// </summary>
  21. private readonly string _accessToken;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="OAuth2Authenticator"/> class.
  24. /// </summary>
  25. /// <param name="accessToken">
  26. /// The access token.
  27. /// </param>
  28. public OAuth2Authenticator(string accessToken)
  29. {
  30. _accessToken = accessToken;
  31. }
  32. /// <summary>
  33. /// Gets the access token.
  34. /// </summary>
  35. public string AccessToken
  36. {
  37. get { return _accessToken; }
  38. }
  39. public abstract void Authenticate(RestClient client, RestRequest request);
  40. }
  41. /// <summary>
  42. /// The OAuth 2 authenticator using URI query parameter.
  43. /// </summary>
  44. /// <remarks>
  45. /// Based on http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-5.1.2
  46. /// </remarks>
  47. public class OAuth2UriQueryParameterAuthenticator : OAuth2Authenticator
  48. {
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="OAuth2UriQueryParameterAuthenticator"/> class.
  51. /// </summary>
  52. /// <param name="accessToken">
  53. /// The access token.
  54. /// </param>
  55. public OAuth2UriQueryParameterAuthenticator(string accessToken)
  56. : base(accessToken)
  57. {
  58. }
  59. public override void Authenticate(RestClient client, RestRequest request)
  60. {
  61. request.AddParameter("oauth_token", AccessToken, ParameterType.GetOrPost);
  62. }
  63. }
  64. /// <summary>
  65. /// The OAuth 2 authenticator using the authorization request header field.
  66. /// </summary>
  67. /// <remarks>
  68. /// Based on http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-5.1.1
  69. /// </remarks>
  70. public class OAuth2AuthorizationRequestHeaderAuthenticator : OAuth2Authenticator
  71. {
  72. /// <summary>
  73. /// Stores the Authoriztion header value as "OAuth accessToken". used for performance.
  74. /// </summary>
  75. private readonly string _authorizationValue;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="OAuth2AuthorizationRequestHeaderAuthenticator"/> class.
  78. /// </summary>
  79. /// <param name="accessToken">
  80. /// The access token.
  81. /// </param>
  82. public OAuth2AuthorizationRequestHeaderAuthenticator(string accessToken)
  83. : base(accessToken)
  84. {
  85. // Conatenate during constructor so that it is only done once. can improve performance.
  86. _authorizationValue = "OAuth " + accessToken;
  87. }
  88. public override void Authenticate(RestClient client, RestRequest request)
  89. {
  90. // only add the Authorization parameter if it hasn't been added.
  91. if (!request.Parameters.Any(p => p.Name.Equals("Authorization", StringComparison.InvariantCultureIgnoreCase)))
  92. {
  93. request.AddParameter("Authorization", _authorizationValue, ParameterType.HttpHeader);
  94. }
  95. }
  96. }
  97. }