Exemplo n.º 1
0
func (client *Client) signRequest(req *request, payload []byte) {

	//	SignString = VERB + "\n"
	//	+ CONTENT-MD5 + "\n"
	//	+ CONTENT-TYPE + "\n"
	//	+ DATE + "\n"
	//	+ CanonicalizedLOGHeaders + "\n"
	//	+ CanonicalizedResource

	if _, ok := req.headers["Authorization"]; ok {
		return
	}

	contentMd5 := ""
	contentType := req.headers["Content-Type"]
	if req.payload != nil {
		hasher := md5.New()
		hasher.Write(payload)
		contentMd5 = strings.ToUpper(hex.EncodeToString(hasher.Sum(nil))) //string(md5.Sum(contentMd5))
		req.headers["Content-MD5"] = contentMd5

	}
	date := req.headers["Date"]
	//date := util.GetGMTime()
	canonicalizedHeader := canonicalizeHeader(req.headers)

	canonicalizedResource := canonicalizeResource(req)

	signString := req.method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedHeader + "\n" + canonicalizedResource

	signature := util.CreateSignature(signString, client.accessKeySecret)
	req.headers["Authorization"] = "LOG " + client.accessKeyId + ":" + signature
}
Exemplo n.º 2
0
func (client *Client) signRequest(request *request) {
	query := request.params

	urlSignature := query.Get("OSSAccessKeyId") != ""

	headers := request.headers
	contentMd5 := headers.Get("Content-Md5")
	contentType := headers.Get("Content-Type")
	date := ""
	if urlSignature {
		date = query.Get("Expires")
	} else {
		date = headers.Get("Date")
	}

	resource := request.path
	if request.bucket != "" {
		resource = "/" + request.bucket + request.path
	}
	params := make(url.Values)
	for k, v := range query {
		if ossParamsToSign[k] {
			params[k] = v
		}
	}

	if len(params) > 0 {
		resource = resource + "?" + util.Encode(params)
	}

	canonicalizedResource := resource

	_, canonicalizedHeader := canonicalizeHeader(headers)

	stringToSign := request.method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedHeader + canonicalizedResource

	//log.Println("stringToSign: ", stringToSign)
	signature := util.CreateSignature(stringToSign, client.AccessKeySecret)

	if query.Get("OSSAccessKeyId") != "" {
		query.Set("Signature", signature)
	} else {
		headers.Set("Authorization", "OSS "+client.AccessKeyId+":"+signature)
	}
}