/
middleware.go
248 lines (198 loc) · 7.1 KB
/
middleware.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package api
import (
"context"
"time"
cogIdp "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
"github.com/rcholic/CognitoREST/models"
"github.com/sirupsen/logrus"
)
// Middleware decorates a service.
type Middleware func(UserService) UserService
// LoggingMiddleware logs method calls, parameters, results, and elapsed time.
func LoggingMiddleware(logger log.Logger) Middleware {
return func(next UserService) UserService {
return loggingMiddleware{
next: next,
logger: logger,
}
}
}
type loggingMiddleware struct {
next UserService
logger log.Logger
}
func (mw loggingMiddleware) SignUp(username, password, confirmPass, email string) (*cogIdp.SignUpOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "SignUp",
"username", username,
"password", "*****",
"confirmPass", "****",
"password =? confirmPass", password == confirmPass,
"email", email,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.SignUp(username, password, confirmPass, email)
}
func (mw loggingMiddleware) SignIn(username, password string) (*cogIdp.InitiateAuthOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "SignIn",
"username", username,
"password", "*****",
"took", time.Since(begin),
)
}(time.Now())
return mw.next.SignIn(username, password)
}
func (mw loggingMiddleware) GetUser(username string) (*cogIdp.AdminGetUserOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "GetUser",
"username", username,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.GetUser(username)
}
func (mw loggingMiddleware) ConfirmSingUp(code, username string) (*cogIdp.ConfirmSignUpOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "ConfirmSingUp",
"code", code,
"username", username,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.ConfirmSingUp(code, username)
}
func (mw loggingMiddleware) ForgotPassword(username string) (*cogIdp.ForgotPasswordOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "ForgotPassword",
"username", username,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.ForgotPassword(username)
}
func (mw loggingMiddleware) ConfirmForgotPassword(newPass, code, username string) (*cogIdp.ConfirmForgotPasswordOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "ConfirmForgotPassword",
"newPassword", "****",
"code", code,
"username", username,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.ConfirmForgotPassword(newPass, code, username)
}
func (mw loggingMiddleware) ChangePassword(accessToken, prevPass, newPass string) (*cogIdp.ChangePasswordOutput, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "ChangePassword",
"accessToken", "*****",
"prevPass", "****",
"newPass", "****",
"took", time.Since(begin),
)
}(time.Now())
return mw.next.ChangePassword(accessToken, prevPass, newPass)
}
func (mw loggingMiddleware) ValidateJwtToken(token string) (models.AuthenticatedUser, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "ValidateJwtToken",
"token", "*****",
"took", time.Since(begin),
)
}(time.Now())
return mw.next.ValidateJwtToken(token)
}
// TODO: instrumentation to be added
type instrumentingService struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
UserService
}
// NewInstrumentingService returns an instance of an instrumenting UserService.
func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.Histogram, s UserService) UserService {
return &instrumentingService{
requestCount: requestCount,
requestLatency: requestLatency,
UserService: s,
}
}
func (s *instrumentingService) SignUp(username, password, confirmPass, email string) (*cogIdp.SignUpOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "SignUp").Add(1)
s.requestLatency.With("method", "SignUp").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.SignUp(username, password, confirmPass, email)
}
func (s *instrumentingService) SignIn(username, password string) (*cogIdp.InitiateAuthOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "SignIn").Add(1)
s.requestLatency.With("method", "SignIn").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.SignIn(username, password)
}
func (s *instrumentingService) GetUser(username string) (*cogIdp.AdminGetUserOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "GetUser").Add(1)
s.requestLatency.With("method", "GetUser").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.GetUser(username)
}
func (s *instrumentingService) ConfirmSingUp(code, username string) (*cogIdp.ConfirmSignUpOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "ConfirmSingUp").Add(1)
s.requestLatency.With("method", "ConfirmSingUp").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.ConfirmSingUp(code, username)
}
func (s *instrumentingService) ForgotPassword(username string) (*cogIdp.ForgotPasswordOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "ForgotPassword").Add(1)
s.requestLatency.With("method", "ForgotPassword").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.ForgotPassword(username)
}
func (s *instrumentingService) ConfirmForgotPassword(newPass, code, username string) (*cogIdp.ConfirmForgotPasswordOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "ConfirmForgotPassword").Add(1)
s.requestLatency.With("method", "ConfirmForgotPassword").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.ConfirmForgotPassword(newPass, code, username)
}
func (s *instrumentingService) ChangePassword(accessToken, prevPass, newPass string) (*cogIdp.ChangePasswordOutput, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "ChangePassword").Add(1)
s.requestLatency.With("method", "ChangePassword").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.ChangePassword(accessToken, prevPass, newPass)
}
func (s *instrumentingService) ValidateJwtToken(token string) (models.AuthenticatedUser, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "ValidateJwtToken").Add(1)
s.requestLatency.With("method", "ValidateJwtToken").Observe(time.Since(begin).Seconds())
}(time.Now())
return s.UserService.ValidateJwtToken(token)
}
// FIXME: this is not being used... TO BE DELETED
func MakeJwtCheckMiddleware() endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
logrus.Infof("logging info from JwtCheckMiddleware....")
// autho := request.(http.Request)
// tokens := autho.Header.Get("Authorization")
logrus.Infof("intercepted! request: %v\n", request)
return next(ctx, request)
}
}
}