auth.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package oss
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "encoding/base64"
  8. "fmt"
  9. "hash"
  10. "io"
  11. "net/http"
  12. "sort"
  13. "strings"
  14. )
  15. // headerSorter defines the key-value structure for storing the sorted data in signHeader.
  16. type headerSorter struct {
  17. Keys []string
  18. Vals []string
  19. }
  20. func (conn Conn) getAdditionalHeaderKeys(req *http.Request) []string {
  21. var additionalHeaderKeys []string
  22. for _, key := range conn.config.AdditionalHeaders {
  23. if _, ok := req.Header[key]; ok {
  24. additionalHeaderKeys = append(additionalHeaderKeys, key)
  25. }
  26. }
  27. return additionalHeaderKeys
  28. }
  29. // signHeader signs the header and sets it as the authorization header.
  30. func (conn Conn) signHeader(req *http.Request, canonicalizedResource string) {
  31. // Get the final authorization string
  32. authorizationStr := ""
  33. if conn.config.AuthVersion == AuthV2 {
  34. additionalHeaderKeys := conn.getAdditionalHeaderKeys(req)
  35. if len(additionalHeaderKeys) > 0 {
  36. authorizationFmt := "OSS2 AccessKeyId:%v,AdditionalHeaders:%v,Signature:%v"
  37. additionnalHeadersStr := strings.Join(additionalHeaderKeys, ";")
  38. authorizationStr = fmt.Sprintf(authorizationFmt, conn.config.AccessKeyID, additionnalHeadersStr, conn.getSignedStr(req, canonicalizedResource))
  39. } else {
  40. authorizationFmt := "OSS2 AccessKeyId:%v,Signature:%v"
  41. authorizationStr = fmt.Sprintf(authorizationFmt, conn.config.AccessKeyID, conn.getSignedStr(req, canonicalizedResource))
  42. }
  43. } else {
  44. authorizationStr = "OSS " + conn.config.AccessKeyID + ":" + conn.getSignedStr(req, canonicalizedResource)
  45. }
  46. // Give the parameter "Authorization" value
  47. req.Header.Set(HTTPHeaderAuthorization, authorizationStr)
  48. }
  49. func (conn Conn) getSignedStr(req *http.Request, canonicalizedResource string) string {
  50. // Find out the "x-oss-"'s address in header of the request
  51. ossHeadersMap := make(map[string]string)
  52. for k, v := range req.Header {
  53. if strings.HasPrefix(strings.ToLower(k), "x-oss-") {
  54. ossHeadersMap[strings.ToLower(k)] = v[0]
  55. }
  56. }
  57. hs := newHeaderSorter(ossHeadersMap)
  58. additionnalHeadersMap := make(map[string]string)
  59. additionalHeaders := ""
  60. if conn.config.AuthVersion == AuthV2 {
  61. additionalHeaderKeys := conn.getAdditionalHeaderKeys(req)
  62. for _, additionalHeaderKey := range additionalHeaderKeys {
  63. additionalHeaderValue := req.Header[additionalHeaderKey]
  64. hs.Keys = append(hs.Keys, strings.ToLower(additionalHeaderKey))
  65. hs.Vals = append(hs.Vals, additionalHeaderValue[0])
  66. additionnalHeadersMap[strings.ToLower(additionalHeaderKey)] = additionalHeaderValue[0]
  67. }
  68. ahs := newHeaderSorter(additionnalHeadersMap)
  69. ahs.Sort()
  70. for i := range ahs.Keys {
  71. additionalHeaders += ahs.Keys[i] + ":" + ahs.Vals[i] + ";"
  72. }
  73. }
  74. // Sort the headers by the ascending order
  75. hs.Sort()
  76. // Get the canonicalizedOSSHeaders
  77. canonicalizedOSSHeaders := ""
  78. for i := range hs.Keys {
  79. canonicalizedOSSHeaders += hs.Keys[i] + ":" + hs.Vals[i] + "\n"
  80. }
  81. // Give other parameters values
  82. // when sign URL, date is expires
  83. date := req.Header.Get(HTTPHeaderDate)
  84. contentType := req.Header.Get(HTTPHeaderContentType)
  85. contentMd5 := req.Header.Get(HTTPHeaderContentMD5)
  86. signStr := req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource
  87. conn.config.WriteLog(Debug, "[Req:%p]signStr:%s.\n", req, signStr)
  88. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  89. if conn.config.AuthVersion == AuthV2 {
  90. signStr = req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + additionalHeaders + "\n" + canonicalizedResource
  91. h = hmac.New(func() hash.Hash { return sha256.New() }, []byte(conn.config.AccessKeySecret))
  92. }
  93. fmt.Printf("the value of signStr is %v", signStr)
  94. io.WriteString(h, signStr)
  95. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  96. return signedStr
  97. }
  98. // newHeaderSorter is an additional function for function SignHeader.
  99. func newHeaderSorter(m map[string]string) *headerSorter {
  100. hs := &headerSorter{
  101. Keys: make([]string, 0, len(m)),
  102. Vals: make([]string, 0, len(m)),
  103. }
  104. for k, v := range m {
  105. hs.Keys = append(hs.Keys, k)
  106. hs.Vals = append(hs.Vals, v)
  107. }
  108. return hs
  109. }
  110. // Sort is an additional function for function SignHeader.
  111. func (hs *headerSorter) Sort() {
  112. sort.Sort(hs)
  113. }
  114. // Len is an additional function for function SignHeader.
  115. func (hs *headerSorter) Len() int {
  116. return len(hs.Vals)
  117. }
  118. // Less is an additional function for function SignHeader.
  119. func (hs *headerSorter) Less(i, j int) bool {
  120. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  121. }
  122. // Swap is an additional function for function SignHeader.
  123. func (hs *headerSorter) Swap(i, j int) {
  124. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  125. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  126. }