2016-04-03 17:59:27 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2016-06-16 11:49:35 +00:00
|
|
|
"crypto/md5"
|
2016-04-08 16:06:26 +00:00
|
|
|
"encoding/json"
|
2016-06-16 11:49:35 +00:00
|
|
|
"fmt"
|
2016-04-03 17:59:27 +00:00
|
|
|
"io/ioutil"
|
2016-06-13 19:17:43 +00:00
|
|
|
"regexp"
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-04-19 14:07:27 +00:00
|
|
|
"git.zxq.co/ripple/rippleapi/common"
|
2016-04-03 17:59:27 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Method wraps an API method to a HandlerFunc.
|
2016-05-15 11:57:04 +00:00
|
|
|
func Method(f func(md common.MethodData) common.CodeMessager, privilegesNeeded ...int) gin.HandlerFunc {
|
2016-04-03 17:59:27 +00:00
|
|
|
return func(c *gin.Context) {
|
2016-05-15 11:57:04 +00:00
|
|
|
initialCaretaker(c, f, privilegesNeeded...)
|
2016-04-08 15:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-05-15 11:57:04 +00:00
|
|
|
func initialCaretaker(c *gin.Context, f func(md common.MethodData) common.CodeMessager, privilegesNeeded ...int) {
|
2016-07-06 14:33:58 +00:00
|
|
|
rateLimiter()
|
|
|
|
|
2016-04-08 15:27:55 +00:00
|
|
|
data, err := ioutil.ReadAll(c.Request.Body)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-04-08 15:27:55 +00:00
|
|
|
token := ""
|
|
|
|
switch {
|
|
|
|
case c.Request.Header.Get("X-Ripple-Token") != "":
|
|
|
|
token = c.Request.Header.Get("X-Ripple-Token")
|
|
|
|
case c.Query("token") != "":
|
|
|
|
token = c.Query("token")
|
|
|
|
case c.Query("k") != "":
|
|
|
|
token = c.Query("k")
|
2016-06-08 16:55:48 +00:00
|
|
|
default:
|
|
|
|
token, _ = c.Cookie("X-Ripple-Token")
|
2016-04-08 15:27:55 +00:00
|
|
|
}
|
2016-06-16 11:49:35 +00:00
|
|
|
c.Set("token", fmt.Sprintf("%x", md5.Sum([]byte(token))))
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-04-08 15:27:55 +00:00
|
|
|
md := common.MethodData{
|
|
|
|
DB: db,
|
|
|
|
RequestData: data,
|
|
|
|
C: c,
|
|
|
|
}
|
|
|
|
if token != "" {
|
|
|
|
tokenReal, exists := GetTokenFull(token, db)
|
|
|
|
if exists {
|
|
|
|
md.User = tokenReal
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
2016-04-08 15:27:55 +00:00
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
|
2016-07-06 17:35:49 +00:00
|
|
|
perUserRequestLimiter(md.ID(), c.ClientIP())
|
2016-07-06 14:33:58 +00:00
|
|
|
|
2016-04-08 15:27:55 +00:00
|
|
|
missingPrivileges := 0
|
|
|
|
for _, privilege := range privilegesNeeded {
|
|
|
|
if int(md.User.Privileges)&privilege == 0 {
|
|
|
|
missingPrivileges |= privilege
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-08 15:27:55 +00:00
|
|
|
if missingPrivileges != 0 {
|
2016-04-16 16:05:24 +00:00
|
|
|
c.IndentedJSON(401, common.SimpleResponse(401, "You don't have the privilege(s): "+common.Privileges(missingPrivileges).String()+"."))
|
2016-04-08 15:27:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := f(md)
|
2016-04-16 16:05:24 +00:00
|
|
|
if resp.GetCode() == 0 {
|
|
|
|
// Dirty hack to set the code
|
|
|
|
type setCoder interface {
|
|
|
|
SetCode(int)
|
|
|
|
}
|
|
|
|
if newver, can := resp.(setCoder); can {
|
|
|
|
newver.SetCode(500)
|
|
|
|
}
|
2016-04-08 15:27:55 +00:00
|
|
|
}
|
2016-04-08 16:06:26 +00:00
|
|
|
|
2016-04-08 15:27:55 +00:00
|
|
|
if _, exists := c.GetQuery("pls200"); exists {
|
2016-04-08 16:06:26 +00:00
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
} else {
|
2016-04-16 16:05:24 +00:00
|
|
|
c.Writer.WriteHeader(resp.GetCode())
|
2016-04-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := c.GetQuery("callback"); exists {
|
|
|
|
c.Header("Content-Type", "application/javascript; charset=utf-8")
|
2016-04-08 15:27:55 +00:00
|
|
|
} else {
|
2016-04-08 16:06:26 +00:00
|
|
|
c.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
|
|
|
mkjson(c, resp)
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:17:43 +00:00
|
|
|
// Very restrictive, but this way it shouldn't completely fuck up.
|
|
|
|
var callbackJSONP = regexp.MustCompile(`^[a-zA-Z_\$][a-zA-Z0-9_\$]*$`)
|
|
|
|
|
2016-04-08 16:06:26 +00:00
|
|
|
// mkjson auto indents json, and wraps json into a jsonp callback if specified by the request.
|
|
|
|
// then writes to the gin.Context the data.
|
|
|
|
func mkjson(c *gin.Context, data interface{}) {
|
|
|
|
exported, err := json.MarshalIndent(data, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
2016-07-06 14:33:58 +00:00
|
|
|
exported = []byte(`{ "code": 500, "message": "something has gone really really really really really really wrong." }`)
|
2016-04-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
cb := c.Query("callback")
|
2016-06-13 19:17:43 +00:00
|
|
|
willcb := cb != "" &&
|
|
|
|
len(cb) < 100 &&
|
|
|
|
callbackJSONP.MatchString(cb)
|
2016-04-08 16:06:26 +00:00
|
|
|
if willcb {
|
|
|
|
c.Writer.Write([]byte("/**/ typeof " + cb + " === 'function' && " + cb + "("))
|
|
|
|
}
|
|
|
|
c.Writer.Write(exported)
|
|
|
|
if willcb {
|
|
|
|
c.Writer.Write([]byte(");"))
|
2016-04-08 15:27:55 +00:00
|
|
|
}
|
2016-04-03 17:59:27 +00:00
|
|
|
}
|