-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticate.go
411 lines (372 loc) · 12.6 KB
/
authenticate.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package apis
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/alwitt/goutils"
"github.com/alwitt/padlock/authenticate"
"github.com/alwitt/padlock/common"
"github.com/alwitt/padlock/match"
"github.com/alwitt/padlock/models"
"github.com/apex/log"
"github.com/golang-jwt/jwt/v4"
)
// AuthenticationHandler the request authentication REST API handler
type AuthenticationHandler struct {
goutils.RestAPIHandler
oidClient authenticate.OpenIDIssuerClient
performIntrospect bool
introspector authenticate.Introspector
targetAudience *string
targetClaims common.OpenIDClaimsOfInterestConfig
reqHeaderParam common.AuthenticateRequestParamLocConfig
respHeaderParam common.AuthorizeRequestParamLocConfig
bypassChecker match.AuthBypassMatch
}
// defineAuthenticationHandler define a new AuthenticationHandler instance
func defineAuthenticationHandler(
logConfig common.HTTPRequestLogging,
oid authenticate.OpenIDIssuerClient,
performIntrospect bool,
introspector authenticate.Introspector,
authnCfg common.AuthenticationConfig,
respHeaderParam common.AuthorizeRequestParamLocConfig,
metrics goutils.HTTPRequestMetricHelper,
) (AuthenticationHandler, error) {
logTags := log.Fields{
"module": "apis", "component": "api-handler", "instance": "authentication",
}
instance := AuthenticationHandler{
RestAPIHandler: goutils.RestAPIHandler{
Component: goutils.Component{
LogTags: logTags,
LogTagModifiers: []goutils.LogMetadataModifier{
goutils.ModifyLogMetadataByRestRequestParam,
},
},
CallRequestIDHeaderField: &logConfig.RequestIDHeader,
DoNotLogHeaders: func() map[string]bool {
result := map[string]bool{}
for _, v := range logConfig.DoNotLogHeaders {
result[v] = true
}
return result
}(),
LogLevel: logConfig.LogLevel,
MetricsHelper: metrics,
},
oidClient: oid,
performIntrospect: performIntrospect,
introspector: introspector,
targetAudience: authnCfg.TargetAudience,
targetClaims: authnCfg.TargetClaims,
reqHeaderParam: authnCfg.RequestParamLocation,
respHeaderParam: respHeaderParam,
bypassChecker: nil,
}
if authnCfg.Bypass != nil {
bypassCheck, err := match.DefineAuthBypassMatch(*authnCfg.Bypass)
if err != nil {
log.WithError(err).WithFields(logTags).Error("Failed define authentication bypass matcher")
return AuthenticationHandler{}, err
}
instance.bypassChecker = bypassCheck
}
return instance, nil
}
// ====================================================================================
// Authenticate
// Authenticate godoc
// @Summary Authenticate a user
// @Description Authticate a user by verifiying the bearer token provided
// @tags Authenticate
// @Produce json
// @Param Padlock-Request-ID header string false "User provided request ID to match against logs"
// @Param Authorization header string true "User must provide a bearer token"
// @Success 200 {object} goutils.RestAPIBaseResponse "success"
// @Failure 400 {object} goutils.RestAPIBaseResponse "error"
// @Failure 401 {string} string "error"
// @Failure 403 {string} string "error"
// @Failure 404 {string} string "error"
// @Failure 500 {object} goutils.RestAPIBaseResponse "error"
// @Router /v1/authenticate [get]
func (h AuthenticationHandler) Authenticate(w http.ResponseWriter, r *http.Request) {
var respCode int
var response interface{}
respHeaders := map[string]string{}
logTags := h.GetLogTagsForContext(r.Context())
defer func() {
if err := h.WriteRESTResponse(w, respCode, response, respHeaders); err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to form response")
}
}()
// Check Bypass rules first
if h.bypassChecker != nil {
// Pull request parameters
t := common.AccessAuthorizeParam{
Method: r.Header.Get(h.reqHeaderParam.Method),
Path: r.Header.Get(h.reqHeaderParam.Path),
Host: r.Header.Get(h.reqHeaderParam.Host),
}
params := match.RequestParam{Host: &t.Host, Method: t.Method, Path: t.Path}
matched, err := h.bypassChecker.Match(r.Context(), params)
if err != nil {
msg := "authn bypass check failed"
log.WithError(err).WithFields(logTags).Error(msg)
respCode = http.StatusBadRequest
response = h.GetStdRESTErrorMsg(r.Context(), http.StatusBadRequest, msg, err.Error())
return
}
// Bypass authentication
if matched {
respCode = http.StatusOK
response = h.GetStdRESTSuccessMsg(r.Context())
return
}
}
errMacroNoErr := func(msg string) {
log.WithFields(logTags).Errorf(msg)
respCode = http.StatusUnauthorized
response = h.GetStdRESTErrorMsg(r.Context(), http.StatusUnauthorized, msg, "")
}
// Read the JWT Bearer token
bearer := r.Header.Get("Authorization")
if bearer == "" {
errMacroNoErr("Header 'Authorization' missing")
return
}
bearerParts := strings.Split(bearer, " ")
if len(bearerParts) != 2 {
errMacroNoErr("Bearer 'Authorization' has incorrect format")
return
}
rawToken := bearerParts[1]
errMacro := func(msg string, err error) {
log.WithError(err).WithFields(logTags).Errorf(msg)
respCode = http.StatusUnauthorized
response = h.GetStdRESTErrorMsg(r.Context(), http.StatusUnauthorized, msg, err.Error())
}
// Parse the JWT token
userClaims := new(jwt.MapClaims)
_, err := h.oidClient.ParseJWT(rawToken, userClaims)
if err != nil {
errMacro("Unable to parse JWT bearer token", err)
return
}
{
t, _ := json.MarshalIndent(userClaims, "", " ")
log.WithFields(logTags).Debugf("Token claims\n%s", t)
}
fetchClaimAsString := func(target string) (string, error) {
if v, ok := (*userClaims)[target]; ok {
if value, ok := v.(string); ok {
return value, nil
}
return "", fmt.Errorf("bearer 'Authorization' token's claim %s is not a string", target)
}
return "", fmt.Errorf("bearer 'Authorization' token missing %s", target)
}
fetchClaimAsFloat := func(target string) (float64, error) {
if v, ok := (*userClaims)[target]; ok {
if value, ok := v.(float64); ok {
return value, nil
}
return 0, fmt.Errorf("bearer 'Authorization' token's claim %s is not a FLOAT64", target)
}
return 0, fmt.Errorf("bearer 'Authorization' token missing %s", target)
}
// OAuth2 introspect
if h.performIntrospect {
if !h.oidClient.CanIntrospect() {
errMacroNoErr("Missing required settings to perform introspection")
return
}
expirationTime, err := fetchClaimAsFloat("exp")
if err != nil {
errMacro("Unable to parse out 'exp' claim", err)
return
}
isValid, err := h.introspector.VerifyToken(
r.Context(), rawToken, int64(expirationTime), time.Now().UTC(),
)
if err != nil {
errMacro("Introspection process errored", err)
return
}
if !isValid {
errMacroNoErr("Token no longer active")
return
}
}
errMacro = func(msg string, err error) {
log.WithError(err).WithFields(logTags).Errorf(msg)
respCode = http.StatusBadRequest
response = h.GetStdRESTErrorMsg(r.Context(), http.StatusBadRequest, msg, err.Error())
}
// Check "aud" if target audience specified
if h.targetAudience != nil {
aud, err := fetchClaimAsString("aud")
if err != nil {
errMacro("Unable to parse out 'aud' claim", err)
return
}
// Verify audience matches
if aud != *h.targetAudience {
err := fmt.Errorf("'aud' claim does not match expectation")
errMacro("Invalid token", err)
return
}
}
// Parse out the critical fields
userParams := models.UserConfig{}
// User ID
uid, err := fetchClaimAsString(h.targetClaims.UserIDClaim)
if err != nil {
errMacro(fmt.Sprintf("Unable to parse out '%s' claim", h.targetClaims.UserIDClaim), err)
return
}
userParams.UserID = uid
respHeaders[h.respHeaderParam.UserID] = uid
// User name
if h.targetClaims.UsernameClaim != nil {
username, err := fetchClaimAsString(*h.targetClaims.UsernameClaim)
if err != nil {
errMacro(fmt.Sprintf("Unable to parse out '%s' claim", *h.targetClaims.UsernameClaim), err)
return
}
userParams.Username = &username
respHeaders[h.respHeaderParam.Username] = username
}
// First name
if h.targetClaims.FirstNameClaim != nil {
firstName, err := fetchClaimAsString(*h.targetClaims.FirstNameClaim)
if err != nil {
errMacro(fmt.Sprintf("Unable to parse out '%s' claim", *h.targetClaims.FirstNameClaim), err)
return
}
userParams.FirstName = &firstName
respHeaders[h.respHeaderParam.FirstName] = firstName
}
// Last name
if h.targetClaims.LastNameClaim != nil {
lastName, err := fetchClaimAsString(*h.targetClaims.LastNameClaim)
if err != nil {
errMacro(fmt.Sprintf("Unable to parse out '%s' claim", *h.targetClaims.LastNameClaim), err)
return
}
userParams.LastName = &lastName
respHeaders[h.respHeaderParam.LastName] = lastName
}
// Email
if h.targetClaims.EmailClaim != nil {
email, err := fetchClaimAsString(*h.targetClaims.EmailClaim)
if err != nil {
errMacro(fmt.Sprintf("Unable to parse out '%s' claim", *h.targetClaims.EmailClaim), err)
return
}
userParams.Email = &email
respHeaders[h.respHeaderParam.Email] = email
}
{
t, _ := json.MarshalIndent(userParams, "", " ")
log.WithFields(logTags).Debugf("User parameters in Token\n%s", t)
}
// Set the response headers
respCode = http.StatusOK
response = h.GetStdRESTSuccessMsg(r.Context())
}
// AuthenticateHandler Wrapper around Authenticate
func (h AuthenticationHandler) AuthenticateHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.Authenticate(w, r)
}
}
// ====================================================================================
// Utilities
// AuthenticationLivenessHandler the request authentication REST API liveness handler
type AuthenticationLivenessHandler struct {
goutils.RestAPIHandler
}
func defineAuthenticationLivenessHandler(
logConfig common.HTTPRequestLogging,
) AuthenticationLivenessHandler {
logTags := log.Fields{
"module": "apis", "component": "api-handler", "instance": "authentication-liveness",
}
return AuthenticationLivenessHandler{
RestAPIHandler: goutils.RestAPIHandler{
Component: goutils.Component{
LogTags: logTags,
LogTagModifiers: []goutils.LogMetadataModifier{
goutils.ModifyLogMetadataByRestRequestParam,
},
},
CallRequestIDHeaderField: &logConfig.RequestIDHeader,
DoNotLogHeaders: func() map[string]bool {
result := map[string]bool{}
for _, v := range logConfig.DoNotLogHeaders {
result[v] = true
}
return result
}(),
LogLevel: logConfig.HealthLogLevel,
},
}
}
// Alive godoc
// @Summary Authentication API liveness check
// @Description Will return success to indicate Authentication REST API module is live
// @tags Authenticate
// @Produce json
// @Param Padlock-Request-ID header string false "User provided request ID to match against logs"
// @Success 200 {object} goutils.RestAPIBaseResponse "success"
// @Failure 400 {object} goutils.RestAPIBaseResponse "error"
// @Failure 404 {string} string "error"
// @Failure 500 {object} goutils.RestAPIBaseResponse "error"
// @Router /v1/alive [get]
func (h AuthenticationLivenessHandler) Alive(w http.ResponseWriter, r *http.Request) {
logTags := h.GetLogTagsForContext(r.Context())
if err := h.WriteRESTResponse(
w, http.StatusOK, h.GetStdRESTSuccessMsg(r.Context()), nil,
); err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to form response")
}
}
// AliveHandler Wrapper around Alive
func (h AuthenticationLivenessHandler) AliveHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.Alive(w, r)
}
}
// -----------------------------------------------------------------------
// Ready godoc
// @Summary Authentication API readiness check
// @Description Will return success if Authentication REST API module is ready for use
// @tags Authenticate
// @Produce json
// @Param Padlock-Request-ID header string false "User provided request ID to match against logs"
// @Success 200 {object} goutils.RestAPIBaseResponse "success"
// @Failure 400 {object} goutils.RestAPIBaseResponse "error"
// @Failure 404 {string} string "error"
// @Failure 500 {object} goutils.RestAPIBaseResponse "error"
// @Router /v1/ready [get]
func (h AuthenticationLivenessHandler) Ready(w http.ResponseWriter, r *http.Request) {
var respCode int
var response interface{}
logTags := h.GetLogTagsForContext(r.Context())
defer func() {
if err := h.WriteRESTResponse(w, respCode, response, nil); err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to form response")
}
}()
respCode = http.StatusOK
response = h.GetStdRESTSuccessMsg(r.Context())
}
// ReadyHandler Wrapper around Alive
func (h AuthenticationLivenessHandler) ReadyHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.Ready(w, r)
}
}