auth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.AccessKeyID + ":" + 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. conn.config.WriteLog(Debug, "[Req:%p]signStr:%s.\n", req, signStr)
  50. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  51. io.WriteString(h, signStr)
  52. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  53. return signedStr
  54. }
  55. func (conn Conn) getRtmpSignedStr(bucketName, channelName, playlistName string, expiration int64, params map[string]interface{}) string {
  56. if params[HTTPParamAccessKeyID] == nil {
  57. return ""
  58. }
  59. canonResource := fmt.Sprintf("/%s/%s", bucketName, channelName)
  60. canonParamsKeys := []string{}
  61. for key := range params {
  62. if key != HTTPParamAccessKeyID && key != HTTPParamSignature && key != HTTPParamExpires && key != HTTPParamSecurityToken {
  63. canonParamsKeys = append(canonParamsKeys, key)
  64. }
  65. }
  66. sort.Strings(canonParamsKeys)
  67. canonParamsStr := ""
  68. for _, key := range canonParamsKeys {
  69. canonParamsStr = fmt.Sprintf("%s%s:%s\n", canonParamsStr, key, params[key].(string))
  70. }
  71. expireStr := strconv.FormatInt(expiration, 10)
  72. signStr := expireStr + "\n" + canonParamsStr + canonResource
  73. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  74. io.WriteString(h, signStr)
  75. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  76. return signedStr
  77. }
  78. // newHeaderSorter is an additional function for function SignHeader.
  79. func newHeaderSorter(m map[string]string) *headerSorter {
  80. hs := &headerSorter{
  81. Keys: make([]string, 0, len(m)),
  82. Vals: make([]string, 0, len(m)),
  83. }
  84. for k, v := range m {
  85. hs.Keys = append(hs.Keys, k)
  86. hs.Vals = append(hs.Vals, v)
  87. }
  88. return hs
  89. }
  90. // Sort is an additional function for function SignHeader.
  91. func (hs *headerSorter) Sort() {
  92. sort.Sort(hs)
  93. }
  94. // Len is an additional function for function SignHeader.
  95. func (hs *headerSorter) Len() int {
  96. return len(hs.Vals)
  97. }
  98. // Less is an additional function for function SignHeader.
  99. func (hs *headerSorter) Less(i, j int) bool {
  100. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  101. }
  102. // Swap is an additional function for function SignHeader.
  103. func (hs *headerSorter) Swap(i, j int) {
  104. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  105. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  106. }