auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package oss
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "hash"
  8. "io"
  9. "net/http"
  10. "sort"
  11. "strings"
  12. )
  13. // headerSorter defines the key-value structure for storing the sorted data in signHeader.
  14. type headerSorter struct {
  15. Keys []string
  16. Vals []string
  17. }
  18. // signHeader signs the header and sets it as the authorization header.
  19. func (conn Conn) signHeader(req *http.Request, canonicalizedResource string) {
  20. // Get the final authorization string
  21. authorizationStr := "OSS " + conn.config.AccessKeyID + ":" + conn.getSignedStr(req, canonicalizedResource)
  22. // Give the parameter "Authorization" value
  23. req.Header.Set(HTTPHeaderAuthorization, authorizationStr)
  24. }
  25. func (conn Conn) getSignedStr(req *http.Request, canonicalizedResource string) string {
  26. // Find out the "x-oss-"'s address in header of the request
  27. temp := make(map[string]string)
  28. for k, v := range req.Header {
  29. if strings.HasPrefix(strings.ToLower(k), "x-oss-") {
  30. temp[strings.ToLower(k)] = v[0]
  31. }
  32. }
  33. hs := newHeaderSorter(temp)
  34. // Sort the temp by the ascending order
  35. hs.Sort()
  36. // Get the canonicalizedOSSHeaders
  37. canonicalizedOSSHeaders := ""
  38. for i := range hs.Keys {
  39. canonicalizedOSSHeaders += hs.Keys[i] + ":" + hs.Vals[i] + "\n"
  40. }
  41. // Give other parameters values
  42. // when sign URL, date is expires
  43. date := req.Header.Get(HTTPHeaderDate)
  44. contentType := req.Header.Get(HTTPHeaderContentType)
  45. contentMd5 := req.Header.Get(HTTPHeaderContentMD5)
  46. signStr := req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource
  47. conn.config.WriteLog(Debug, "[Req:%p]signStr:%s.\n", req, signStr)
  48. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  49. io.WriteString(h, signStr)
  50. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  51. return signedStr
  52. }
  53. // newHeaderSorter is an additional function for function SignHeader.
  54. func newHeaderSorter(m map[string]string) *headerSorter {
  55. hs := &headerSorter{
  56. Keys: make([]string, 0, len(m)),
  57. Vals: make([]string, 0, len(m)),
  58. }
  59. for k, v := range m {
  60. hs.Keys = append(hs.Keys, k)
  61. hs.Vals = append(hs.Vals, v)
  62. }
  63. return hs
  64. }
  65. // Sort is an additional function for function SignHeader.
  66. func (hs *headerSorter) Sort() {
  67. sort.Sort(hs)
  68. }
  69. // Len is an additional function for function SignHeader.
  70. func (hs *headerSorter) Len() int {
  71. return len(hs.Vals)
  72. }
  73. // Less is an additional function for function SignHeader.
  74. func (hs *headerSorter) Less(i, j int) bool {
  75. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  76. }
  77. // Swap is an additional function for function SignHeader.
  78. func (hs *headerSorter) Swap(i, j int) {
  79. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  80. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  81. }