Skip to content

Commit 7eecf8a

Browse files
committed
add generate access token of Qiniu type
1 parent 81c2a70 commit 7eecf8a

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

examples/atlab_check_qiniu_auth.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const qiniu = require("../index.js");
2+
const proc = require("process");
3+
4+
var accessKey = proc.env.QINIU_ACCESS_KEY;
5+
var secretKey = proc.env.QINIU_SECRET_KEY;
6+
7+
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
8+
var reqURL = "http://serve.atlab.ai/v1/eval/facex-detect";
9+
var contentType = 'application/json';
10+
var reqBody = '{"data":{"uri":"https://ors35x6a7.qnssl.com/atshow-face-detection-20170703/1.png"}}';
11+
var accessToken = qiniu.util.generateAccessTokenV2(mac, reqURL, 'POST', contentType, reqBody);
12+
var headers = {
13+
'Authorization': accessToken,
14+
'Content-Type': contentType,
15+
}
16+
17+
qiniu.rpc.post(reqURL, reqBody, headers, function(err, body, info) {
18+
console.log(info);
19+
console.log(body);
20+
});

index.d.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,22 @@ export declare namespace util {
331331
/**
332332
* 创建AccessToken凭证
333333
* @param mac AK&SK对象
334-
* @param requestURI 回调的URL中的requestURI 请求Body,仅当请求的ContentType为application/x-www-form-urlencoded时才需要传入该参数
335-
* @param reqBody
334+
* @param requestURI 请求URL
335+
* @param reqBody 请求Body,仅当请求的ContentType为application/x-www-form-urlencoded 时才需要传入该参数
336336
*/
337337
function generateAccessToken(mac: auth.digest.Mac, requestURI: string, reqBody?: string): string;
338338

339+
340+
/**
341+
* 创建AccessToken凭证
342+
* @param mac AK&SK对象
343+
* @param requestURI 请求URL
344+
* @param reqMethod 请求方法,例如 GET,POST
345+
* @param reqContentType 请求类型,例如 application/json 或者 application/x-www-form-urlencoded
346+
* @param reqBody 请求Body,仅当请求的 ContentType 为 application/json 或者 application/x-www-form-urlencoded 时才需要传入该参数
347+
*/
348+
function generateAccessTokenV2(mac: auth.digest.Mac, requestURI: string, reqMethod: string, reqContentType: string, reqBody?: string)
349+
339350
/**
340351
* 校验七牛上传回调的Authorization
341352
* @param mac AK&SK对象

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qiniu",
3-
"version": "7.0.8",
3+
"version": "7.0.9",
44
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API",
55
"main": "index.js",
66
"directories": {

qiniu/util.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ exports.hmacSha1 = function(encodedFlags, secretKey) {
6363
return hmac.digest('base64');
6464
}
6565

66-
// 创建AccessToken凭证
66+
// 创建 AccessToken 凭证
6767
// @param mac AK&SK对象
68-
// @param requestURI 回调的URL中的requestURI
69-
// @param reqBody 请求Body,仅当请求的ContentType为
68+
// @param requestURI 请求URL
69+
// @param reqBody 请求Body,仅当请求的 ContentType 为
7070
// application/x-www-form-urlencoded时才需要传入该参数
7171
exports.generateAccessToken = function(mac, requestURI, reqBody) {
7272
var u = url.parse(requestURI);
@@ -82,6 +82,50 @@ exports.generateAccessToken = function(mac, requestURI, reqBody) {
8282
return 'QBox ' + mac.accessKey + ':' + safeDigest;
8383
}
8484

85+
// 创建 AccessToken 凭证
86+
// @param mac AK&SK对象
87+
// @param requestURI 请求URL
88+
// @param reqMethod 请求方法,例如 GET,POST
89+
// @param reqContentType 请求类型,例如 application/json 或者 application/x-www-form-urlencoded
90+
// @param reqBody 请求Body,仅当请求的 ContentType 为 application/json 或者
91+
// application/x-www-form-urlencoded 时才需要传入该参数
92+
exports.generateAccessTokenV2 = function (mac, requestURI, reqMethod, reqContentType, reqBody) {
93+
var u = url.parse(requestURI);
94+
var path = u.path;
95+
var query = u.query;
96+
var host = u.host;
97+
var port = u.port;
98+
99+
var access = reqMethod.toUpperCase() + ' ' + path;
100+
if (query) {
101+
access += '?' + query;
102+
}
103+
// add host
104+
access += '\nHost: ' + host;
105+
// add port
106+
if (port) {
107+
access += ':' + port;
108+
}
109+
110+
// add content type
111+
if (reqContentType && (reqContentType=="application/json" || reqContentType=="application/x-www-form-urlencoded")) {
112+
access += '\nContent-Type: ' + reqContentType;
113+
}
114+
115+
access += '\n\n';
116+
117+
// add reqbody
118+
if (reqBody) {
119+
access += reqBody;
120+
}
121+
122+
console.log(access);
123+
124+
var digest = exports.hmacSha1(access, mac.secretKey);
125+
var safeDigest = exports.base64ToUrlSafe(digest);
126+
return 'Qiniu ' + mac.accessKey + ':' + safeDigest;
127+
}
128+
85129
// 校验七牛上传回调的Authorization
86130
// @param mac AK&SK对象
87131
// @param requestURI 回调的URL中的requestURI

0 commit comments

Comments
 (0)