Skip to content

Commit 9694319

Browse files
author
欧林宝
authored
first release
1 parent 5a5b7c6 commit 9694319

39 files changed

Lines changed: 3614 additions & 2 deletions

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea
2+
cmake-build-debug
3+
CMakeFiles
4+
*.a
5+
*.cbp
6+
*.cmake
7+
Demo
8+
Makefile
9+
.DS_Store
10+
CMakeCache.txt

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018 JDCLOUD.COM
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.5)
16+
project(Demo)
17+
18+
set(CMAKE_CXX_STANDARD 11)
19+
20+
include_directories(${PROJECT_SOURCE_DIR}/h)
21+
include_directories(${PROJECT_SOURCE_DIR}/http)
22+
include_directories(${PROJECT_SOURCE_DIR}/util)
23+
include_directories(${PROJECT_SOURCE_DIR}/util/crypto)
24+
include_directories(${PROJECT_SOURCE_DIR}/util/logging)
25+
add_subdirectory(src)
26+
27+
set(CMAKE_BUILD_TYPE "Release")
28+
29+
add_executable(Demo main.cpp)
30+
31+
target_link_libraries(Demo jdcloudsigner)

README.md

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,127 @@
1-
# jdcloud-sdk-cpp-signer
2-
C++ SDK签名库
1+
# 京东云 C++ SDK签名库
2+
## 基本说明
3+
京东云C++签名工具提供了C++语言访问京东云OpenAPI时的请求签名功能,它以AccessKey和SecretKey为素材,将HTTP请求的相关信息经过多次处理,再加上时间和nonce随机值对请求进行签名。使用本签名工具可以节省您编写签名过程的时间,没有正确签名,有可能会造成无法正常访问京东云OpenAPI。使用签名功能,可以保证您的身份不被冒充。请注意AK/SK的安全。
4+
5+
本签名工具使用C++11标准,以静态库的方式提供。使用的大致流程是:
6+
- 将依赖的头文件和静态库通过cmake工具引入您的项目
7+
- 将HTTP请求的信息填充到签名工具的 HttpRequest对象中
8+
- 调用签名接口
9+
- 把返回的HttpRequest对象中Header的Authorization、x-jdcloud-date、x-jdcloud-nonce三项及其值放到您的真实请求Header中
10+
- 然后向京东云OpenAPI网关发起调用
11+
12+
## Linux(Ubuntu)
13+
### 安装方法
14+
1) 安装开发依赖库
15+
```
16+
sudo apt-get install g++ cmake libssl-dev uuid-dev
17+
```
18+
2) 从GitHub下载Demo例子,地址为:https://github.com/jdcloud-api/jdcloud-sdk-cpp-signer
19+
20+
### 使用方法
21+
1) 新建项目工程目录
22+
2) 编写cmake文件,参考Demo中的例子,引用头文件
23+
```
24+
include_directories(${PROJECT_SOURCE_DIR}/h)
25+
include_directories(${PROJECT_SOURCE_DIR}/http)
26+
include_directories(${PROJECT_SOURCE_DIR}/util)
27+
include_directories(${PROJECT_SOURCE_DIR}/util/crypto)
28+
include_directories(${PROJECT_SOURCE_DIR}/util/logging)
29+
```
30+
3) 引用静态库
31+
```
32+
link_libraries(${PROJECT_SOURCE_DIR}/libjdcloudsigner.a)
33+
link_libraries(ssl)
34+
link_libraries(crypto)
35+
link_libraries(uuid)
36+
```
37+
4) 参考Demo中的main.cpp,调用签名接口.详细请见"调用方法"小节。
38+
5) 编译链接
39+
```
40+
cmake .
41+
make
42+
```
43+
44+
## macOS
45+
### 安装方法
46+
1) 安装cmake3.5以上版本
47+
```
48+
brew install cmake
49+
```
50+
2) 从GitHub下载Demo例子,地址为:https://github.com/jdcloud-api/jdcloud-sdk-cpp-signer
51+
52+
### 使用方法
53+
1) 新建项目工程目录
54+
2) 编写cmake文件,参考Demo中的例子,引用头文件
55+
```
56+
include_directories(${PROJECT_SOURCE_DIR}/h)
57+
include_directories(${PROJECT_SOURCE_DIR}/http)
58+
include_directories(${PROJECT_SOURCE_DIR}/util)
59+
include_directories(${PROJECT_SOURCE_DIR}/util/crypto)
60+
include_directories(${PROJECT_SOURCE_DIR}/util/logging)
61+
```
62+
3) 引用静态库(其中ssl库为mac系统自带,不用安装)
63+
```
64+
link_libraries(${PROJECT_SOURCE_DIR}/libjdcloudsigner.a)
65+
link_libraries(ssl)
66+
link_libraries(crypto)
67+
```
68+
4) 参考Demo中的main.cpp,调用签名接口。详细请见"调用方法"小节。
69+
5) 编译链接
70+
```
71+
cmake .
72+
make
73+
```
74+
## Windows
75+
### 安装方法
76+
1) Visual Stdio 2015以上版本,官方地址为:https://visualstudio.microsoft.com/
77+
2) CMake 3.5以上版本,官方地址为:https://cmake.org/
78+
3) 从GitHub下载Demo例子,地址为:https://github.com/jdcloud-api/jdcloud-sdk-cpp-signer
79+
80+
### 使用方法
81+
1) 新建项目工程目录
82+
2) 编写cmake文件,参考Demo中的例子,引用头文件
83+
```
84+
include_directories(${PROJECT_SOURCE_DIR}/h)
85+
include_directories(${PROJECT_SOURCE_DIR}/http)
86+
include_directories(${PROJECT_SOURCE_DIR}/util)
87+
include_directories(${PROJECT_SOURCE_DIR}/util/crypto)
88+
include_directories(${PROJECT_SOURCE_DIR}/util/logging)
89+
```
90+
3) 引用静态库
91+
```
92+
link_libraries(${PROJECT_SOURCE_DIR}/jdcloudsigner.lib)
93+
link_libraries(${PROJECT_SOURCE_DIR}/libeay32.lib)
94+
```
95+
4) 参考Demo中的main.cpp,调用签名接口。详细请见"调用方法"小节。
96+
5) 编译链接
97+
98+
打开Visual Studio 开发人员命令提示符,执行
99+
```
100+
cmake .
101+
devenv Demo.sln /build
102+
```
103+
104+
使用Visual Studio打开Demo.sln解决方案,编译。
105+
106+
## 调用方法
107+
```
108+
// 配置日志
109+
ConsoleLogSystem* cls = new ConsoleLogSystem(LogLevel::Debug);
110+
shared_ptr<ConsoleLogSystem> log(cls);
111+
InitializeLogging(log);
112+
113+
// 创建HttpRequest对象
114+
HttpRequest request(URI("YOUR URL"), HttpMethod::HTTP_GET);
115+
request.SetHeaderValue(CONTENT_TYPE_HEADER, "application/json");
116+
request.SetHeaderValue(USER_AGENT_HEADER, "JdcloudSdkGo/1.0.2 vm/0.7.4");
117+
118+
// 创建签名对象
119+
Credential credential("YOUR AK", "YOUR SK");
120+
JdcloudSigner signer(credential, "vm", "cn-north-1");
121+
122+
// 调用签名方法
123+
bool result = signer.SignRequest(request);
124+
125+
// 把Header中的三项 "Authorization、x-jdcloud-date、x-jdcloud-nonce" 放到真正的请求头中
126+
// 向京东云网关发起HTTP请求
127+
```

h/Credential.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2018 JDCLOUD.COM
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// NOTE: This file is modified from AWS V4 Signer algorithm.
16+
17+
#pragma once
18+
19+
#include <string>
20+
21+
using std::string;
22+
23+
class Credential
24+
{
25+
public:
26+
Credential(const string& accessKey, const string& secretKey):
27+
m_accessKey(accessKey), m_secretKey(secretKey)
28+
{}
29+
30+
inline const string& GetAccessKey() const
31+
{
32+
return m_accessKey;
33+
}
34+
35+
inline const string& GetSecretKey() const
36+
{
37+
return m_secretKey;
38+
}
39+
40+
private:
41+
std::string m_accessKey;
42+
std::string m_secretKey;
43+
};

h/JdcloudSigner.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018 JDCLOUD.COM
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// NOTE: This file is modified from AWS V4 Signer algorithm.
16+
17+
#pragma once
18+
19+
#include <string>
20+
#include <map>
21+
#include <set>
22+
#include <iostream>
23+
#include <sstream>
24+
#include <algorithm>
25+
#include "Credential.h"
26+
#include "util/crypto/Sha256.h"
27+
#include "util/crypto/Sha256HMAC.h"
28+
#include "util/DateTime.h"
29+
#include "http/HttpRequest.h"
30+
31+
using std::string;
32+
using std::map;
33+
using std::set;
34+
using std::iostream;
35+
using std::stringstream;
36+
using std::unique_ptr;
37+
38+
39+
class JdcloudSigner
40+
{
41+
public:
42+
JdcloudSigner(const Credential& credential, const string& serviceName, const string& region);
43+
44+
virtual ~JdcloudSigner();
45+
46+
bool SignRequest(HttpRequest& request) const;
47+
48+
private:
49+
bool ShouldSignHeader(const string& header) const;
50+
string GenerateSignature(const Credential& credentials, const string& stringToSign, const string& simpleDate) const;
51+
string GenerateSignature(const string& stringToSign, const string& key) const;
52+
string GenerateStringToSign(const string& dateValue, const string& simpleDate, const string& canonicalRequestHash,
53+
const string& region, const string& serviceName) const;
54+
55+
string ComputeHash(const string& secretKey, const string& simpleDate, const string& region,
56+
const string& serviceName) const;
57+
string ComputePayloadHash(HttpRequest& request) const;
58+
DateTime GetSigningTimestamp() const { return DateTime::Now(); }
59+
60+
Credential m_credential;
61+
string m_serviceName;
62+
string m_region;
63+
set<string> m_unsignedHeaders;
64+
unique_ptr<Sha256> m_hash;
65+
unique_ptr<Sha256HMAC> m_hmac;
66+
};
67+

0 commit comments

Comments
 (0)