Skip to content

Commit 44a686f

Browse files
authored
Merge pull request #396 from qiniu/features/add-x-qiniu-date
add X-Qiniu-Date and fix bucket lifecycle tests
2 parents df49490 + 0f4d891 commit 44a686f

File tree

8 files changed

+582
-78
lines changed

8 files changed

+582
-78
lines changed

index.d.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ export declare type callback = (e?: Error, respBody?: any, respInfo?: any) => vo
99

1010
export declare namespace auth {
1111
namespace digest {
12+
interface MacOptions {
13+
disableQiniuTimestampSignature?: boolean;
14+
}
15+
1216
class Mac {
1317
accessKey: string;
1418
secretKey: string;
1519

16-
constructor(accessKey?: string, secretKey?: string);
20+
constructor(accessKey?: string, secretKey?: string, options?: MacOptions);
1721
}
1822
}
1923
}
@@ -350,6 +354,21 @@ export declare namespace resume_up {
350354
export declare namespace util {
351355
function isTimestampExpired(timestamp: number): boolean;
352356

357+
/**
358+
* 使用 UTC 时间来格式化日期时间
359+
*
360+
* @param date 与 new Date() 接受的参数一样,内部会使用 new Date(date) 生成日期时间对象
361+
* @param layout 目前仅接受
362+
* YYYY
363+
* MM
364+
* DD
365+
* HH
366+
* mm
367+
* ss
368+
* SSS
369+
*/
370+
function formatDateUTC(date: Date | number | string, layout?: string): string;
371+
353372
function encodedEntry(bucket: string, key?: string): string;
354373

355374
function getAKFromUptoken(uploadToken: string): string;
@@ -399,11 +418,16 @@ export declare namespace util {
399418
}
400419

401420
export declare namespace rpc {
402-
interface Headers {
421+
type Headers = Record<string, string> & {
403422
'User-Agent'?: string;
404423
Connection?: string;
405424
}
406425

426+
interface RequestOptions {
427+
headers: Headers;
428+
mac: auth.digest.Mac;
429+
}
430+
407431
/**
408432
*
409433
* @param requestUrl 请求地址
@@ -412,6 +436,17 @@ export declare namespace rpc {
412436
*/
413437
function get(requestUrl: string, headers: Headers | null, callbackFunc: callback): void;
414438

439+
/**
440+
* @param requestUrl 请求地址
441+
* @param options 请求的配置
442+
* @param callbackFunc 回调函数
443+
*/
444+
function getWithOptions(
445+
requestUrl: string,
446+
options: RequestOptions | null,
447+
callbackFunc: callback
448+
): ReturnType<typeof get>;
449+
415450
/**
416451
*
417452
* @param requestUrl 请求地址
@@ -429,6 +464,20 @@ export declare namespace rpc {
429464
*/
430465
function post(requestURI: string, requestForm: Buffer | string | NodeJS.ReadableStream | null, headers: Headers | null, callback: callback): void;
431466

467+
468+
/**
469+
* @param requestUrl 请求地址
470+
* @param requestForm 请求体
471+
* @param options 请求的配置
472+
* @param callbackFunc 回调函数
473+
*/
474+
function postWithOptions(
475+
requestUrl: string,
476+
requestForm: Buffer | string | NodeJS.ReadableStream | null,
477+
options: RequestOptions | null,
478+
callbackFunc: callback
479+
): ReturnType<typeof post>;
480+
432481
/**
433482
*
434483
* @param requestURI

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"encodeurl": "^1.0.1",
5858
"formstream": "^1.1.0",
5959
"mime": "^2.4.4",
60+
"mockdate": "^3.0.5",
6061
"tunnel-agent": "^0.6.0",
6162
"urllib": "^2.34.1"
6263
},

qiniu/auth/digest.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ var conf = require('../conf');
22

33
exports.Mac = Mac;
44

5-
function Mac (accessKey, secretKey) {
5+
const defaultOptions = {
6+
disableQiniuTimestampSignature: null
7+
};
8+
9+
function Mac (accessKey, secretKey, options) {
610
this.accessKey = accessKey || conf.ACCESS_KEY;
711
this.secretKey = secretKey || conf.SECRET_KEY;
12+
this.options = Object.assign({}, defaultOptions, options);
813
}

qiniu/rpc.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,61 @@
11
var urllib = require('urllib');
22
var conf = require('./conf');
3+
const digest = require('./auth/digest');
4+
const util = require('./util');
35

46
exports.get = get;
57
exports.post = post;
68
exports.put = put;
9+
exports.getWithOptions = getWithOptions;
710
exports.getWithToken = getWithToken;
11+
exports.postWithOptions = postWithOptions;
812
exports.postMultipart = postMultipart;
913
exports.postWithForm = postWithForm;
1014
exports.postWithoutForm = postWithoutForm;
1115

16+
function addAuthHeaders (headers, mac) {
17+
const xQiniuDate = util.formatDateUTC(new Date(), 'YYYYMMDDTHHmmssZ');
18+
if (mac.options.disableQiniuTimestampSignature !== null) {
19+
if (!mac.options.disableQiniuTimestampSignature) {
20+
headers['X-Qiniu-Date'] = xQiniuDate;
21+
}
22+
} else if (process.env.DISABLE_QINIU_TIMESTAMP_SIGNATURE) {
23+
if (process.env.DISABLE_QINIU_TIMESTAMP_SIGNATURE.toLowerCase() !== 'true') {
24+
headers['X-Qiniu-Date'] = xQiniuDate;
25+
}
26+
} else {
27+
headers['X-Qiniu-Date'] = xQiniuDate;
28+
}
29+
return headers;
30+
}
31+
32+
function getWithOptions (requestURI, options, callbackFunc) {
33+
let headers = options.headers || {};
34+
const mac = options.mac || new digest.Mac();
35+
36+
if (!headers['Content-Type']) {
37+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
38+
}
39+
40+
headers = addAuthHeaders(headers, mac);
41+
42+
// if there are V3, V4 token generator in the future, extends options with signVersion
43+
const token = util.generateAccessTokenV2(
44+
mac,
45+
requestURI,
46+
'GET',
47+
headers['Content-Type'],
48+
null,
49+
headers
50+
);
51+
52+
if (mac.accessKey) {
53+
headers.Authorization = token;
54+
}
55+
56+
return get(requestURI, headers, callbackFunc);
57+
}
58+
1259
function getWithToken (requestUrl, token, callbackFunc) {
1360
const headers = {
1461
'Content-Type': 'application/x-www-form-urlencoded'
@@ -19,6 +66,33 @@ function getWithToken (requestUrl, token, callbackFunc) {
1966
return get(requestUrl, headers, callbackFunc);
2067
}
2168

69+
function postWithOptions (requestURI, requestForm, options, callbackFunc) {
70+
let headers = options.headers || {};
71+
const mac = options.mac || new digest.Mac();
72+
73+
if (!headers['Content-Type']) {
74+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
75+
}
76+
77+
headers = addAuthHeaders(headers, mac);
78+
79+
// if there are V3, V4 token generator in the future, extends options with signVersion
80+
const token = util.generateAccessTokenV2(
81+
mac,
82+
requestURI,
83+
'POST',
84+
headers['Content-Type'],
85+
requestForm,
86+
headers
87+
);
88+
89+
if (mac.accessKey) {
90+
headers.Authorization = token;
91+
}
92+
93+
return post(requestURI, requestForm, headers, callbackFunc);
94+
}
95+
2296
function postMultipart(requestURI, requestForm, callbackFunc) {
2397
return post(requestURI, requestForm, requestForm.headers(), callbackFunc);
2498
}

0 commit comments

Comments
 (0)