/
akskAuth.go
157 lines (140 loc) · 3.43 KB
/
akskAuth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package midware
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/bytedance/Elkeid/server/service_discovery/common"
"github.com/gin-gonic/gin"
"github.com/levigross/grequests"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
var (
AK string
SK string
)
func init() {
for k, v := range common.AuthKeys {
AK = k
SK = v
break
}
}
func getSecKec(ak string) string {
sk, ok := common.AuthKeys[ak]
if !ok {
return ""
}
return sk
}
func sha256byteArr(in []byte) string {
if in == nil || len(in) == 0 {
return ""
}
h := sha256.New()
h.Write(in)
return hex.EncodeToString(h.Sum(nil))
}
func generateSign(method, url, query, ak, timestamp, sk string, requestBody []byte) string {
return hmacSha256(fmt.Sprintf(`%s\n%s\n%s\n%s\n%s\n%s`, method, url, query, ak, timestamp, sha256byteArr(requestBody)), sk)
}
func hmacSha256(data string, secret string) string {
//fmt.Println("hmacSha256", data, secret)
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func beforeRequestFunc(req *http.Request) error {
var (
timestamp = fmt.Sprintf(`%d`, time.Now().Unix())
err error
requestBody []byte
)
if req.Body != nil {
requestBody, err = ioutil.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
} else {
requestBody = []byte{}
}
sign := generateSign(req.Method, formatURLPath(req.URL.Path), req.URL.RawQuery, AK, timestamp, SK, requestBody)
req.Header.Add("AccessKey", AK)
req.Header.Add("Signature", sign)
req.Header.Add("TimeStamp", timestamp)
return nil
}
func formatURLPath(in string) string {
in = strings.TrimSpace(in)
if strings.HasSuffix(in, "/") {
return in[:len(in)-1]
}
return in
}
func AuthRequestOption() *grequests.RequestOptions {
option := &grequests.RequestOptions{
InsecureSkipVerify: true,
BeforeRequest: beforeRequestFunc,
}
return option
}
func AKSKAuth() gin.HandlerFunc {
return func(c *gin.Context) {
var (
ak, sk, sign, timeStamp, serverSign string
iTime, timeDiff int64
err error
requestBody []byte
)
ak = strings.ToLower(c.Request.Header.Get("AccessKey"))
sign = c.Request.Header.Get("Signature")
timeStamp = c.Request.Header.Get("TimeStamp")
if ak == "" || sign == "" || timeStamp == "" {
abort(c, "header missed: AccessKey|Signature|TimeStamp")
return
}
//check_time
iTime, err = strconv.ParseInt(timeStamp, 10, 64)
if err != nil {
abort(c, fmt.Sprintf(`TimeStamp Error %s`, err.Error()))
return
}
timeDiff = time.Now().Unix() - iTime
if timeDiff >= 60 || timeDiff <= -60 {
abort(c, "TimeStamp Error")
return
}
//check_sign
sk = getSecKec(ak)
if sk == "" {
abort(c, "User not exist")
return
}
requestBody, err = ioutil.ReadAll(c.Request.Body)
if err != nil {
abort(c, err.Error())
return
}
c.Request.Body.Close()
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
serverSign = generateSign(c.Request.Method, formatURLPath(c.Request.URL.Path), c.Request.URL.RawQuery, ak, timeStamp, sk, requestBody)
if serverSign != sign {
abort(c, "Signature Error")
return
}
c.Next()
return
}
}
func abort(c *gin.Context, reason string) {
c.Abort()
c.JSON(http.StatusForbidden, gin.H{"code": -1, "msg": "auth failed", "data": reason})
return
}