refactor(rpc): GenerateAuthCookieResult -> AuthCookieResult

Type will be used for reading the cookie in next commit.

Also corrects ERR/ERROR mismatch in docstring in request.h, and changes to CamelCase to avoid potential collision with Windows headers (https://github.com/bitcoin/bitcoin/pull/34965#issuecomment-4161331392).
This commit is contained in:
Hodlinator
2026-03-31 10:08:18 +02:00
parent c97ac44c34
commit 84c3f8d325
3 changed files with 19 additions and 19 deletions

View File

@@ -264,12 +264,12 @@ static bool InitRPCAuthentication()
}
switch (GenerateAuthCookie(cookie_perms, user, pass)) {
case GenerateAuthCookieResult::ERR:
case AuthCookieResult::Error:
return false;
case GenerateAuthCookieResult::DISABLED:
case AuthCookieResult::Disabled:
LogInfo("RPC authentication cookie file generation is disabled.");
break;
case GenerateAuthCookieResult::OK:
case AuthCookieResult::Ok:
LogInfo("Using random cookie authentication.");
break;
}

View File

@@ -97,9 +97,9 @@ static fs::path GetAuthCookieFile(bool temp=false)
static bool g_generated_cookie = false;
GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
std::string& user,
std::string& pass)
AuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
std::string& user,
std::string& pass)
{
const size_t COOKIE_SIZE = 32;
unsigned char rand_pwd[COOKIE_SIZE];
@@ -112,12 +112,12 @@ GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cook
std::ofstream file;
fs::path filepath_tmp = GetAuthCookieFile(true);
if (filepath_tmp.empty()) {
return GenerateAuthCookieResult::DISABLED; // -norpccookiefile
return AuthCookieResult::Disabled; // -norpccookiefile
}
file.open(filepath_tmp.std_path());
if (!file.is_open()) {
LogWarning("Unable to open cookie authentication file %s for writing", fs::PathToString(filepath_tmp));
return GenerateAuthCookieResult::ERR;
return AuthCookieResult::Error;
}
file << COOKIEAUTH_USER << ":" << rand_pwd_hex;
file.close();
@@ -125,14 +125,14 @@ GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cook
fs::path filepath = GetAuthCookieFile(false);
if (!RenameOver(filepath_tmp, filepath)) {
LogWarning("Unable to rename cookie authentication file %s to %s", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
return GenerateAuthCookieResult::ERR;
return AuthCookieResult::Error;
}
if (cookie_perms) {
std::error_code code;
fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
if (code) {
LogWarning("Unable to set permissions on cookie authentication file %s", fs::PathToString(filepath));
return GenerateAuthCookieResult::ERR;
return AuthCookieResult::Error;
}
}
@@ -142,7 +142,7 @@ GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cook
user = COOKIEAUTH_USER;
pass = rand_pwd_hex;
return GenerateAuthCookieResult::OK;
return AuthCookieResult::Ok;
}
bool GetAuthCookie(std::string *cookie_out)

View File

@@ -23,10 +23,10 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
UniValue JSONRPCError(int code, const std::string& message);
enum class GenerateAuthCookieResult : uint8_t {
DISABLED, // -norpccookiefile
ERR,
OK,
enum class AuthCookieResult : uint8_t {
Disabled, // -norpccookiefile
Error,
Ok,
};
/**
@@ -34,11 +34,11 @@ enum class GenerateAuthCookieResult : uint8_t {
* @param[in] cookie_perms Filesystem permissions to use for the cookie file.
* @param[out] user Generated username, only set if `OK` is returned.
* @param[out] pass Generated password, only set if `OK` is returned.
* @retval GenerateAuthCookieResult::DISABLED Authentication via cookie is disabled.
* @retval GenerateAuthCookieResult::ERROR Error occurred, auth data could not be saved to disk.
* @retval GenerateAuthCookieResult::OK Auth data was generated, saved to disk and in `user` and `pass`.
* @retval AuthCookieResult::Disabled Authentication via cookie is disabled.
* @retval AuthCookieResult::Error Error occurred, auth data could not be saved to disk.
* @retval AuthCookieResult::Ok Auth data was generated, saved to disk and in `user` and `pass`.
*/
GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
AuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
std::string& user,
std::string& pass);