1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "SignUtils.h"
18 #include "Utils.h"
19 #include <sstream>
20 #include <map>
21 #include <set>
22 #include <alibabacloud/oss/Const.h>
23 #include <alibabacloud/oss/Types.h>
24 #include <alibabacloud/oss/http/HttpType.h>
25 
26 using namespace AlibabaCloud::OSS;
27 
28 const static std::set<std::string> ParamtersToSign =
29 {
30     "acl", "location", "bucketInfo", "stat", "referer", "cors", "website", "restore",
31     "logging", "symlink", "qos", "uploadId", "uploads", "partNumber",
32     "response-content-type", "response-content-language", "response-expires",
33     "response-cache-control", "response-content-disposition", "response-content-encoding",
34     "append", "position", "lifecycle", "delete", "live", "status", "comp", "vod",
35     "startTime", "endTime", "x-oss-process", "security-token", "objectMeta",
36     "callback", "callback-var", "tagging", "policy", "requestPayment", "x-oss-traffic-limit",
37     "encryption", "qosInfo", "versioning", "versionId", "versions",
38     "x-oss-request-payer", "sequential", "inventory", "inventoryId", "continuation-token"
39 };
40 
SignUtils(const std::string & version)41 SignUtils::SignUtils(const std::string &version):
42     signVersion_(version),
43     canonicalString_()
44 {
45 }
46 
~SignUtils()47 SignUtils::~SignUtils()
48 {
49 }
50 
CanonicalString() const51 const std::string &SignUtils::CanonicalString() const
52 {
53     return canonicalString_;
54 }
55 
build(const std::string & method,const std::string & resource,const std::string & date,const HeaderCollection & headers,const ParameterCollection & parameters)56 void SignUtils::build(const std::string &method,
57                       const std::string &resource,
58                       const std::string &date,
59                       const HeaderCollection &headers,
60                       const ParameterCollection &parameters)
61 {
62     std::stringstream ss;
63 
64     /*Version 1*/
65     // VERB + "\n" +
66     // Content-MD5 + "\n"  +
67     // Content-Type + "\n"
68     // Date + "\n"  +
69     // CanonicalizedOSSHeaders +
70     // CanonicalizedResource) +
71 
72     //common headers
73     ss << method << "\n";
74     if (headers.find(Http::CONTENT_MD5) != headers.end()) {
75         ss << headers.at(Http::CONTENT_MD5);
76     }
77     ss << "\n";
78     if (headers.find(Http::CONTENT_TYPE) != headers.end()) {
79         ss << headers.at(Http::CONTENT_TYPE);
80     }
81     ss << "\n";
82     //Date or EXPIRES
83     ss << date << "\n";
84 
85     //CanonicalizedOSSHeaders, start with x-oss-
86     for (const auto &header : headers) {
87         std::string lower = Trim(ToLower(header.first.c_str()).c_str());
88         if (lower.compare(0, 6, "x-oss-", 6) == 0) {
89             std::string value = Trim(header.second.c_str());
90             ss << lower << ":" << value << "\n";
91         }
92     }
93 
94     //CanonicalizedResource, the sub resouce in
95     ss << resource;
96     char separator = '?';
97     for (auto const& param : parameters) {
98         if (ParamtersToSign.find(param.first) == ParamtersToSign.end()) {
99             continue;
100         }
101 
102         ss << separator;
103         ss << param.first;
104         if (!param.second.empty()) {
105             ss << "=" << param.second;
106         }
107         separator = '&';
108     }
109 
110     canonicalString_ = ss.str();
111 }
112 
build(const std::string & expires,const std::string & resource,const ParameterCollection & parameters)113 void SignUtils::build(const std::string &expires,
114     const std::string &resource,
115     const ParameterCollection &parameters)
116 {
117     std::stringstream ss;
118     ss << expires << '\n';
119     for(auto const& param : parameters)
120     {
121         ss << param.first << ":" << param.second << '\n';
122     }
123     ss << resource;
124     canonicalString_ = ss.str();
125 }
126