oauth_request_signer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef GOOGLE_APIS_GAIA_OAUTH_REQUEST_SIGNER_H_
  5. #define GOOGLE_APIS_GAIA_OAUTH_REQUEST_SIGNER_H_
  6. #include <map>
  7. #include <string>
  8. class GURL;
  9. // Implements the OAuth request signing process as described here:
  10. // http://oauth.net/core/1.0/#signing_process
  11. //
  12. // NOTE: Currently the only supported SignatureMethod is HMAC_SHA1_SIGNATURE
  13. class OAuthRequestSigner {
  14. public:
  15. enum SignatureMethod {
  16. HMAC_SHA1_SIGNATURE,
  17. RSA_SHA1_SIGNATURE,
  18. PLAINTEXT_SIGNATURE
  19. };
  20. enum HttpMethod {
  21. GET_METHOD,
  22. POST_METHOD
  23. };
  24. typedef std::map<std::string,std::string> Parameters;
  25. OAuthRequestSigner() = delete;
  26. OAuthRequestSigner(const OAuthRequestSigner&) = delete;
  27. OAuthRequestSigner& operator=(const OAuthRequestSigner&) = delete;
  28. // Percent encoding and decoding for OAuth.
  29. //
  30. // The form of percent encoding used for OAuth request signing is very
  31. // specific and strict. See http://oauth.net/core/1.0/#encoding_parameters.
  32. // This definition is considered the current standard as of January 2005.
  33. // While as of July 2011 many systems to do not comply, any valid OAuth
  34. // implementation must comply.
  35. //
  36. // Any character which is in the "unreserved set" MUST NOT be encoded.
  37. // All other characters MUST be encoded.
  38. //
  39. // The unreserved set is comprised of the alphanumeric characters and these
  40. // others:
  41. // - minus (-)
  42. // - period (.)
  43. // - underscore (_)
  44. // - tilde (~)
  45. static bool Decode(const std::string& text, std::string* decoded_text);
  46. static std::string Encode(const std::string& text);
  47. // Signs a request specified as URL string, complete with parameters.
  48. //
  49. // If HttpMethod is GET_METHOD, the signed result is the full URL, otherwise
  50. // it is the request parameters, including the oauth_signature field.
  51. static bool ParseAndSign(const GURL& request_url_with_parameters,
  52. SignatureMethod signature_method,
  53. HttpMethod http_method,
  54. const std::string& consumer_key,
  55. const std::string& consumer_secret,
  56. const std::string& token_key,
  57. const std::string& token_secret,
  58. std::string* signed_result);
  59. // Signs a request specified as the combination of a base URL string, with
  60. // parameters included in a separate map data structure. NOTE: The base URL
  61. // string must not contain a question mark (?) character. If it does,
  62. // you can use ParseAndSign() instead.
  63. //
  64. // If HttpMethod is GET_METHOD, the signed result is the full URL, otherwise
  65. // it is the request parameters, including the oauth_signature field.
  66. static bool SignURL(const GURL& request_base_url,
  67. const Parameters& parameters,
  68. SignatureMethod signature_method,
  69. HttpMethod http_method,
  70. const std::string& consumer_key,
  71. const std::string& consumer_secret,
  72. const std::string& token_key,
  73. const std::string& token_secret,
  74. std::string* signed_result);
  75. // Similar to SignURL(), but the returned string is not a URL, but the payload
  76. // to for an HTTP Authorization header.
  77. static bool SignAuthHeader(const GURL& request_base_url,
  78. const Parameters& parameters,
  79. SignatureMethod signature_method,
  80. HttpMethod http_method,
  81. const std::string& consumer_key,
  82. const std::string& consumer_secret,
  83. const std::string& token_key,
  84. const std::string& token_secret,
  85. std::string* signed_result);
  86. };
  87. #endif // GOOGLE_APIS_GAIA_OAUTH_REQUEST_SIGNER_H_