You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 lines
3.4 KiB

  1. package s3
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "github.com/goamz/goamz/aws"
  7. "log"
  8. "sort"
  9. "strings"
  10. )
  11. var b64 = base64.StdEncoding
  12. // ----------------------------------------------------------------------------
  13. // S3 signing (http://goo.gl/G1LrK)
  14. var s3ParamsToSign = map[string]bool{
  15. "acl": true,
  16. "location": true,
  17. "logging": true,
  18. "notification": true,
  19. "partNumber": true,
  20. "policy": true,
  21. "requestPayment": true,
  22. "torrent": true,
  23. "uploadId": true,
  24. "uploads": true,
  25. "versionId": true,
  26. "versioning": true,
  27. "versions": true,
  28. "response-content-type": true,
  29. "response-content-language": true,
  30. "response-expires": true,
  31. "response-cache-control": true,
  32. "response-content-disposition": true,
  33. "response-content-encoding": true,
  34. "website": true,
  35. "delete": true,
  36. }
  37. type keySortableTupleList []keySortableTuple
  38. type keySortableTuple struct {
  39. Key string
  40. TupleString string
  41. }
  42. func (l keySortableTupleList) StringSlice() []string {
  43. slice := make([]string, len(l))
  44. for i, v := range l {
  45. slice[i] = v.TupleString
  46. }
  47. return slice
  48. }
  49. func (l keySortableTupleList) Len() int {
  50. return len(l)
  51. }
  52. func (l keySortableTupleList) Less(i, j int) bool {
  53. return l[i].Key < l[j].Key
  54. }
  55. func (l keySortableTupleList) Swap(i, j int) {
  56. l[i], l[j] = l[j], l[i]
  57. }
  58. func sign(auth aws.Auth, method, canonicalPath string, params, headers map[string][]string) {
  59. var md5, ctype, date, xamz string
  60. var xamzDate bool
  61. var sarray keySortableTupleList
  62. for k, v := range headers {
  63. k = strings.ToLower(k)
  64. switch k {
  65. case "content-md5":
  66. md5 = v[0]
  67. case "content-type":
  68. ctype = v[0]
  69. case "date":
  70. if !xamzDate {
  71. date = v[0]
  72. }
  73. default:
  74. if strings.HasPrefix(k, "x-amz-") {
  75. vall := strings.Join(v, ",")
  76. sarray = append(sarray, keySortableTuple{k, k + ":" + vall})
  77. if k == "x-amz-date" {
  78. xamzDate = true
  79. date = ""
  80. }
  81. }
  82. }
  83. }
  84. if len(sarray) > 0 {
  85. sort.Sort(sarray)
  86. xamz = strings.Join(sarray.StringSlice(), "\n") + "\n"
  87. }
  88. expires := false
  89. if v, ok := params["Expires"]; ok {
  90. // Query string request authentication alternative.
  91. expires = true
  92. date = v[0]
  93. params["AWSAccessKeyId"] = []string{auth.AccessKey}
  94. }
  95. sarray = sarray[0:0]
  96. for k, v := range params {
  97. if s3ParamsToSign[k] {
  98. for _, vi := range v {
  99. if vi == "" {
  100. sarray = append(sarray, keySortableTuple{k, k})
  101. } else {
  102. // "When signing you do not encode these values."
  103. sarray = append(sarray, keySortableTuple{k, k + "=" + vi})
  104. }
  105. }
  106. }
  107. }
  108. if len(sarray) > 0 {
  109. sort.Sort(sarray)
  110. canonicalPath = canonicalPath + "?" + strings.Join(sarray.StringSlice(), "&")
  111. }
  112. payload := method + "\n" + md5 + "\n" + ctype + "\n" + date + "\n" + xamz + canonicalPath
  113. hash := hmac.New(sha1.New, []byte(auth.SecretKey))
  114. hash.Write([]byte(payload))
  115. signature := make([]byte, b64.EncodedLen(hash.Size()))
  116. b64.Encode(signature, hash.Sum(nil))
  117. if expires {
  118. params["Signature"] = []string{string(signature)}
  119. } else {
  120. headers["Authorization"] = []string{"AWS " + auth.AccessKey + ":" + string(signature)}
  121. }
  122. if debug {
  123. log.Printf("Signature payload: %q", payload)
  124. log.Printf("Signature: %q", signature)
  125. }
  126. }