Skip to content

Commit

Permalink
[RPC] pass HTTP basic authentication username to the JSONRequest object
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschnelli committed Oct 19, 2016
1 parent 69d1c25 commit e7156ad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static bool multiUserAuthorized(std::string strUserPass)
return false;
}

static bool RPCAuthorized(const std::string& strAuth)
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
{
if (strRPCUserColonPass.empty()) // Belt-and-suspenders measure if InitRPCAuthentication was not called
return false;
Expand All @@ -136,7 +136,10 @@ static bool RPCAuthorized(const std::string& strAuth)
std::string strUserPass64 = strAuth.substr(6);
boost::trim(strUserPass64);
std::string strUserPass = DecodeBase64(strUserPass64);


if (strUserPass.find(":") != std::string::npos)
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(":"));

//Check if authorized under single-user field
if (TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
return true;
Expand All @@ -159,7 +162,8 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
return false;
}

if (!RPCAuthorized(authHeader.second)) {
JSONRPCRequest jreq;
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", req->GetPeer().ToString());

/* Deter brute-forcing
Expand All @@ -172,7 +176,6 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
return false;
}

JSONRPCRequest jreq;
try {
// Parse request
UniValue valRequest;
Expand Down
1 change: 1 addition & 0 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
switch (rf) {
case RF_JSON: {
JSONRPCRequest jsonRequest;
jsonRequest.params = UniValue(UniValue::VARR);
UniValue chainInfoObject = getblockchaininfo(jsonRequest);
string strJSON = chainInfoObject.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class JSONRPCRequest
UniValue params;
bool fHelp;
std::string URI;
std::string authUser;

JSONRPCRequest() { id = NullUniValue; }
JSONRPCRequest() { id = NullUniValue; params = NullUniValue; fHelp = false; }
void parse(const UniValue& valRequest);
};

Expand Down

0 comments on commit e7156ad

Please sign in to comment.