oss_api.go 16 KB

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