-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_signature.html
304 lines (267 loc) · 9.41 KB
/
header_signature.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>华为对象存储 --生成Header鉴权签名</title>
</head>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/base.css">
<body>
<div class="container-fluid" style="margin-top: 30px;">
<div class="row">
<div class="col-lg-8">
<div class="well">
<h3>生成Header鉴权签名:</h3>
<div class="row">
<div class="col-xs-6">
<pre>
1. 计算StringToSign:
StringToSign = HTTP-Verb + "\n" +
Content-MD5 + "\n" +
Content-Type + "\n" +
Date + "\n" +
CanonicalizedHeaders +
CanonicalizedResource
</pre>
</div>
<div class="col-xs-6">
<pre>
2. 计算Signature:
Signature =
Base64(Hmac-sha1(SK, UTF-8-Encoding-Of(StringToSign)))
3. 计算Authorization
Authorization = "OBS " + AK + ":" + Signature
</pre>
</div>
</div>
</div>
</div>
<div class="col-lg-11">
<div id="postForm1" class="form-horizontal">
<div class="form-group">
<label for="ak" class="col-sm-1 control-label">AK</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="ak" placeholder="必选">
</div>
</div>
<div class="form-group">
<label for="sk" class="col-sm-1 control-label">SK</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="sk" placeholder="必选" >
</div>
</div>
<div class="form-group">
<label for="verb" class="col-sm-1 control-label">HTTP-Verb</label>
<div class="col-sm-6">
<select class="form-control" id="verb">
<option value="GET">GET</option>
<option value="PUT">PUT</option>
<option value="POST">POST</option>
<option value="DELETE">DELETE</option>
<option value="HEAD">HEAD</option>
<option value="OPTIONS">OPTIONS</option>
</select>
</div>
</div>
<div class="form-group">
<label for="md5" class="col-sm-1 control-label">Content-MD5</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="md5" placeholder="可选">
</div>
</div>
<div class="form-group">
<label for="ctype" class="col-sm-1 control-label">Content-Type</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="ctype" placeholder="可选">
</div>
</div>
<div class="form-group">
<label for="date" class="col-sm-1 control-label">Date</label>
<div class="col-sm-6">
<div class="input-group">
<input type="text" class="form-control" id="date" placeholder="可选, e.g Wed, 12 Dec 2018 08:58:38 GMT">
<span class="input-group-btn">
<button class="btn btn-info" type="button" onclick="createDate()">Now</button>
</span>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Canonicalized Headers</label>
<div class="col-sm-6">
<table class="table table-bordered">
<tbody id="headers">
<tr>
<td class="flex align-center justify-between">
<input type="text" class="form-control" name="headerKey" size="30" placeholder="可选,如果设置则必须以x-obs-开头"/>
:
<input type="text" class="form-control" name="headerValue" size="30" placeholder="可选"/></td>
<td>
<button size="auto" type="button" class="btn btn-primary" onclick="addHeader()">+</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="form-group">
<label for="resource" class="col-sm-1 control-label">Canonicalized Resource</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="resource" placeholder="必选且必须以/开头,e.g: /bucketname/objectkey?query_string" value="/">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-6">
<button type="button" class="btn btn-primary" onclick="generate()">生成Header鉴权签名</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
StringToSign:
<div id="stringToSign" class="well" style="overflow: auto">
</div>
<p>
</p>
Authorization:
<div id="authorization" class="well">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
<script src="./js/jquery.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<script src="./js/crypto-js.min.js"></script>
<script type="text/javascript">
function createDate(){
$('#date').val(new Date().toUTCString());
}
function addHeader(){
var tr = $('<tr></tr>')
tr.append('<td><input type="text" name="headerKey" size="30" placeholder="可选,如果设置则必须以x-obs-开头"/>:<input type="text" name="headerValue" size="30" placeholder="可选"/></td><td><div class="btn-group"><button class="btn btn-primary" onclick="addHeader()">+</button><button class="btn btn-danger" onclick="removeHeader(this)">-</button></div></td>');
tr.appendTo('#headers')
}
function removeHeader(item){
var $item = $(item);
$item.parent().parent().parent().remove();
}
function generate(){
var ak = $.trim($('#ak').val());
var sk = $.trim($('#sk').val());
var date = $.trim($('#date').val());
var resource = $.trim($('#resource').val());
if(ak === ''){
window.alert('AK输入有误!');
$('#ak').focus();
return;
}
if(sk === ''){
window.alert('SK输入有误!');
$('#sk').focus();
return;
}
var headerKeys = $('#headers input[name=headerKey]');
var headerValues = $('#headers input[name=headerValue]');
var index;
var keys = [];
var headers = {};
var obsData = '';
for(index=0;index<headerKeys.length;index++){
var input = $(headerKeys[index]);
var headerKey = $.trim(input.val());
if(headerKey === ''){
continue;
}
//转小写
headerKey = headerKey.toLowerCase();
if(headerKey.indexOf('x-obs-') !== 0){
window.alert('Canonicalized Headers输入有误!');
input.focus();
return;
}
if (headerKey === 'x-obs-date') {
obsData = $.trim($(headerValues[index]).val());
}
if (keys.includes(headerKey)) {
let value = headers[headerKey] + ",";
value += $.trim($(headerValues[index]).val());
headers[headerKey] = value;
} else {
keys.push(headerKey);
headers[headerKey] = $.trim($(headerValues[index]).val());
}
}
if (obsData) {
date = '';
}
if(!date && !obsData){
window.alert('Date输入有误!');
$('#date').focus();
return;
}
if(resource === '' || resource.indexOf('/') !== 0){
window.alert('Canonicalized Resource输入有误!');
$('#resource').focus();
return;
}
var stringToSign = [];
stringToSign.push($('#verb').val());
stringToSign.push($.trim($('#md5').val()));
stringToSign.push($.trim($('#ctype').val()));
stringToSign.push(date);
//排序
keys = keys.sort();
for(index=0;index<keys.length;index++){
var key = keys[index];
stringToSign.push(key + ':' + headers[key]);
}
var url = resource;
var query = '';
if((index=resource.indexOf('?')) > 0){
url = resource.slice(0, index);
query = resource.slice(index+1, resource.length).split('&');
var queryKeys = [];
var queries = {};
for(index=0;index<query.length;index++){
var q = query[index].split('=');
queryKeys.push(q[0]);
if(q.length == 2){
queries[q[0]] = q[1];
}else{
queries[q[0]] = '';
}
}
query = '?';
queryKeys = queryKeys.sort();
for(index=0;index<queryKeys.length;index++){
query += queryKeys[index];
if(queries[queryKeys[index]] !== ''){
query += '=' + queries[queryKeys[index]];
}
if(index !== queryKeys.length-1){
query += '&';
}
}
}
resource = url + query;
stringToSign.push(resource);
var _stringToSign = stringToSign.join('\n');
var stringToSignToShow = stringToSign.join('<br>');
var signature = CryptoJS.HmacSHA1(_stringToSign, sk).toString(CryptoJS.enc.Base64);
var authorization = 'OBS ' + ak + ':' + signature;
$('#stringToSign').html(stringToSignToShow);
$('#authorization').html(authorization);
$('#myModal').modal();
}
</script>
</html>