HTTPCredentials.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // HTTPCredentials.h
  3. //
  4. // Library: Net
  5. // Package: HTTP
  6. // Module: HTTPCredentials
  7. //
  8. // Definition of the HTTPCredentials class.
  9. //
  10. // Copyright (c) 2011, Anton V. Yabchinskiy (arn at bestmx dot ru).
  11. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  12. // and Contributors.
  13. //
  14. // SPDX-License-Identifier: BSL-1.0
  15. //
  16. #ifndef Net_HTTPCredentials_INCLUDED
  17. #define Net_HTTPCredentials_INCLUDED
  18. #include "Poco/Net/HTTPDigestCredentials.h"
  19. namespace Poco {
  20. class URI;
  21. namespace Net {
  22. class HTTPRequest;
  23. class HTTPResponse;
  24. class Net_API HTTPCredentials
  25. /// This is a utility class for working with HTTP
  26. /// authentication (Basic or Digest) in HTTPRequest objects.
  27. ///
  28. /// Usage is as follows:
  29. /// First, create a HTTPCredentials object containing
  30. /// the username and password.
  31. /// Poco::Net::HTTPCredentials creds("user", "s3cr3t");
  32. ///
  33. /// Second, send the HTTP request with Poco::Net::HTTPClientSession.
  34. /// Poco::Net::HTTPClientSession session("pocoproject.org");
  35. /// Poco::Net::HTTPRequest request(HTTPRequest::HTTP_GET, "/index.html", HTTPMessage::HTTP_1_1);
  36. /// session.sendRequest(request);
  37. /// Poco::Net::HTTPResponse;
  38. /// std::istream& istr = session.receiveResponse(response);
  39. ///
  40. /// If the server responds with a 401 status, authenticate the
  41. /// request and resend it:
  42. /// if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
  43. /// {
  44. /// creds.authenticate(request, response);
  45. /// session.sendRequest(request);
  46. /// ...
  47. /// }
  48. ///
  49. /// To perform multiple authenticated requests, call updateAuthInfo()
  50. /// instead of authenticate() on subsequent requests.
  51. /// creds.updateAuthInfo(request);
  52. /// session.sendRequest(request);
  53. /// ...
  54. ///
  55. /// Note: Do not forget to read the entire response stream from the 401 response
  56. /// before sending the authenticated request, otherwise there may be
  57. /// problems if a persistent connection is used.
  58. {
  59. public:
  60. HTTPCredentials();
  61. /// Creates an empty HTTPCredentials object.
  62. HTTPCredentials(const std::string& username, const std::string& password);
  63. /// Creates an HTTPCredentials object with the given username and password.
  64. ~HTTPCredentials();
  65. /// Destroys the HTTPCredentials.
  66. void fromUserInfo(const std::string& userInfo);
  67. /// Parses username:password string and sets username and password of
  68. /// the credentials object.
  69. /// Throws SyntaxException on invalid user information.
  70. void fromURI(const URI& uri);
  71. /// Extracts username and password from the given URI and sets username
  72. /// and password of the credentials object.
  73. /// Does nothing if URI has no user info part.
  74. void clear();
  75. /// Clears username, password and host.
  76. void setUsername(const std::string& username);
  77. /// Sets the username.
  78. const std::string& getUsername() const;
  79. /// Returns the username.
  80. void setPassword(const std::string& password);
  81. /// Sets the password.
  82. const std::string& getPassword() const;
  83. /// Returns the password.
  84. bool empty() const;
  85. /// Returns true if both username and password are empty, otherwise false.
  86. void authenticate(HTTPRequest& request, const HTTPResponse& response);
  87. /// Inspects WWW-Authenticate header of the response, initializes
  88. /// the internal state (in case of digest authentication) and
  89. /// adds required information to the given HTTPRequest.
  90. ///
  91. /// Does nothing if there is no WWW-Authenticate header in the
  92. /// HTTPResponse.
  93. void updateAuthInfo(HTTPRequest& request);
  94. /// Updates internal state (in case of digest authentication) and
  95. /// replaces authentication information in the request accordingly.
  96. void proxyAuthenticate(HTTPRequest& request, const HTTPResponse& response);
  97. /// Inspects Proxy-Authenticate header of the response, initializes
  98. /// the internal state (in case of digest authentication) and
  99. /// adds required information to the given HTTPRequest.
  100. ///
  101. /// Does nothing if there is no Proxy-Authenticate header in the
  102. /// HTTPResponse.
  103. void updateProxyAuthInfo(HTTPRequest& request);
  104. /// Updates internal state (in case of digest authentication) and
  105. /// replaces proxy authentication information in the request accordingly.
  106. static bool isBasicCredentials(const std::string& header);
  107. /// Returns true if authentication header is for Basic authentication.
  108. static bool isDigestCredentials(const std::string& header);
  109. /// Returns true if authentication header is for Digest authentication.
  110. static bool hasBasicCredentials(const HTTPRequest& request);
  111. /// Returns true if an Authorization header with Basic credentials is present in the request.
  112. static bool hasDigestCredentials(const HTTPRequest& request);
  113. /// Returns true if an Authorization header with Digest credentials is present in the request.
  114. static bool hasNTLMCredentials(const HTTPRequest& request);
  115. /// Returns true if an Authorization header with NTLM credentials is present in the request.
  116. static bool hasProxyBasicCredentials(const HTTPRequest& request);
  117. /// Returns true if a Proxy-Authorization header with Basic credentials is present in the request.
  118. static bool hasProxyDigestCredentials(const HTTPRequest& request);
  119. /// Returns true if a Proxy-Authorization header with Digest credentials is present in the request.
  120. static void extractCredentials(const std::string& userInfo, std::string& username, std::string& password);
  121. /// Extracts username and password from user:password information string.
  122. static void extractCredentials(const Poco::URI& uri, std::string& username, std::string& password);
  123. /// Extracts username and password from the given URI (e.g.: "http://user:pass@sample.com/secret").
  124. private:
  125. HTTPCredentials(const HTTPCredentials&);
  126. HTTPCredentials& operator = (const HTTPCredentials&);
  127. HTTPDigestCredentials _digest;
  128. };
  129. //
  130. // inlines
  131. //
  132. inline void HTTPCredentials::setUsername(const std::string& username)
  133. {
  134. _digest.setUsername(username);
  135. }
  136. inline const std::string& HTTPCredentials::getUsername() const
  137. {
  138. return _digest.getUsername();
  139. }
  140. inline void HTTPCredentials::setPassword(const std::string& password)
  141. {
  142. _digest.setPassword(password);
  143. }
  144. inline const std::string& HTTPCredentials::getPassword() const
  145. {
  146. return _digest.getPassword();
  147. }
  148. inline bool HTTPCredentials::empty() const
  149. {
  150. return _digest.empty();
  151. }
  152. } } // namespace Poco::Net
  153. #endif // Net_HTTPCredentials_INCLUDED