Skip to content

Commit

Permalink
Added HTTP*Credentials::empty() and HTTPCredentials::clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Jun 24, 2019
1 parent 670e7b8 commit 8dc9370
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 32 deletions.
24 changes: 18 additions & 6 deletions Net/include/Poco/Net/HTTPBasicCredentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Net_API HTTPBasicCredentials
public:
HTTPBasicCredentials();
/// Creates an empty HTTPBasicCredentials object.

HTTPBasicCredentials(const std::string& username, const std::string& password);
/// Creates a HTTPBasicCredentials object with the given username and password.

Expand All @@ -55,18 +55,24 @@ class Net_API HTTPBasicCredentials
~HTTPBasicCredentials();
/// Destroys the HTTPBasicCredentials.

void clear();
/// Clears both username and password.

void setUsername(const std::string& username);
/// Sets the username.

const std::string& getUsername() const;
/// Returns the username.

void setPassword(const std::string& password);
/// Sets the password.

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.

Expand All @@ -84,7 +90,7 @@ class Net_API HTTPBasicCredentials
private:
HTTPBasicCredentials(const HTTPBasicCredentials&);
HTTPBasicCredentials& operator = (const HTTPBasicCredentials&);

std::string _username;
std::string _password;
};
Expand All @@ -105,6 +111,12 @@ inline const std::string& HTTPBasicCredentials::getPassword() const
}


inline bool HTTPBasicCredentials::empty() const
{
return _username.empty() && _password.empty();
}


} } // namespace Poco::Net


Expand Down
25 changes: 20 additions & 5 deletions Net/include/Poco/Net/HTTPCredentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +90,9 @@ class Net_API HTTPCredentials
/// 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.

Expand All @@ -102,6 +105,9 @@ class Net_API HTTPCredentials
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
Expand Down Expand Up @@ -133,16 +139,19 @@ class Net_API HTTPCredentials
/// 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.
Expand Down Expand Up @@ -185,6 +194,12 @@ inline const std::string& HTTPCredentials::getPassword() const
}


inline bool HTTPCredentials::empty() const
{
return _digest.empty();
}


} } // namespace Poco::Net


Expand Down
19 changes: 16 additions & 3 deletions Net/include/Poco/Net/HTTPDigestCredentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Net_API HTTPDigestCredentials

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.
Expand All @@ -65,6 +69,9 @@ class Net_API HTTPDigestCredentials
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.
Expand Down Expand Up @@ -110,7 +117,7 @@ class Net_API HTTPDigestCredentials

bool verifyAuthParams(const HTTPRequest& request, const HTTPAuthenticationParams& params) const;
/// Verifies the digest authentication information in the given HTTPRequest
/// and HTTPAuthenticationParams by recomputing the response and comparing
/// and HTTPAuthenticationParams by recomputing the response and comparing
/// it with what's in the request.

static std::string createNonce();
Expand All @@ -125,7 +132,7 @@ class Net_API HTTPDigestCredentials
void createAuthParams(const HTTPRequest& request, const HTTPAuthenticationParams& responseAuthParams);
void updateAuthParams(const HTTPRequest& request);
int updateNonceCounter(const std::string& nonce);

static const std::string DEFAULT_ALGORITHM;
static const std::string DEFAULT_QOP;
static const std::string NONCE_PARAM;
Expand All @@ -146,7 +153,7 @@ class Net_API HTTPDigestCredentials
std::string _password;
HTTPAuthenticationParams _requestAuthParams;
NonceCounterMap _nc;

static int _nonceCounter;
static Poco::FastMutex _nonceMutex;
};
Expand All @@ -167,6 +174,12 @@ inline const std::string& HTTPDigestCredentials::getPassword() const
}


inline bool HTTPDigestCredentials::empty() const
{
return _username.empty() && _password.empty();
}


} } // namespace Poco::Net


Expand Down
17 changes: 12 additions & 5 deletions Net/src/HTTPBasicCredentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ HTTPBasicCredentials::HTTPBasicCredentials()
{
}


HTTPBasicCredentials::HTTPBasicCredentials(const std::string& username, const std::string& password):
_username(username),
_password(password)
Expand Down Expand Up @@ -69,18 +69,25 @@ HTTPBasicCredentials::~HTTPBasicCredentials()
}


void HTTPBasicCredentials::clear()
{
_username.clear();
_password.clear();
}


void HTTPBasicCredentials::setUsername(const std::string& username)
{
_username = username;
}


void HTTPBasicCredentials::setPassword(const std::string& password)
{
_password = password;
}


void HTTPBasicCredentials::authenticate(HTTPRequest& request) const
{
std::ostringstream ostr;
Expand Down
33 changes: 20 additions & 13 deletions Net/src/HTTPDigestCredentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ namespace
engine.update(a);
engine.update(':');
engine.update(b);
if (!c.empty())
if (!c.empty())
{
engine.update(':');
engine.update(c);
if (!d.empty())
if (!d.empty())
{
engine.update(':');
engine.update(d);
Expand All @@ -55,7 +55,7 @@ namespace
}
return Poco::DigestEngine::digestToHex(engine.digest());
}

std::string formatNonceCounter(int counter)
{
return Poco::NumberFormatter::formatHex(counter, 8);
Expand Down Expand Up @@ -89,7 +89,7 @@ HTTPDigestCredentials::HTTPDigestCredentials()
{
}


HTTPDigestCredentials::HTTPDigestCredentials(const std::string& username, const std::string& password):
_username(username),
_password(password)
Expand All @@ -113,14 +113,21 @@ void HTTPDigestCredentials::setUsername(const std::string& username)
{
_username = username;
}


void HTTPDigestCredentials::setPassword(const std::string& password)
{
_password = password;
}


void HTTPDigestCredentials::clear()
{
_username.clear();
_password.clear();
}


void HTTPDigestCredentials::authenticate(HTTPRequest& request, const HTTPResponse& response)
{
authenticate(request, HTTPAuthenticationParams(response));
Expand Down Expand Up @@ -186,7 +193,7 @@ void HTTPDigestCredentials::createAuthParams(const HTTPRequest& request, const H

const std::string& algorithm = responseAuthParams.get(ALGORITHM_PARAM, DEFAULT_ALGORITHM);

if (icompare(algorithm, DEFAULT_ALGORITHM) != 0)
if (icompare(algorithm, DEFAULT_ALGORITHM) != 0)
throw NotImplementedException("Unsupported digest algorithm", algorithm);

const std::string& nonce = responseAuthParams.get(NONCE_PARAM);
Expand All @@ -197,15 +204,15 @@ void HTTPDigestCredentials::createAuthParams(const HTTPRequest& request, const H
_requestAuthParams.set(USERNAME_PARAM, _username);
_requestAuthParams.set(NONCE_PARAM, nonce);
_requestAuthParams.setRealm(realm);
if (responseAuthParams.has(OPAQUE_PARAM))
if (responseAuthParams.has(OPAQUE_PARAM))
{
_requestAuthParams.set(OPAQUE_PARAM, responseAuthParams.get(OPAQUE_PARAM));
}

if (qop.empty())
{
updateAuthParams(request);
}
}
else
{
Poco::StringTokenizer tok(qop, ",", Poco::StringTokenizer::TOK_TRIM);
Expand All @@ -221,9 +228,9 @@ void HTTPDigestCredentials::createAuthParams(const HTTPRequest& request, const H
break;
}
}
if (!qopSupported)
if (!qopSupported)
throw NotImplementedException("Unsupported QoP requested", qop);
}
}
}


Expand All @@ -243,7 +250,7 @@ void HTTPDigestCredentials::updateAuthParams(const HTTPRequest& request)

_requestAuthParams.set(RESPONSE_PARAM, digest(engine, ha1, nonce, ha2));
}
else if (icompare(qop, AUTH_PARAM) == 0)
else if (icompare(qop, AUTH_PARAM) == 0)
{
const std::string& cnonce = _requestAuthParams.get(CNONCE_PARAM);

Expand Down Expand Up @@ -277,7 +284,7 @@ bool HTTPDigestCredentials::verifyAuthParams(const HTTPRequest& request, const H
const std::string ha2 = digest(engine, request.getMethod(), request.getURI());
response = digest(engine, ha1, nonce, ha2);
}
else if (icompare(qop, AUTH_PARAM) == 0)
else if (icompare(qop, AUTH_PARAM) == 0)
{
const std::string& cnonce = params.get(CNONCE_PARAM);
const std::string& nc = params.get(NC_PARAM);
Expand All @@ -293,7 +300,7 @@ int HTTPDigestCredentials::updateNonceCounter(const std::string& nonce)
{
NonceCounterMap::iterator iter = _nc.find(nonce);

if (iter == _nc.end())
if (iter == _nc.end())
{
iter = _nc.insert(NonceCounterMap::value_type(nonce, 0)).first;
}
Expand Down

0 comments on commit 8dc9370

Please sign in to comment.