utils.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package base
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "math/rand"
  9. "net/http"
  10. "net/url"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/google/uuid"
  16. )
  17. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  18. func init() {
  19. rand.Seed(time.Now().Unix())
  20. }
  21. func createTempAKSK() (accessKeyId string, plainSk string, err error) {
  22. if accessKeyId, err = generateAccessKeyId("AKTP"); err != nil {
  23. return
  24. }
  25. plainSk, err = generateSecretKey()
  26. if err != nil {
  27. return
  28. }
  29. return
  30. }
  31. func generateAccessKeyId(prefix string) (string, error) {
  32. uuid := uuid.New()
  33. uidBase64 := base64.StdEncoding.EncodeToString([]byte(strings.Replace(uuid.String(), "-", "", -1)))
  34. s := strings.Replace(uidBase64, "=", "", -1)
  35. s = strings.Replace(s, "/", "", -1)
  36. s = strings.Replace(s, "+", "", -1)
  37. s = strings.Replace(s, "-", "", -1)
  38. return prefix + s, nil
  39. }
  40. func randStringRunes(n int) string {
  41. b := make([]rune, n)
  42. for i := range b {
  43. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  44. }
  45. return string(b)
  46. }
  47. func generateSecretKey() (string, error) {
  48. randString32 := randStringRunes(32)
  49. return aesEncryptCBCWithBase64([]byte(randString32), []byte("bytedance-isgood"))
  50. }
  51. func createInnerToken(credentials Credentials, sts *SecurityToken2, inlinePolicy *Policy, t int64) (*InnerToken, error) {
  52. var err error
  53. innerToken := new(InnerToken)
  54. innerToken.LTAccessKeyId = credentials.AccessKeyID
  55. innerToken.AccessKeyId = sts.AccessKeyID
  56. innerToken.ExpiredTime = t
  57. key := md5.Sum([]byte(credentials.SecretAccessKey))
  58. innerToken.SignedSecretAccessKey, err = aesEncryptCBCWithBase64([]byte(sts.SecretAccessKey), key[:])
  59. if err != nil {
  60. return nil, err
  61. }
  62. if inlinePolicy != nil {
  63. b, _ := json.Marshal(inlinePolicy)
  64. innerToken.PolicyString = string(b)
  65. }
  66. signStr := fmt.Sprintf("%s|%s|%d|%s|%s", innerToken.LTAccessKeyId, innerToken.AccessKeyId, innerToken.ExpiredTime, innerToken.SignedSecretAccessKey, innerToken.PolicyString)
  67. innerToken.Signature = hex.EncodeToString(hmacSHA256(key[:], signStr))
  68. return innerToken, nil
  69. }
  70. func getTimeout(serviceTimeout, apiTimeout time.Duration) time.Duration {
  71. timeout := time.Second
  72. if serviceTimeout != time.Duration(0) {
  73. timeout = serviceTimeout
  74. }
  75. if apiTimeout != time.Duration(0) {
  76. timeout = apiTimeout
  77. }
  78. return timeout
  79. }
  80. func mergeQuery(query1, query2 url.Values) (query url.Values) {
  81. query = url.Values{}
  82. if query1 != nil {
  83. for k, vv := range query1 {
  84. for _, v := range vv {
  85. query.Add(k, v)
  86. }
  87. }
  88. }
  89. if query2 != nil {
  90. for k, vv := range query2 {
  91. for _, v := range vv {
  92. query.Add(k, v)
  93. }
  94. }
  95. }
  96. return
  97. }
  98. func mergeHeader(header1, header2 http.Header) (header http.Header) {
  99. header = http.Header{}
  100. if header1 != nil {
  101. for k, v := range header1 {
  102. header.Set(k, strings.Join(v, ";"))
  103. }
  104. }
  105. if header2 != nil {
  106. for k, v := range header2 {
  107. header.Set(k, strings.Join(v, ";"))
  108. }
  109. }
  110. return
  111. }
  112. func NewAllowStatement(actions, resources []string) *Statement {
  113. sts := new(Statement)
  114. sts.Effect = "Allow"
  115. sts.Action = actions
  116. sts.Resource = resources
  117. return sts
  118. }
  119. func NewDenyStatement(actions, resources []string) *Statement {
  120. sts := new(Statement)
  121. sts.Effect = "Deny"
  122. sts.Action = actions
  123. sts.Resource = resources
  124. return sts
  125. }
  126. func ToUrlValues(i interface{}) (values url.Values) {
  127. values = url.Values{}
  128. iVal := reflect.ValueOf(i).Elem()
  129. typ := iVal.Type()
  130. for i := 0; i < iVal.NumField(); i++ {
  131. f := iVal.Field(i)
  132. // You ca use tags here...
  133. // tag := typ.Field(i).Tag.Get("tagname")
  134. // Convert each type into a string for the url.Values string map
  135. var v string
  136. switch f.Interface().(type) {
  137. case int, int8, int16, int32, int64:
  138. v = strconv.FormatInt(f.Int(), 10)
  139. case uint, uint8, uint16, uint32, uint64:
  140. v = strconv.FormatUint(f.Uint(), 10)
  141. case float32:
  142. v = strconv.FormatFloat(f.Float(), 'f', 4, 32)
  143. case float64:
  144. v = strconv.FormatFloat(f.Float(), 'f', 4, 64)
  145. case []byte:
  146. v = string(f.Bytes())
  147. case string:
  148. v = f.String()
  149. }
  150. values.Set(typ.Field(i).Name, v)
  151. }
  152. return
  153. }