/
http.go
165 lines (143 loc) 路 3.86 KB
/
http.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
package model
import (
"net/http"
)
const TriggerTypeHTTP TriggerType = "http"
type HTTPMethod string
var (
HTTPMethodGET HTTPMethod = "GET"
HTTPMethodPUT HTTPMethod = "PUT"
HTTPMethodPOST HTTPMethod = "POST"
HTTPMethodPATCH HTTPMethod = "PATCH"
HTTPMethodDELETE HTTPMethod = "DELETE"
HTTPMethodCOPY HTTPMethod = "COPY"
HTTPMethodHEAD HTTPMethod = "HEAD"
HTTPMethodOPTIONS HTTPMethod = "OPTIONS"
HTTPMethodLINK HTTPMethod = "LINK"
HTTPMethodUNLINK HTTPMethod = "UNLINK"
HTTPMethodPURGE HTTPMethod = "PURGE"
HTTPMethodLOCK HTTPMethod = "LOCK"
HTTPMethodUNLOCK HTTPMethod = "UNLOCK"
HTTPMethodPROPFIND HTTPMethod = "PROPFIND"
HTTPMethodVIEW HTTPMethod = "VIEW"
)
type HTTPHeader struct {
Key string `expr_enabled:"true"`
Value string `expr_enabled:"true"`
}
type HTTPRequest struct {
Method HTTPMethod `expr_enabled:"true" json:"method,omitempty"`
URL string `expr_enabled:"true" json:"url"`
Body string `expr_enabled:"true" json:"body,omitempty"`
Headers []HTTPHeader `json:"headers,omitempty"`
Auth *HTTPAuthenticator `json:"auth,omitempty"`
SSLVerification bool `json:"sslVerification,omitempty"`
}
func (a HTTPRequest) Authenticate(req *http.Request) {
if a.Auth == nil {
return
}
a.Auth.AuthenticateHTTP(req)
}
type HTTPResponse struct {
Status string
StatusCode int
Headers []HTTPHeader
Body string
}
type HTTPAuthenticator struct {
Type string `expr_enabled:"true"`
APIKey APIKeyAuthenticator
Basic BasicAuthenticator
Bearer BearerAuthenticator
}
func (a HTTPAuthenticator) Map(mapFn func(current string) (string, error)) (HTTPAuthenticator, error) {
var err error
switch a.Type {
case "apiKey":
in := string(a.APIKey.In)
in, err = mapFn(in)
if err != nil {
return a, err
}
a.APIKey.In = APIKeyPosition(in)
a.APIKey.Key, err = mapFn(a.APIKey.Key)
if err != nil {
return a, err
}
a.APIKey.Value, err = mapFn(a.APIKey.Value)
if err != nil {
return a, err
}
case "basic":
a.Basic.Username, err = mapFn(a.Basic.Username)
if err != nil {
return a, err
}
a.Basic.Password, err = mapFn(a.Basic.Password)
if err != nil {
return a, err
}
case "bearer":
a.Bearer.Bearer, err = mapFn(a.Bearer.Bearer)
if err != nil {
return a, err
}
}
return a, nil
}
func (a HTTPAuthenticator) AuthenticateGRPC() {}
func (a HTTPAuthenticator) AuthenticateHTTP(req *http.Request) {
var auth authenticator
switch a.Type {
case "apiKey":
auth = a.APIKey
case "basic":
auth = a.Basic
case "bearer":
auth = a.Bearer
default:
return
}
auth.AuthenticateHTTP(req)
}
type APIKeyPosition string
const (
APIKeyPositionHeader APIKeyPosition = "header"
APIKeyPositionQuery APIKeyPosition = "query"
)
type authenticator interface {
AuthenticateHTTP(req *http.Request)
AuthenticateGRPC()
}
type APIKeyAuthenticator struct {
Key string `expr_enabled:"true"`
Value string `expr_enabled:"true"`
In APIKeyPosition `expr_enabled:"true"`
}
func (a APIKeyAuthenticator) AuthenticateGRPC() {}
func (a APIKeyAuthenticator) AuthenticateHTTP(req *http.Request) {
switch a.In {
case APIKeyPositionHeader:
req.Header.Set(a.Key, a.Value)
case APIKeyPositionQuery:
q := req.URL.Query()
q.Add(a.Key, a.Value)
req.URL.RawQuery = q.Encode()
}
}
type BasicAuthenticator struct {
Username string `expr_enabled:"true"`
Password string `expr_enabled:"true"`
}
func (a BasicAuthenticator) AuthenticateGRPC() {}
func (a BasicAuthenticator) AuthenticateHTTP(req *http.Request) {
req.SetBasicAuth(a.Username, a.Password)
}
type BearerAuthenticator struct {
Bearer string `expr_enabled:"true"`
}
func (a BearerAuthenticator) AuthenticateGRPC() {}
func (a BearerAuthenticator) AuthenticateHTTP(req *http.Request) {
req.Header.Add("Authorization", a.Bearer)
}