@@ -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时才需要传入该参数
7171exports . 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