-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.go
181 lines (150 loc) · 4.44 KB
/
gateway.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
package theta
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"strings"
"github.com/apex/gateway"
"github.com/aws/aws-lambda-go/events"
"github.com/go-chi/chi/v5"
"github.com/phogolabs/log"
)
// GatewayRoute represents a gateway route
type GatewayRoute interface {
Mount(r chi.Router)
}
// GatewayInterceptor represents a gateway interceptor
type GatewayInterceptor func(http.Handler) http.Handler
// GatewayHandler represents a webhook
type GatewayHandler struct {
router chi.Router
}
// Use use a given interceptor.
func (h *GatewayHandler) Use(m GatewayInterceptor) {
h.mux().Use(m)
}
// Group returns the router.
func (h *GatewayHandler) Mount(route GatewayRoute) {
route.Mount(h.mux())
}
// HandleContext handles the API Gateway Proxy Request
func (h *GatewayHandler) HandleContext(ctx context.Context, r events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
logger := log.WithFields(
log.Map{
"gateway_http_method": r.HTTPMethod,
"gateway_path": r.Path,
"gateway_resource": r.Resource,
"gateway_request_context_resource_id": r.RequestContext.ResourceID,
"gateway_request_context_resource_path": r.RequestContext.ResourcePath,
"gateway_request_context_request_id": r.RequestContext.RequestID,
"gateway_request_context_stage": r.RequestContext.Stage,
},
)
// enrich the context
ctx = log.SetContext(ctx, logger)
logger.Info("prepare a HTTP request")
// prepare the request
request, err := gateway.NewRequest(ctx, r)
// suppress the execution
if err != nil {
logger.WithError(err).Error("prepare a HTTP request failure")
return events.APIGatewayProxyResponse{}, err
}
logger.Info("serve a HTTP request")
// prepare the response
response := gateway.NewResponse()
// delegate to the router
h.mux().ServeHTTP(response, request)
// end
return response.End(), nil
}
func (h *GatewayHandler) mux() chi.Router {
if h.router == nil {
h.router = chi.NewMux()
}
return h.router
}
// AuthConfig represents an auth configuration.
type GatewayHandlerAuthConfig struct {
Users []*GatewayHandlerAuthUserConfig
}
// GatewayHandlerAuthUserConfig represents an user configuration.
type GatewayHandlerAuthUserConfig struct {
Name string
Password string
}
// GatewayHandlerAuth represents a authentication handler
type GatewayHandlerAuth struct {
// Credentials keeps a map of the registered credentials
Config *GatewayHandlerAuthConfig
}
// HandleContext handles the authorization request
func (p *GatewayHandlerAuth) HandleContext(ctx context.Context, r events.APIGatewayCustomAuthorizerRequest) (events.APIGatewayCustomAuthorizerResponse, error) {
var (
unauthorized = fmt.Errorf("Unauthorized")
empty = events.APIGatewayCustomAuthorizerResponse{}
)
logger := log.WithFields(
log.Map{
"gateway_auth_method_arn": r.MethodArn,
"gateway_auth_type": r.Type,
},
)
tUsername, tPassword, ok := p.credentials(r.AuthorizationToken)
if !ok {
logger.Info("no credentials provided")
// suppress
return empty, unauthorized
}
logger.Info("token authorization")
for _, user := range p.Config.Users {
if user.Name == tUsername && user.Password == tPassword {
logger.Info("token authorizion succeeded")
// create a policy
return p.policy(user.Name, "Allow", "*"), nil
}
}
logger.Info("token authorizion failure")
// suppress
return empty, unauthorized
}
func (h *GatewayHandlerAuth) credentials(auth string) (username, password string, ok bool) {
const prefix = "Basic "
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
return
}
data, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}
var (
parts = string(data)
index = strings.IndexByte(parts, ':')
)
if index < 0 {
return
}
return parts[:index], parts[index+1:], true
}
func (h *GatewayHandlerAuth) policy(principal, effect, resource string) events.APIGatewayCustomAuthorizerResponse {
response := events.APIGatewayCustomAuthorizerResponse{
PrincipalID: principal,
}
if effect != "" && resource != "" {
response.PolicyDocument = events.APIGatewayCustomAuthorizerPolicy{
Version: "2012-10-17",
Statement: []events.IAMPolicyStatement{
{
Action: []string{"execute-api:Invoke"},
Resource: []string{resource},
Effect: effect,
},
},
}
}
response.Context = map[string]interface{}{
"principal_id": principal,
}
return response
}