auth.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  48. io.WriteString(h, signStr)
  49. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  50. return signedStr
  51. }
  52. // newHeaderSorter is an additional function for function SignHeader.
  53. func newHeaderSorter(m map[string]string) *headerSorter {
  54. hs := &headerSorter{
  55. Keys: make([]string, 0, len(m)),
  56. Vals: make([]string, 0, len(m)),
  57. }
  58. for k, v := range m {
  59. hs.Keys = append(hs.Keys, k)
  60. hs.Vals = append(hs.Vals, v)
  61. }
  62. return hs
  63. }
  64. // Sort is an additional function for function SignHeader.
  65. func (hs *headerSorter) Sort() {
  66. sort.Sort(hs)
  67. }
  68. // Len is an additional function for function SignHeader.
  69. func (hs *headerSorter) Len() int {
  70. return len(hs.Vals)
  71. }
  72. // Less is an additional function for function SignHeader.
  73. func (hs *headerSorter) Less(i, j int) bool {
  74. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  75. }
  76. // Swap is an additional function for function SignHeader.
  77. func (hs *headerSorter) Swap(i, j int) {
  78. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  79. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  80. }