-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (160 loc) · 4.34 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"encoding/json"
"errors"
"fmt"
goku_plugin "github.com/eolinker/goku-plugin"
"reflect"
"strings"
)
var builder = new(goku_APIKeyAuthFactory)
func Builder() goku_plugin.PluginFactory {
return builder
}
type APIKeyNode struct {
APIKey string `json:"Apikey"`
HideCredential bool `json:"hideCredential"`
Remark string `json:"remark"`
}
type APIKeyConf []APIKeyNode
type goku_APIKeyAuth struct {
conf APIKeyConf
}
type goku_APIKeyAuthFactory struct {
}
func (f *goku_APIKeyAuthFactory) Create(config string, clusterName string, updateTag string, strategyId string, apiId int) (*goku_plugin.PluginObj, error) {
if config != "" {
var conf APIKeyConf
if err := json.Unmarshal([]byte(config), &conf); err != nil {
//解析配置信息失败
return nil, fmt.Errorf("[apikey_auth] Parsing plugin config :%s", err.Error())
}
return &goku_plugin.PluginObj{
Access: &goku_APIKeyAuth{
conf: conf,
},
}, nil
}
return nil, errors.New("need config")
}
func (p *goku_APIKeyAuth) Access(ctx goku_plugin.ContextAccess) (isContinue bool, e error) {
for _, node := range p.conf {
_, flag, err := retrieveAPIKeyCredential(ctx, node)
if flag {
//认证成功
return true, nil
}
if err != nil {
ctx.SetStatus(403, "403")
ctx.SetBody([]byte(err.Error()))
return false, err
}
}
//认证信息不匹配
ctx.SetStatus(403, "403")
ctx.SetBody([]byte("[apikey_auth] Invalid Apikey"))
return false, errors.New("[apikey_auth] Invalid Apikey")
}
//获取验证信息
func retrieveAPIKeyCredential(ctx goku_plugin.ContextAccess, node APIKeyNode) (string, bool, error) {
v := ""
if values, ok := ctx.Request().Headers()["Authorization"]; ok {
v = values[0]
if v != "" && v == node.APIKey {
if node.HideCredential {
ctx.Proxy().DelHeader("Authorization")
}
return v, true, nil
}
return v, false, nil
}
if values, ok := ctx.Request().Headers()["Apikey"]; ok {
v := values[0]
if v != "" && v == node.APIKey {
if node.HideCredential {
ctx.Proxy().DelHeader("Apikey")
}
return v, true, nil
}
return v, false, nil
}
if values, ok := ctx.Request().URL().Query()["Apikey"]; ok {
v = values[0]
if v != "" && v == node.APIKey {
if node.HideCredential {
ctx.Proxy().Querys().Del("Apikey")
}
return v, true, nil
}
return v, false, nil
}
contentType := ctx.Request().Headers().Get("Content-Type")
if strings.Contains(contentType, "application/x-www-form-urlencoded") || strings.Contains(contentType, "application/www-form-urlencoded") {
formParams, err := ctx.Proxy().BodyForm()
if err != nil {
return "", false, err
}
if _, ok := formParams["Apikey"]; ok {
v = formParams["Apikey"][0]
if v != "" && v == node.APIKey {
if node.HideCredential {
delete(formParams, "Apikey")
ctx.Proxy().SetForm(formParams)
}
return v, true, nil
}
return v, false, nil
}
} else if strings.Contains(contentType, "application/json") {
var body map[string]interface{}
rawbody, err := ctx.Proxy().RawBody()
if err != nil {
return "", false, err
}
if err := json.Unmarshal(rawbody, &body); err != nil {
return "", false, err
}
if _, ok := body["Apikey"]; !ok {
return "", false, errors.New("[apikey_auth] cannot find the Apikey in body")
}
if TOfData(body["Apikey"]) == reflect.String {
v = body["Apikey"].(string)
if v != "" && v == node.APIKey {
if node.HideCredential {
delete(body, "Apikey")
editedBody, err := json.Marshal(body)
if err != nil {
return "", false, err
}
ctx.Proxy().SetRaw(contentType, editedBody)
}
return v, true, nil
}
return v, false, nil
} else {
return "", false, errors.New("[apikey_auth] Invalid data type for Apikey")
}
} else if strings.Contains(contentType, "multipart/form-data") {
bodyform, err := ctx.Proxy().BodyForm()
if err != nil {
return "", false, err
}
if values, ok := bodyform["Apikey"]; ok {
v = values[0]
if v != "" && v == node.APIKey {
if node.HideCredential {
delete(bodyform, "Apikey")
ctx.Proxy().SetForm(bodyform)
}
return v, true, nil
}
return v, false, nil
}
} else {
return "", false, errors.New("[apikey_auth] Unsupported Content-Type")
}
if v == "" {
return "", false, errors.New("[apikey_auth] cannot find the Apikey in query/body/header")
}
return v, true, nil
}