XRootD
Loading...
Searching...
No Matches
XrdClHttpTokenFileParser.cc
Go to the documentation of this file.
2
4#include "XrdCl/XrdClLog.hh"
5
6#include "XrdOuc/XrdOucJson.hh"
7
8#include <fstream>
9#include <sstream>
10
11using namespace std::string_literals;
12
13bool XrdClHttp::ParseTokenFile( const std::string &token_file,
14 CurlCopyOp::Headers &src_hdrs,
15 CurlCopyOp::Headers &dst_hdrs )
16{
18
19 std::ifstream file(token_file);
20
21 if (!file.is_open())
22 {
23 log->Warning(kLogXrdClHttp, "Failed to open token file");
24 return false;
25 }
26
27 constexpr std::streamsize max_token_file_size = 10 * 1024;
28
29 std::string content(max_token_file_size, '\0');
30 file.read(&content[0], max_token_file_size);
31 content.resize(file.gcount());
32
33 std::string src_token;
34 std::string dst_token;
35
36 if (const auto json = nlohmann::json::parse(content, nullptr, false); json.is_object())
37 {
38 const auto read_token = [&json, log] (const char *key, std::string &token)
39 {
40 if (const auto value = json.find(key); value != json.end() && value->is_string() && !value->get_ref<const std::string &>().empty())
41 token = value->get_ref<const std::string &>();
42 else
43 log->Warning(kLogXrdClHttp, "Property '%s' of the token file is not a non-empty string", key);
44 };
45
46 read_token("src", src_token);
47 read_token("dst", dst_token);
48 }
49 else
50 {
51 std::istringstream lines(content);
52 std::getline(lines, src_token);
53 std::getline(lines, dst_token);
54 }
55
56 if (src_token.empty() && dst_token.empty())
57 {
58 log->Warning(kLogXrdClHttp, "Token file holds neither a <src> nor a <dst> token");
59 return false;
60 }
61
62 const auto add_token = [] (CurlCopyOp::Headers &headers, const std::string &token)
63 {
64 if (!token.empty())
65 headers.emplace_back("Authorization"s, "Bearer "s + token);
66 };
67
68 add_token(src_hdrs, src_token);
69 add_token(dst_hdrs, dst_token);
70
71 return true;
72}
nlohmann::json json
std::vector< std::pair< std::string, std::string > > Headers
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition XrdClLog.hh:101
void Warning(uint64_t topic, const char *format,...)
Report a warning.
Definition XrdClLog.cc:248
const uint64_t kLogXrdClHttp
bool ParseTokenFile(const std::string &token_file, CurlCopyOp::Headers &src_hdrs, CurlCopyOp::Headers &dst_hdrs)