userver: /data/code/userver/libraries/s3api/src/s3api/authenticators/access_key.cpp Source File
Loading...
Searching...
No Matches
access_key.cpp
1#include <userver/s3api/authenticators/access_key.hpp>
2
3#include <userver/s3api/authenticators/utils.hpp>
4
5#include <optional>
6#include <unordered_map>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace s3api::authenticators {
11
12std::unordered_map<std::string, std::string> AccessKey::Auth(const Request& request) const {
13 // https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication.html
14 // without url encoding
15
16 static const std::string kDate{"Date"};
17 static const std::string kContentMd5{"Content-MD5"};
18 static const std::string kAuthorization{"Authorization"};
19
20 std::optional<std::string> header_content_md5;
21
22 if (!request.body.empty()) {
23 header_content_md5 = MakeHeaderContentMd5(request.body);
24 }
25
26 auto header_date = MakeHeaderDate();
27 auto string_to_sign = MakeStringToSign(request, header_date, header_content_md5);
28
29 std::unordered_map<std::string, std::string> auth_headers{
30 {kDate, std::move(header_date)},
31 {kAuthorization, MakeHeaderAuthorization(string_to_sign, access_key_, secret_key_)}};
32
33 if (header_content_md5) {
34 auth_headers.emplace(kContentMd5, std::move(*header_content_md5));
35 }
36
37 return auth_headers;
38}
39
40std::unordered_map<std::string, std::string> AccessKey::Sign(const Request& request, time_t expires) const {
41 static const std::string kExpires{"Expires"};
42 static const std::string kSignature{"Signature"};
43 static const std::string kAWSAccessKeyId{"AWSAccessKeyId"};
44
45 const auto param_expires = std::to_string(expires);
46 const auto string_to_sign = MakeStringToSign(request, param_expires, std::nullopt);
47
48 std::unordered_map<std::string, std::string> sign_params{
49 {kExpires, std::move(param_expires)},
50 {kAWSAccessKeyId, access_key_},
51 {kSignature, MakeSignature(string_to_sign, secret_key_)}};
52 return sign_params;
53}
54
55} // namespace s3api::authenticators
56
57USERVER_NAMESPACE_END