-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.go
233 lines (203 loc) · 5.64 KB
/
middlewares.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package http
import (
"encoding/json"
"net/http"
"bytes"
"context"
"fmt"
"io/ioutil"
"strings"
"time"
l "github.com/bolatik/golang-libs-logger"
"github.com/bolatik/golang-libs-utils-jwt"
)
var errSys = &ErrorSystem{"ACL", 20}
type Handler func(w http.ResponseWriter, r *http.Request)
type Endpoint func(w http.ResponseWriter, r *http.Request) Response
//func Caching(gc cache.Cache, exp time.Duration, fn Endpoint) Endpoint {
// return func(w http.ResponseWriter, r *http.Request) Response {
// if r.Method != "GET" {
// return fn(w, r)
// }
//
// d, er := gc.Get(r.URL.String())
// if er == nil {
// data, ok := d.(Response)
// if ok {
// return data
// }
// }
//
// resp := fn(w, r)
// if resp.StatusCode() == http.StatusOK {
// _ = gc.SetWithExpire(r.URL.String(), resp, exp)
// }
//
// return resp
// }
//}
type Form int
const (
JsonForm Form = iota
ImagePngForm
FileXlsForm
RedirectForm
)
type OktaJwt struct {
Ver int `json:"ver"`
Jti string `json:"jti"`
Iss string `json:"iss"`
Aud string `json:"aud"`
Sub string `json:"sub"`
Iat int `json:"iat"`
Exp int `json:"exp"`
Cid string `json:"cid"`
UID string `json:"uid"`
Scp []string `json:"scp"`
}
func (idToken *OktaJwt) Valid() error {
return nil
}
func OktaJWT(fn Endpoint, vldtr *jwt.OktaJWTValidator) Endpoint {
return func(w http.ResponseWriter, r *http.Request) Response {
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
if len(authHeader) < 2 {
return errSys.BadRequest(11, "Authorization must be Bearer token")
}
idToken := authHeader[1]
if idToken == "" {
return errSys.BadRequest(10, "Authorization not provided")
}
tkn, err := vldtr.Validate(idToken)
if err != nil {
return errSys.BadRequest(10, err.Error())
}
ctx := context.WithValue(r.Context(), "token", tkn)
r = r.WithContext(ctx)
return fn(w, r)
}
}
func ACL(log l.Logger, fn Endpoint) Endpoint {
return func(w http.ResponseWriter, r *http.Request) Response {
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
if len(authHeader) < 2 {
return errSys.BadRequest(11, "Authorization must be Bearer token")
}
idToken := authHeader[1]
if idToken == "" {
return errSys.BadRequest(10, "Authorization not provided")
}
claims, err := jwt.Parse(idToken)
if err != nil {
return errSys.BadRequest(10, err.Error())
}
parts := strings.Split(claims.Audience, "/")
if len(parts) < 3 {
return errSys.BadRequest(11, "Audience is invalid")
}
bucket := parts[0]
brand := parts[1]
clientId := parts[2]
ctx := context.WithValue(r.Context(), "bucket", bucket)
ctx = context.WithValue(ctx, "brand", brand)
ctx = context.WithValue(ctx, "client_id", clientId)
ctx = context.WithValue(ctx, "user_id", claims.Subject)
ctx = context.WithValue(ctx, "acl", claims.Acl)
ctx = context.WithValue(ctx, "email", claims.Email)
ctx = context.WithValue(ctx, "phone_number", claims.PhoneNumber)
log.Info("ACL for the user with ID: ", claims.Subject, " is: ", claims.Acl)
r = r.WithContext(ctx)
return fn(w, r)
}
}
func Json(fn Endpoint) Handler {
return func(w http.ResponseWriter, r *http.Request) {
d := fn(w, r)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
for k, v := range d.Headers() {
w.Header().Set(k, v)
}
statusCode := d.StatusCode()
if statusCode == 302 || statusCode == 301 {
http.Redirect(w, r, d.Response().(string), statusCode)
return
}
w.WriteHeader(d.StatusCode())
err := json.NewEncoder(w).Encode(d.Response())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func ImagePNG(fn Endpoint) Handler {
return func(w http.ResponseWriter, r *http.Request) {
d := fn(w, r)
w.Header().Set("Content-Type", "image/png")
for k, v := range d.Headers() {
w.Header().Set(k, v)
}
w.WriteHeader(d.StatusCode())
resp := d.Response()
data, ok := resp.([]byte)
if !ok {
json.NewEncoder(w).Encode(d.Response())
} else {
w.Write(data)
}
}
}
func FileXls(fn Endpoint) Handler {
return func(w http.ResponseWriter, r *http.Request) {
d := fn(w, r)
for k, v := range d.Headers() {
w.Header().Set(k, v)
}
w.WriteHeader(d.StatusCode())
}
}
func Logging(log l.Logger, fn Endpoint) Endpoint {
return func(w http.ResponseWriter, r *http.Request) Response {
start := time.Now()
// Read the content
var bodyBytes []byte
if r.Body != nil {
bodyBytes, _ = ioutil.ReadAll(r.Body)
}
// Restore the io.ReadCloser to its original state
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// Use the content
bodyString := string(bodyBytes)
d := fn(w, r)
dBytes, _ := json.Marshal(d.Response())
if d.StatusCode()%200 < 100 || d.StatusCode()%300 < 100 {
log.Warn(LogRequest(r), " body= ", bodyString, time.Since(start), " ", d.StatusCode(), " ", string(dBytes))
} else {
log.Debug(LogRequest(r), " body= ", bodyString, time.Since(start), " ", d.StatusCode(), " ", string(dBytes))
}
return d
}
}
func LogRequest(r *http.Request) string {
// Create return string
var request []string
// Add the request string
urlPath := fmt.Sprintf("%v %v", r.Method, r.URL)
request = append(request, urlPath)
// Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host))
// Loop through headers
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
request = append(request, fmt.Sprintf("%v: %v", name, h))
}
}
// If this is a POST, add post data
if r.Method == "POST" {
_ = r.ParseForm()
request = append(request, " ")
request = append(request, r.Form.Encode())
}
// Return the request as a string
return strings.Join(request, " ") + " "
}