Skip to content

Commit 3b2eee9

Browse files
committed
add X-Qiniu-Date
1 parent 7b16a73 commit 3b2eee9

File tree

8 files changed

+580
-76
lines changed

8 files changed

+580
-76
lines changed

index.d.ts

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

88
export declare namespace auth {
99
namespace digest {
10+
interface MacOptions {
11+
disableQiniuTimestampSignature?: boolean;
12+
}
13+
1014
class Mac {
1115
accessKey: string;
1216
secretKey: string;
1317

14-
constructor(accessKey?: string, secretKey?: string);
18+
constructor(accessKey?: string, secretKey?: string, options?: MacOptions);
1519
}
1620
}
1721
}
@@ -348,6 +352,21 @@ export declare namespace resume_up {
348352
export declare namespace util {
349353
function isTimestampExpired(timestamp: number): boolean;
350354

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

353372
function getAKFromUptoken(uploadToken: string): string;
@@ -397,11 +416,16 @@ export declare namespace util {
397416
}
398417

399418
export declare namespace rpc {
400-
interface Headers {
419+
type Headers = Record<string, string> & {
401420
'User-Agent'?: string;
402421
Connection?: string;
403422
}
404423

424+
interface RequestOptions {
425+
headers: Headers;
426+
mac: auth.digest.Mac;
427+
}
428+
405429
/**
406430
*
407431
* @param requestUrl 请求地址
@@ -410,6 +434,17 @@ export declare namespace rpc {
410434
*/
411435
function get(requestUrl: string, headers: Headers | null, callbackFunc: callback): void;
412436

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

465+
466+
/**
467+
* @param requestUrl 请求地址
468+
* @param requestForm 请求体
469+
* @param options 请求的配置
470+
* @param callbackFunc 回调函数
471+
*/
472+
function postWithOptions(
473+
requestUrl: string,
474+
requestForm: Buffer | string | NodeJS.ReadableStream | null,
475+
options: RequestOptions | null,
476+
callbackFunc: callback
477+
): ReturnType<typeof post>;
478+
430479
/**
431480
*
432481
* @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)