File: sign2.go

package info (click to toggle)
golang-github-smartystreets-go-aws-auth 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package awsauth

import (
	"encoding/base64"
	"net/http"
	"net/url"
	"strings"
)

func prepareRequestV2(request *http.Request, keys Credentials) *http.Request {

	keyID := keys.AccessKeyID

	values := url.Values{}
	values.Set("AWSAccessKeyId", keyID)
	values.Set("SignatureVersion", "2")
	values.Set("SignatureMethod", "HmacSHA256")
	values.Set("Timestamp", timestampV2())

	augmentRequestQuery(request, values)

	if request.URL.Path == "" {
		request.URL.Path += "/"
	}

	return request
}

func stringToSignV2(request *http.Request) string {
	str := request.Method + "\n"
	str += strings.ToLower(request.URL.Host) + "\n"
	str += request.URL.Path + "\n"
	str += canonicalQueryStringV2(request)
	return str
}

func signatureV2(strToSign string, keys Credentials) string {
	hashed := hmacSHA256([]byte(keys.SecretAccessKey), strToSign)
	return base64.StdEncoding.EncodeToString(hashed)
}

func canonicalQueryStringV2(request *http.Request) string {
	return request.URL.RawQuery
}

func timestampV2() string {
	return now().Format(timeFormatV2)
}

const timeFormatV2 = "2006-01-02T15:04:05"