userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
credentials.cpp
1
#
include
"credentials.hpp"
2
3
#
include
<
stdexcept
>
4
#
include
<
string
>
5
#
include
<
utility
>
6
7
#
include
<
userver
/
formats
/
json
/
serialize
.
hpp
>
8
#
include
<
userver
/
formats
/
json
/
value
.
hpp
>
9
#
include
<
userver
/
fs
/
blocking
/
read
.
hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
s3api::cli {
14
15
namespace
{
16
constexpr
auto
kInvalidCredentials =
17
"Credentials must be JSON with nonempty string fields access_key_id and "
18
"secret_key"
;
19
}
20
21
Credentials
ParseCredentials(
const
std::string& contents) {
22
try
{
23
const
auto
json = formats
::
json
::
FromString
(
contents
)
;
24
auto
access_key = json
[
"access_key_id"
]
.
As
<
std
::
string
>
(
)
;
25
auto
secret_key = json
[
"secret_key"
]
.
As
<
std
::
string
>
(
)
;
26
if
(access_key.empty() || secret_key.empty()) {
27
throw
std::invalid_argument(kInvalidCredentials);
28
}
29
return
{std::move(access_key), Secret{std::move(secret_key)}};
30
}
catch
(
const
std::exception&) {
31
throw
std::invalid_argument(kInvalidCredentials);
32
}
33
}
34
35
Credentials
ReadCredentials(
const
std::string& path) {
36
std::string contents;
37
try
{
38
contents = fs
::
blocking
::
ReadFileContents
(
path
)
;
39
}
catch
(
const
std::exception& error) {
40
throw
std::invalid_argument(
41
std::string{
"Cannot read --credentials-file; check the path and permissions: "
} + error.what()
42
);
43
}
44
return
ParseCredentials(contents);
45
}
46
47
}
// namespace s3api::cli
48
49
USERVER_NAMESPACE_END
tools
cli
credentials.cpp
Generated on
for userver by
Doxygen
1.17.0