2015-08-10 10:02:34 +00:00
|
|
|
// v2 signing
|
|
|
|
|
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2024-08-03 10:35:32 +00:00
|
|
|
"context"
|
2015-08-10 10:02:34 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-08-03 10:35:32 +00:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
v4signer "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
|
2015-08-10 10:02:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// URL parameters that need to be added to the signature
|
|
|
|
var s3ParamsToSign = map[string]struct{}{
|
2023-05-14 10:25:52 +00:00
|
|
|
"delete": {},
|
2016-02-29 16:57:23 +00:00
|
|
|
"acl": {},
|
|
|
|
"location": {},
|
|
|
|
"logging": {},
|
|
|
|
"notification": {},
|
|
|
|
"partNumber": {},
|
|
|
|
"policy": {},
|
|
|
|
"requestPayment": {},
|
|
|
|
"torrent": {},
|
|
|
|
"uploadId": {},
|
|
|
|
"uploads": {},
|
|
|
|
"versionId": {},
|
|
|
|
"versioning": {},
|
|
|
|
"versions": {},
|
|
|
|
"response-content-type": {},
|
|
|
|
"response-content-language": {},
|
|
|
|
"response-expires": {},
|
|
|
|
"response-cache-control": {},
|
|
|
|
"response-content-disposition": {},
|
|
|
|
"response-content-encoding": {},
|
2015-08-10 10:02:34 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 10:35:32 +00:00
|
|
|
// Implement HTTPSignerV4 interface
|
|
|
|
type v2Signer struct {
|
|
|
|
opt *Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignHTTP signs requests using v2 auth.
|
|
|
|
//
|
|
|
|
// Cobbled together from goamz and aws-sdk-go.
|
2015-08-10 10:02:34 +00:00
|
|
|
//
|
2024-08-03 10:35:32 +00:00
|
|
|
// Bodged up to compile with AWS SDK v2
|
|
|
|
func (v2 *v2Signer) SignHTTP(ctx context.Context, credentials aws.Credentials, req *http.Request, payloadHash string, service string, region string, signingTime time.Time, optFns ...func(*v4signer.SignerOptions)) error {
|
2015-08-10 10:02:34 +00:00
|
|
|
// Set date
|
|
|
|
date := time.Now().UTC().Format(time.RFC1123)
|
|
|
|
req.Header.Set("Date", date)
|
|
|
|
|
|
|
|
// Sort out URI
|
2018-10-09 12:05:18 +00:00
|
|
|
uri := req.URL.EscapedPath()
|
2015-08-10 10:02:34 +00:00
|
|
|
if uri == "" {
|
|
|
|
uri = "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look through headers of interest
|
|
|
|
var md5 string
|
|
|
|
var contentType string
|
|
|
|
var headersToSign []string
|
2021-01-27 14:43:02 +00:00
|
|
|
tmpHeadersToSign := make(map[string][]string)
|
2015-08-10 10:02:34 +00:00
|
|
|
for k, v := range req.Header {
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
switch k {
|
|
|
|
case "content-md5":
|
|
|
|
md5 = v[0]
|
|
|
|
case "content-type":
|
|
|
|
contentType = v[0]
|
|
|
|
default:
|
|
|
|
if strings.HasPrefix(k, "x-amz-") {
|
2021-01-27 14:43:02 +00:00
|
|
|
tmpHeadersToSign[k] = v
|
2015-08-10 10:02:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 14:43:02 +00:00
|
|
|
var keys []string
|
|
|
|
for k := range tmpHeadersToSign {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
vall := strings.Join(tmpHeadersToSign[key], ",")
|
|
|
|
headersToSign = append(headersToSign, key+":"+vall)
|
|
|
|
}
|
2015-08-10 10:02:34 +00:00
|
|
|
// Make headers of interest into canonical string
|
|
|
|
var joinedHeadersToSign string
|
|
|
|
if len(headersToSign) > 0 {
|
|
|
|
joinedHeadersToSign = strings.Join(headersToSign, "\n") + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for query parameters which need to be added to the signature
|
|
|
|
params := req.URL.Query()
|
|
|
|
var queriesToSign []string
|
|
|
|
for k, vs := range params {
|
|
|
|
if _, ok := s3ParamsToSign[k]; ok {
|
|
|
|
for _, v := range vs {
|
|
|
|
if v == "" {
|
|
|
|
queriesToSign = append(queriesToSign, k)
|
|
|
|
} else {
|
|
|
|
queriesToSign = append(queriesToSign, k+"="+v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add query parameters to URI
|
|
|
|
if len(queriesToSign) > 0 {
|
|
|
|
sort.StringSlice(queriesToSign).Sort()
|
|
|
|
uri += "?" + strings.Join(queriesToSign, "&")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make signature
|
|
|
|
payload := req.Method + "\n" + md5 + "\n" + contentType + "\n" + date + "\n" + joinedHeadersToSign + uri
|
2024-08-03 10:35:32 +00:00
|
|
|
hash := hmac.New(sha1.New, []byte(v2.opt.SecretAccessKey))
|
2015-09-22 06:31:12 +00:00
|
|
|
_, _ = hash.Write([]byte(payload))
|
2015-08-10 10:02:34 +00:00
|
|
|
signature := make([]byte, base64.StdEncoding.EncodedLen(hash.Size()))
|
|
|
|
base64.StdEncoding.Encode(signature, hash.Sum(nil))
|
|
|
|
|
|
|
|
// Set signature in request
|
2024-08-03 10:35:32 +00:00
|
|
|
req.Header.Set("Authorization", "AWS "+v2.opt.AccessKeyID+":"+string(signature))
|
|
|
|
return nil
|
2015-08-10 10:02:34 +00:00
|
|
|
}
|