auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // 用于signHeader的字典排序存放容器。
  16. type headerSorter struct {
  17. Keys []string
  18. Vals []string
  19. }
  20. // 生成签名方法(直接设置请求的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 this request'header
  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. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  50. io.WriteString(h, signStr)
  51. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  52. return signedStr
  53. }
  54. func (conn Conn) getRtmpSignedStr(bucketName, channelName, playlistName string, expiration int64, params map[string]interface{}) string {
  55. if params[HTTPParamAccessKeyID] == nil {
  56. return ""
  57. }
  58. canonResource := fmt.Sprintf("/%s/%s", bucketName, channelName)
  59. canonParamsKeys := []string{}
  60. for key := range params {
  61. if key != HTTPParamAccessKeyID && key != HTTPParamSignature && key != HTTPParamExpires && key != HTTPParamSecurityToken {
  62. canonParamsKeys = append(canonParamsKeys, key)
  63. }
  64. }
  65. sort.Strings(canonParamsKeys)
  66. canonParamsStr := ""
  67. for _, key := range canonParamsKeys {
  68. canonParamsStr = fmt.Sprintf("%s%s:%s\n", canonParamsStr, key, params[key].(string))
  69. }
  70. expireStr := strconv.FormatInt(expiration, 10)
  71. signStr := expireStr + "\n" + canonParamsStr + canonResource
  72. h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(conn.config.AccessKeySecret))
  73. io.WriteString(h, signStr)
  74. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  75. return signedStr
  76. }
  77. // Additional function for function SignHeader.
  78. func newHeaderSorter(m map[string]string) *headerSorter {
  79. hs := &headerSorter{
  80. Keys: make([]string, 0, len(m)),
  81. Vals: make([]string, 0, len(m)),
  82. }
  83. for k, v := range m {
  84. hs.Keys = append(hs.Keys, k)
  85. hs.Vals = append(hs.Vals, v)
  86. }
  87. return hs
  88. }
  89. // Additional function for function SignHeader.
  90. func (hs *headerSorter) Sort() {
  91. sort.Sort(hs)
  92. }
  93. // Additional function for function SignHeader.
  94. func (hs *headerSorter) Len() int {
  95. return len(hs.Vals)
  96. }
  97. // Additional function for function SignHeader.
  98. func (hs *headerSorter) Less(i, j int) bool {
  99. return bytes.Compare([]byte(hs.Keys[i]), []byte(hs.Keys[j])) < 0
  100. }
  101. // Additional function for function SignHeader.
  102. func (hs *headerSorter) Swap(i, j int) {
  103. hs.Vals[i], hs.Vals[j] = hs.Vals[j], hs.Vals[i]
  104. hs.Keys[i], hs.Keys[j] = hs.Keys[j], hs.Keys[i]
  105. }