oss.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package upload
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/rsa"
  8. "crypto/sha1"
  9. "crypto/x509"
  10. "encoding/base64"
  11. "encoding/json"
  12. "encoding/pem"
  13. "errors"
  14. "fmt"
  15. "hash"
  16. "io"
  17. "io/ioutil"
  18. "net/http"
  19. "strconv"
  20. "time"
  21. )
  22. type Oss struct {
  23. AccessKeyId string
  24. AccessKeySecret string
  25. Host string
  26. CallbackUrl string
  27. ExpireTime int64
  28. MaxByte int64
  29. }
  30. const (
  31. base64Table = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
  32. )
  33. var coder = base64.NewEncoding(base64Table)
  34. func base64Encode(src []byte) []byte {
  35. return []byte(coder.EncodeToString(src))
  36. }
  37. func get_gmt_iso8601(expire_end int64) string {
  38. var tokenExpire = time.Unix(expire_end, 0).UTC().Format("2006-01-02T15:04:05Z")
  39. return tokenExpire
  40. }
  41. type ConfigStruct struct {
  42. Expiration string `json:"expiration"`
  43. Conditions [][]interface{} `json:"conditions"`
  44. }
  45. type PolicyToken struct {
  46. AccessKeyId string `json:"accessid"`
  47. Host string `json:"host"`
  48. Expire int64 `json:"expire"`
  49. Signature string `json:"signature"`
  50. Policy string `json:"policy"`
  51. Directory string `json:"dir"`
  52. Callback string `json:"callback"`
  53. }
  54. type CallbackParam struct {
  55. CallbackUrl string `json:"callbackUrl"`
  56. CallbackBody string `json:"callbackBody"`
  57. CallbackBodyType string `json:"callbackBodyType"`
  58. }
  59. type CallbackBaseForm struct {
  60. Bucket string `json:"bucket" form:"bucket"`
  61. Etag string `json:"etag" form:"etag"`
  62. Filename string `json:"filename" form:"filename"`
  63. Size string `json:"size" form:"size"`
  64. MimeType string `json:"mime_type" form:"mime_type"`
  65. Height string `json:"height" form:"height"`
  66. Width string `json:"width" form:"width"`
  67. Format string `json:"format" form:"format"`
  68. OriginFilename string `json:"origin_filename" form:"origin_filename"`
  69. }
  70. func (oc *Oss) GetPolicyToken(uploadDir string) string {
  71. now := time.Now().Unix()
  72. expire_end := now + oc.ExpireTime
  73. var tokenExpire = get_gmt_iso8601(expire_end)
  74. //create post policy json
  75. var config ConfigStruct
  76. config.Expiration = tokenExpire
  77. var condition = []interface{}{"starts-with", "$key", uploadDir}
  78. var condition_limit = []interface{}{"content-length-range", 0, oc.MaxByte}
  79. config.Conditions = append(config.Conditions, condition, condition_limit)
  80. //calucate signature
  81. result, err := json.Marshal(config)
  82. debyte := base64.StdEncoding.EncodeToString(result)
  83. h := hmac.New(func() hash.Hash {
  84. return sha1.New()
  85. }, []byte(oc.AccessKeySecret))
  86. io.WriteString(h, debyte)
  87. signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
  88. var callbackParam CallbackParam
  89. callbackParam.CallbackUrl = oc.CallbackUrl
  90. callbackParam.CallbackBody =
  91. "bucket=${bucket}&" +
  92. "etag=${etag}&" +
  93. "filename=${object}&" +
  94. "size=${size}&" +
  95. "mime_type=${mimeType}&" +
  96. "height=${imageInfo.height}&" +
  97. "width=${imageInfo.width}&" +
  98. "format=${imageInfo.format}&" +
  99. "origin_filename=${x:origin_filename}"
  100. callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
  101. callback_str, err := json.Marshal(callbackParam)
  102. if err != nil {
  103. fmt.Println("callback json err:", err)
  104. }
  105. callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
  106. var policyToken PolicyToken
  107. policyToken.AccessKeyId = oc.AccessKeyId
  108. policyToken.Host = oc.Host
  109. policyToken.Expire = expire_end
  110. policyToken.Signature = string(signedStr)
  111. policyToken.Directory = uploadDir
  112. policyToken.Policy = string(debyte)
  113. policyToken.Callback = string(callbackBase64)
  114. response, err := json.Marshal(policyToken)
  115. if err != nil {
  116. fmt.Println("json err:", err)
  117. }
  118. return string(response)
  119. }
  120. func (oc *Oss) Verify(r *http.Request) bool {
  121. // Get PublicKey bytes
  122. bytePublicKey, err := getPublicKey(r)
  123. if err != nil {
  124. return false
  125. }
  126. // Get Authorization bytes : decode from Base64String
  127. byteAuthorization, err := getAuthorization(r)
  128. if err != nil {
  129. return false
  130. }
  131. // Get MD5 bytes from Newly Constructed Authrization String.
  132. byteMD5, err := getMD5FromNewAuthString(r)
  133. if err != nil {
  134. return false
  135. }
  136. // verifySignature and response to client
  137. if verifySignature(bytePublicKey, byteMD5, byteAuthorization) {
  138. // do something you want accoding to callback_body ...
  139. return true
  140. } else {
  141. return false
  142. }
  143. }
  144. // getPublicKey : Get PublicKey bytes from Request.URL
  145. func getPublicKey(r *http.Request) ([]byte, error) {
  146. var bytePublicKey []byte
  147. // get PublicKey URL
  148. publicKeyURLBase64 := r.Header.Get("x-oss-pub-key-url")
  149. if publicKeyURLBase64 == "" {
  150. fmt.Println("GetPublicKey from Request header failed : No x-oss-pub-key-url field. ")
  151. return bytePublicKey, errors.New("no x-oss-pub-key-url field in Request header ")
  152. }
  153. publicKeyURL, _ := base64.StdEncoding.DecodeString(publicKeyURLBase64)
  154. // fmt.Printf("publicKeyURL={%s}\n", publicKeyURL)
  155. // get PublicKey Content from URL
  156. responsePublicKeyURL, err := http.Get(string(publicKeyURL))
  157. if err != nil {
  158. fmt.Printf("Get PublicKey Content from URL failed : %s \n", err.Error())
  159. return bytePublicKey, err
  160. }
  161. bytePublicKey, err = ioutil.ReadAll(responsePublicKeyURL.Body)
  162. if err != nil {
  163. fmt.Printf("Read PublicKey Content from URL failed : %s \n", err.Error())
  164. return bytePublicKey, err
  165. }
  166. defer responsePublicKeyURL.Body.Close()
  167. // fmt.Printf("publicKey={%s}\n", bytePublicKey)
  168. return bytePublicKey, nil
  169. }
  170. // getAuthorization : decode from Base64String
  171. func getAuthorization(r *http.Request) ([]byte, error) {
  172. var byteAuthorization []byte
  173. // Get Authorization bytes : decode from Base64String
  174. strAuthorizationBase64 := r.Header.Get("authorization")
  175. if strAuthorizationBase64 == "" {
  176. fmt.Println("Failed to get authorization field from request header. ")
  177. return byteAuthorization, errors.New("no authorization field in Request header")
  178. }
  179. byteAuthorization, _ = base64.StdEncoding.DecodeString(strAuthorizationBase64)
  180. return byteAuthorization, nil
  181. }
  182. // getMD5FromNewAuthString : Get MD5 bytes from Newly Constructed Authrization String.
  183. func getMD5FromNewAuthString(r *http.Request) ([]byte, error) {
  184. var byteMD5 []byte
  185. // Construct the New Auth String from URI+Query+Body
  186. bodyContent, err := ioutil.ReadAll(r.Body)
  187. r.Body.Close()
  188. r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyContent))
  189. if err != nil {
  190. fmt.Printf("Read Request Body failed : %s \n", err.Error())
  191. return byteMD5, err
  192. }
  193. strCallbackBody := string(bodyContent)
  194. // fmt.Printf("r.URL.RawPath={%s}, r.URL.Query()={%s}, strCallbackBody={%s}\n", r.URL.RawPath, r.URL.Query(), strCallbackBody)
  195. strURLPathDecode, errUnescape := unescapePath(r.URL.Path, encodePathSegment) //url.PathUnescape(r.URL.Path) for Golang v1.8.2+
  196. if errUnescape != nil {
  197. fmt.Printf("url.PathUnescape failed : URL.Path=%s, error=%s \n", r.URL.Path, err.Error())
  198. return byteMD5, errUnescape
  199. }
  200. // Generate New Auth String prepare for MD5
  201. strAuth := ""
  202. if r.URL.RawQuery == "" {
  203. strAuth = fmt.Sprintf("%s\n%s", strURLPathDecode, strCallbackBody)
  204. } else {
  205. strAuth = fmt.Sprintf("%s?%s\n%s", strURLPathDecode, r.URL.RawQuery, strCallbackBody)
  206. }
  207. // fmt.Printf("NewlyConstructedAuthString={%s}\n", strAuth)
  208. // Generate MD5 from the New Auth String
  209. md5Ctx := md5.New()
  210. md5Ctx.Write([]byte(strAuth))
  211. byteMD5 = md5Ctx.Sum(nil)
  212. return byteMD5, nil
  213. }
  214. /* VerifySignature
  215. * VerifySignature需要三个重要的数据信息来进行签名验证: 1>获取公钥PublicKey; 2>生成新的MD5鉴权串; 3>解码Request携带的鉴权串;
  216. * 1>获取公钥PublicKey : 从RequestHeader的"x-oss-pub-key-url"字段中获取 URL, 读取URL链接的包含的公钥内容, 进行解码解析, 将其作为rsa.VerifyPKCS1v15的入参。
  217. * 2>生成新的MD5鉴权串 : 把Request中的url中的path部分进行urldecode, 加上url的query部分, 再加上body, 组合之后进行MD5编码, 得到MD5鉴权字节串。
  218. * 3>解码Request携带的鉴权串 : 获取RequestHeader的"authorization"字段, 对其进行Base64解码,作为签名验证的鉴权对比串。
  219. * rsa.VerifyPKCS1v15进行签名验证,返回验证结果。
  220. * */
  221. func verifySignature(bytePublicKey []byte, byteMd5 []byte, authorization []byte) bool {
  222. pubBlock, _ := pem.Decode(bytePublicKey)
  223. if pubBlock == nil {
  224. fmt.Printf("Failed to parse PEM block containing the public key")
  225. return false
  226. }
  227. pubInterface, err := x509.ParsePKIXPublicKey(pubBlock.Bytes)
  228. if (pubInterface == nil) || (err != nil) {
  229. fmt.Printf("x509.ParsePKIXPublicKey(publicKey) failed : %s \n", err.Error())
  230. return false
  231. }
  232. pub := pubInterface.(*rsa.PublicKey)
  233. errorVerifyPKCS1v15 := rsa.VerifyPKCS1v15(pub, crypto.MD5, byteMd5, authorization)
  234. if errorVerifyPKCS1v15 != nil {
  235. fmt.Printf("\nSignature Verification is Failed : %s \n", errorVerifyPKCS1v15.Error())
  236. //printByteArray(byteMd5, "AuthMd5(fromNewAuthString)")
  237. //printByteArray(bytePublicKey, "PublicKeyBase64")
  238. //printByteArray(authorization, "AuthorizationFromRequest")
  239. return false
  240. }
  241. fmt.Printf("\nSignature Verification is Successful. \n")
  242. return true
  243. }
  244. func printByteArray(byteArrary []byte, arrName string) {
  245. fmt.Printf("++++++++ printByteArray : ArrayName=%s, ArrayLength=%d \n", arrName, len(byteArrary))
  246. for i := 0; i < len(byteArrary); i++ {
  247. fmt.Printf("%02x", byteArrary[i])
  248. }
  249. fmt.Printf("\n-------- printByteArray : End . \n")
  250. }
  251. type EscapeError string
  252. func (e EscapeError) Error() string {
  253. return "invalid URL escape " + strconv.Quote(string(e))
  254. }
  255. type InvalidHostError string
  256. func (e InvalidHostError) Error() string {
  257. return "invalid character " + strconv.Quote(string(e)) + " in host name"
  258. }
  259. type encoding int
  260. const (
  261. encodePath encoding = 1 + iota
  262. encodePathSegment
  263. encodeHost
  264. encodeZone
  265. encodeUserPassword
  266. encodeQueryComponent
  267. encodeFragment
  268. )
  269. // unescapePath : unescapes a string; the mode specifies, which section of the URL string is being unescaped.
  270. func unescapePath(s string, mode encoding) (string, error) {
  271. // Count %, check that they're well-formed.
  272. mode = encodePathSegment
  273. n := 0
  274. hasPlus := false
  275. for i := 0; i < len(s); {
  276. switch s[i] {
  277. case '%':
  278. n++
  279. if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
  280. s = s[i:]
  281. if len(s) > 3 {
  282. s = s[:3]
  283. }
  284. return "", EscapeError(s)
  285. }
  286. // Per https://tools.ietf.org/html/rfc3986#page-21
  287. // in the host component %-encoding can only be used
  288. // for non-ASCII bytes.
  289. // But https://tools.ietf.org/html/rfc6874#section-2
  290. // introduces %25 being allowed to escape a percent sign
  291. // in IPv6 scoped-address literals. Yay.
  292. if mode == encodeHost && unhex(s[i+1]) < 8 && s[i:i+3] != "%25" {
  293. return "", EscapeError(s[i : i+3])
  294. }
  295. if mode == encodeZone {
  296. // RFC 6874 says basically "anything goes" for zone identifiers
  297. // and that even non-ASCII can be redundantly escaped,
  298. // but it seems prudent to restrict %-escaped bytes here to those
  299. // that are valid host name bytes in their unescaped form.
  300. // That is, you can use escaping in the zone identifier but not
  301. // to introduce bytes you couldn't just write directly.
  302. // But Windows puts spaces here! Yay.
  303. v := unhex(s[i+1])<<4 | unhex(s[i+2])
  304. if s[i:i+3] != "%25" && v != ' ' && shouldEscape(v, encodeHost) {
  305. return "", EscapeError(s[i : i+3])
  306. }
  307. }
  308. i += 3
  309. case '+':
  310. hasPlus = mode == encodeQueryComponent
  311. i++
  312. default:
  313. if (mode == encodeHost || mode == encodeZone) && s[i] < 0x80 && shouldEscape(s[i], mode) {
  314. return "", InvalidHostError(s[i : i+1])
  315. }
  316. i++
  317. }
  318. }
  319. if n == 0 && !hasPlus {
  320. return s, nil
  321. }
  322. t := make([]byte, len(s)-2*n)
  323. j := 0
  324. for i := 0; i < len(s); {
  325. switch s[i] {
  326. case '%':
  327. t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
  328. j++
  329. i += 3
  330. case '+':
  331. if mode == encodeQueryComponent {
  332. t[j] = ' '
  333. } else {
  334. t[j] = '+'
  335. }
  336. j++
  337. i++
  338. default:
  339. t[j] = s[i]
  340. j++
  341. i++
  342. }
  343. }
  344. return string(t), nil
  345. }
  346. // Please be informed that for now shouldEscape does not check all
  347. // reserved characters correctly. See golang.org/issue/5684.
  348. func shouldEscape(c byte, mode encoding) bool {
  349. // §2.3 Unreserved characters (alphanum)
  350. if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' {
  351. return false
  352. }
  353. if mode == encodeHost || mode == encodeZone {
  354. // §3.2.2 Host allows
  355. // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  356. // as part of reg-name.
  357. // We add : because we include :port as part of host.
  358. // We add [ ] because we include [ipv6]:port as part of host.
  359. // We add < > because they're the only characters left that
  360. // we could possibly allow, and Parse will reject them if we
  361. // escape them (because hosts can't use %-encoding for
  362. // ASCII bytes).
  363. switch c {
  364. case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '[', ']', '<', '>', '"':
  365. return false
  366. }
  367. }
  368. switch c {
  369. case '-', '_', '.', '~': // §2.3 Unreserved characters (mark)
  370. return false
  371. case '$', '&', '+', ',', '/', ':', ';', '=', '?', '@': // §2.2 Reserved characters (reserved)
  372. // Different sections of the URL allow a few of
  373. // the reserved characters to appear unescaped.
  374. switch mode {
  375. case encodePath: // §3.3
  376. // The RFC allows : @ & = + $ but saves / ; , for assigning
  377. // meaning to individual path segments. This package
  378. // only manipulates the path as a whole, so we allow those
  379. // last three as well. That leaves only ? to escape.
  380. return c == '?'
  381. case encodePathSegment: // §3.3
  382. // The RFC allows : @ & = + $ but saves / ; , for assigning
  383. // meaning to individual path segments.
  384. return c == '/' || c == ';' || c == ',' || c == '?'
  385. case encodeUserPassword: // §3.2.1
  386. // The RFC allows ';', ':', '&', '=', '+', '$', and ',' in
  387. // userinfo, so we must escape only '@', '/', and '?'.
  388. // The parsing of userinfo treats ':' as special so we must escape
  389. // that too.
  390. return c == '@' || c == '/' || c == '?' || c == ':'
  391. case encodeQueryComponent: // §3.4
  392. // The RFC reserves (so we must escape) everything.
  393. return true
  394. case encodeFragment: // §4.1
  395. // The RFC text is silent but the grammar allows
  396. // everything, so escape nothing.
  397. return false
  398. }
  399. }
  400. // Everything else must be escaped.
  401. return true
  402. }
  403. func ishex(c byte) bool {
  404. switch {
  405. case '0' <= c && c <= '9':
  406. return true
  407. case 'a' <= c && c <= 'f':
  408. return true
  409. case 'A' <= c && c <= 'F':
  410. return true
  411. }
  412. return false
  413. }
  414. func unhex(c byte) byte {
  415. switch {
  416. case '0' <= c && c <= '9':
  417. return c - '0'
  418. case 'a' <= c && c <= 'f':
  419. return c - 'a' + 10
  420. case 'A' <= c && c <= 'F':
  421. return c - 'A' + 10
  422. }
  423. return 0
  424. }