Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V7 func collectData(req *http.Request) (data []byte, err error) { 和文档上不符合啊 #37

Open
mojocn opened this issue Aug 5, 2021 · 3 comments

Comments

@mojocn
Copy link

mojocn commented Aug 5, 2021

https://github.com/qiniu/go-sdk/blob/master/auth/credentials.go

https://developer.qiniu.com/kodo/1201/access-token#1

signingStr=req.Method(HTTP协议的Method是大小写敏感的) + " "(空格) +req.Path
如果query为非空字符串(query不包含问号(?)字符)
signingStr=req.Method + " "(空格) +req.Path+?(英文问号)+req.query

接下来增加Host信息
signingStr=signingStr+\n(换行符)Host:(英文符号冒号) (空格)+req.Host

如果您设置了Content-Type的 Header,也需要添加
signingStr=signingStr+\n(换行符)Content-Type:(英文符号冒号) (空格)+req.Content-Type

对于七牛特殊的X-Qiniu-<key>头信息,如果有也需要添加, ”X-Qiniu-<key>“header是指在请求Header中以“X-Qiniu-”字符串开头的头部信息对,为七牛服务端理解。其中key不可为空字符。在生成签名算法中对key有一定的格式转换要求,第一个字母和连字符(-)后面的字母大写,其余字母都是小写。满足以上条件的键值对,根据<key>字符串 ASCII大小排序后,由小到大,依次加入待签名字符串
signingStr=signingStr+\n(换行符)+<key1>:(英文符号冒号) (空格)+<value1>+\n(换行符)+<key2>:(英文符号冒号) (空格)+<value2>+...

完成以上信息之后加入2个连续对换行符
signingStr=signingStr+\n(换行符)+\n(换行符)

如果您设置了请求Body,并且设置Content-Type不为"application/octet-stream"类型,Body也需要加入待签名字符串
signingStr=signingStr+<body>
@mojocn
Copy link
Author

mojocn commented Aug 5, 2021

golang v7 签名 data

func collectData(req *http.Request) (data []byte, err error) {
	u := req.URL
	s := u.Path
	if u.RawQuery != "" {
		s += "?"
		s += u.RawQuery
	}
	s += "\n"

	data = []byte(s)
	if incBody(req) {
		s2, rErr := api.BytesFromRequest(req)
		if rErr != nil {
			err = rErr
			return
		}
		req.Body = ioutil.NopCloser(bytes.NewReader(s2))
		data = append(data, s2...)
	}
	return
}

python 签名data

   def token_of_request(
            self,
            method,
            host,
            url,
            qheaders,
            content_type=None,
            body=None):
        """
        <Method> <PathWithRawQuery>
        Host: <Host>
        Content-Type: <ContentType>
        [<X-Qiniu-*> Headers]

        [<Body>] #这里的 <Body> 只有在 <ContentType> 存在且不为 application/octet-stream 时才签进去。

        """
        parsed_url = urlparse(url)
        netloc = parsed_url.netloc
        path = parsed_url.path
        query = parsed_url.query

        if not host:
            host = netloc

        path_with_query = path
        if query != '':
            path_with_query = ''.join([path_with_query, '?', query])
        data = ''.join(["%s %s" %
                        (method, path_with_query), "\n", "Host: %s" %
                        host, "\n"])

        if content_type:
            data += "Content-Type: %s" % (content_type) + "\n"

        data += qheaders
        data += "\n"

        if content_type and content_type != "application/octet-stream" and body:
            if isinstance(body, bytes):
                data += body.decode(encoding='UTF-8')
            else:
                data += body
        return '{0}:{1}'.format(self.__access_key, self.__token(data))

@mojocn
Copy link
Author

mojocn commented Aug 5, 2021

为什么golang v7 不带上 host签名啊

接下来增加Host信息
signingStr=signingStr+\n(换行符)Host:(英文符号冒号) (空格)+req.Host

@mojocn
Copy link
Author

mojocn commented Aug 5, 2021

我使用golang sdk 导致获取域名列表limit参数不生效啊. python的sdk是可以的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant