MCPcopy Create free account
hub / github.com/ElementsProject/elements / GenerateAuthCookie

Function GenerateAuthCookie

src/rpc/request.cpp:83–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81static bool g_generated_cookie = false;
82
83bool GenerateAuthCookie(std::string *cookie_out)
84{
85 const size_t COOKIE_SIZE = 32;
86 unsigned char rand_pwd[COOKIE_SIZE];
87 GetRandBytes(rand_pwd, COOKIE_SIZE);
88 std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
89
90 /** the umask determines what permissions are used to create this file -
91 * these are set to 077 in init.cpp unless overridden with -sysperms.
92 */
93 std::ofstream file;
94 fs::path filepath_tmp = GetAuthCookieFile(true);
95 file.open(filepath_tmp);
96 if (!file.is_open()) {
97 LogPrintf("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp));
98 return false;
99 }
100 file << cookie;
101 file.close();
102
103 fs::path filepath = GetAuthCookieFile(false);
104 if (!RenameOver(filepath_tmp, filepath)) {
105 LogPrintf("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
106 return false;
107 }
108 g_generated_cookie = true;
109 LogPrintf("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
110
111 if (cookie_out)
112 *cookie_out = cookie;
113 return true;
114}
115
116bool GetAuthCookie(std::string *cookie_out)
117{

Callers 1

InitRPCAuthenticationFunction · 0.85

Calls 7

GetRandBytesFunction · 0.85
GetAuthCookieFileFunction · 0.85
PathToStringFunction · 0.85
RenameOverFunction · 0.85
HexStrFunction · 0.50
openMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected