Added HTTP*Credentials::empty() and HTTPCredentials::clear()

This commit is contained in:
Günter Obiltschnig 2019-06-24 20:15:40 +02:00
parent 670e7b8827
commit 8dc93706b3
5 changed files with 86 additions and 32 deletions

View File

@ -55,6 +55,9 @@ public:
~HTTPBasicCredentials();
/// Destroys the HTTPBasicCredentials.
void clear();
/// Clears both username and password.
void setUsername(const std::string& username);
/// Sets the username.
@ -67,6 +70,9 @@ public:
const std::string& getPassword() const;
/// Returns the password.
bool empty() const;
/// Returns true if both username and password are empty, otherwise false.
void authenticate(HTTPRequest& request) const;
/// Adds authentication information to the given HTTPRequest.
@ -105,6 +111,12 @@ inline const std::string& HTTPBasicCredentials::getPassword() const
}
inline bool HTTPBasicCredentials::empty() const
{
return _username.empty() && _password.empty();
}
} } // namespace Poco::Net

View File

@ -37,7 +37,7 @@ class HTTPResponse;
class Net_API HTTPCredentials
/// This is a utility class for working with HTTP
/// authentication (basic or digest) in HTTPRequest objects.
/// authentication (Basic or Digest) in HTTPRequest objects.
///
/// Usage is as follows:
/// First, create a HTTPCredentials object containing
@ -90,6 +90,9 @@ public:
/// and password of the credentials object.
/// Does nothing if URI has no user info part.
void clear();
/// Clears username, password and host.
void setUsername(const std::string& username);
/// Sets the username.
@ -102,6 +105,9 @@ public:
const std::string& getPassword() const;
/// Returns the password.
bool empty() const;
/// Returns true if both username and password are empty, otherwise false.
void authenticate(HTTPRequest& request, const HTTPResponse& response);
/// Inspects WWW-Authenticate header of the response, initializes
/// the internal state (in case of digest authentication) and
@ -133,16 +139,19 @@ public:
/// Returns true if authentication header is for Digest authentication.
static bool hasBasicCredentials(const HTTPRequest& request);
/// Returns true if Authorization with Basic credentials header is present in the request.
/// Returns true if an Authorization header with Basic credentials is present in the request.
static bool hasDigestCredentials(const HTTPRequest& request);
/// Returns true if Authorization with Digest credentials header is present in the request.
/// Returns true if an Authorization header with Digest credentials is present in the request.
static bool hasNTLMCredentials(const HTTPRequest& request);
/// Returns true if an Authorization header with NTLM credentials is present in the request.
static bool hasProxyBasicCredentials(const HTTPRequest& request);
/// Returns true if Authorization with Basic credentials header is present in the request.
/// Returns true if a Proxy-Authorization header with Basic credentials is present in the request.
static bool hasProxyDigestCredentials(const HTTPRequest& request);
/// Returns true if Authorization with Digest credentials header is present in the request.
/// Returns true if a Proxy-Authorization header with Digest credentials is present in the request.
static void extractCredentials(const std::string& userInfo, std::string& username, std::string& password);
/// Extracts username and password from user:password information string.
@ -185,6 +194,12 @@ inline const std::string& HTTPCredentials::getPassword() const
}
inline bool HTTPCredentials::empty() const
{
return _digest.empty();
}
} } // namespace Poco::Net

View File

@ -52,6 +52,10 @@ public:
void reset();
/// Resets the HTTPDigestCredentials object to a clean state.
/// Does not clear username and password.
void clear();
/// Clears both username and password.
void setUsername(const std::string& username);
/// Sets the username.
@ -65,6 +69,9 @@ public:
const std::string& getPassword() const;
/// Returns the password.
bool empty() const;
/// Returns true if both username and password are empty, otherwise false.
void authenticate(HTTPRequest& request, const HTTPResponse& response);
/// Parses WWW-Authenticate header of the HTTPResponse, initializes
/// internal state, and adds authentication information to the given HTTPRequest.
@ -167,6 +174,12 @@ inline const std::string& HTTPDigestCredentials::getPassword() const
}
inline bool HTTPDigestCredentials::empty() const
{
return _username.empty() && _password.empty();
}
} } // namespace Poco::Net

View File

@ -69,6 +69,13 @@ HTTPBasicCredentials::~HTTPBasicCredentials()
}
void HTTPBasicCredentials::clear()
{
_username.clear();
_password.clear();
}
void HTTPBasicCredentials::setUsername(const std::string& username)
{
_username = username;

View File

@ -121,6 +121,13 @@ void HTTPDigestCredentials::setPassword(const std::string& password)
}
void HTTPDigestCredentials::clear()
{
_username.clear();
_password.clear();
}
void HTTPDigestCredentials::authenticate(HTTPRequest& request, const HTTPResponse& response)
{
authenticate(request, HTTPAuthenticationParams(response));