auth.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package oss
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "fmt"
  8. "hash"
  9. "io"
  10. "net/http"
  11. "sort"
  12. "strconv"
  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. // signHeader signs the header and sets it as the authorization header.
  21. func (conn Conn) signHeader(req *http.Request, canonicalizedResource string) {
  22. // Get the final authorization string
  23. authorizationStr := "OSS " + conn.config.GetAccessKeyID() + ":" + conn.getSignedStr(req, canonicalizedResource)
  24. // Give the parameter "Authorization" value
  25. req.Header.Set(HTTPHeaderAuthorization, authorizationStr)
  26. }
  27. func (conn Conn) getSignedStr(req *http.Request, canonicalizedResource string) string {
  28. // Find out the "x-oss-"'s address in header of the request
  29. temp := make(map[string]string)
  30. for k, v := range req.Header {
  31. if strings.HasPrefix(strings.ToLower(k), "x-oss-") {
  32. temp[strings.ToLower(k)] = v[0]
  33. }
  34. }
  35. hs := newHeaderSorter(temp)
  36. // Sort the temp by the ascending order
  37. hs.Sort()
  38. // Get the canonicalizedOSSHeaders
  39. canonicalizedOSSHeaders := ""
  40. for i := range hs.Keys {
  41. canonicalizedOSSHeaders += hs.Keys[i] + ":" + hs.Vals[i] + "\n"
  42. }
  43. // Give other parameters values
  44. // when sign URL, date is expires
  45. date := req.Header.Get(HTTPHeaderDate)
  46. contentType := req.Header.Get(HTTPHeaderContentType)
  47. contentMd5 := req.Header.Get(HTTPHeaderContentMD5)
  48. signStr := req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource
  49. // convert sign to log for easy to view
  50. var signBuf bytes.Buffer
  51. for i := 0; i < len(signStr); i++ {
  52. if signStr[i] != '\n' {
  53. signBuf.WriteByte(signStr[i])
  54. } else {
  55. signBuf.WriteString("\\n")
  56. }
  57. }
  58. conn.config.WriteLog(Debug, "[Req:%p]signStr:%s\n", req, signBuf.String())
  59. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.GetAccessKeySecret()))
  60. io.WriteString(h, signStr)
  61. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  62. return signedStr
  63. }
  64. func (conn Conn) getRtmpSignedStr(bucketName, channelName, playlistName string, expiration int64, params map[string]interface{}) string {
  65. if params[HTTPParamAccessKeyID] == nil {
  66. return ""
  67. }
  68. canonResource := fmt.Sprintf("/%s/%s", bucketName, channelName)
  69. canonParamsKeys := []string{}
  70. for key := range params {
  71. if key != HTTPParamAccessKeyID && key != HTTPParamSignature && key != HTTPParamExpires && key != HTTPParamSecurityToken {
  72. canonParamsKeys = append(canonParamsKeys, key)
  73. }
  74. }
  75. sort.Strings(canonParamsKeys)
  76. canonParamsStr := ""
  77. for _, key := range canonParamsKeys {
  78. canonParamsStr = fmt.Sprintf("%s%s:%s\n", canonParamsStr, key, params[key].(string))
  79. }
  80. expireStr := strconv.FormatInt(expiration, 10)
  81. signStr := expireStr + "\n" + canonParamsStr + canonResource
  82. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.GetAccessKeySecret()))
  83. io.WriteString(h, signStr)
  84. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  85. return signedStr
  86. }
  87. // newHeaderSorter is an additional function for function SignHeader.
  88. func newHeaderSorter(m map[string]string) *headerSorter {
  89. hs := &headerSorter{
  90. Keys: make([]string, 0, len(m)),
  91. Vals: make([]string, 0, len(m)),
  92. }
  93. for k, v := range m {
  94. hs.Keys = append(hs.Keys, k)
  95. hs.Vals = append(hs.Vals, v)
  96. }
  97. return hs
  98. }
  99. // Sort is an additional function for function SignHeader.
  100. func (hs *headerSorter) Sort() {
  101. sort.Sort(hs)
  102. }
  103. // Len is an additional function for function SignHeader.
  104. func (hs *headerSorter) Len() int {
  105. return len(hs.Vals)
  106. }
  107. // Less is an additional function for function SignHeader.
  108. func (hs *headerSorter) Less(i, j int) bool {
  109. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  110. }
  111. // Swap is an additional function for function SignHeader.
  112. func (hs *headerSorter) Swap(i, j int) {
  113. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  114. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  115. }