Skip to content

Commit 5860a2e

Browse files
committed
Revert changes and improve middleware declarations
1 parent 1ef90ba commit 5860a2e

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

cmds/core-service/main.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,10 @@ func RunHTTPServer(ctx context.Context, ctxCanceler func(), address, locality st
315315
multiRouter.Routers = append(multiRouter.Routers, &scdV1Router)
316316
}
317317

318-
handler := logging.HTTPMiddleware(logger, *dumpRequests,
319-
healthyEndpointMiddleware(logger,
320-
&multiRouter,
321-
))
322-
323-
handler = authDecoderMiddleware(authorizer, handler)
318+
// the middlewares are wrapped and, therefore, executed in the opposite order
319+
handler := healthyEndpointMiddleware(logger, &multiRouter)
320+
handler = logging.HTTPMiddleware(logger, *dumpRequests, handler)
321+
handler = authMiddleware(authorizer, handler)
324322

325323
httpServer := &http.Server{
326324
Addr: address,
@@ -382,8 +380,8 @@ func healthyEndpointMiddleware(logger *zap.Logger, next http.Handler) http.Handl
382380
})
383381
}
384382

385-
// authDecoderMiddleware decodes the authentication token and adds the Subject claim to the context.
386-
func authDecoderMiddleware(authorizer *auth.Authorizer, handler http.Handler) http.Handler {
383+
// authMiddleware decodes the authentication token and passes the claims to the context.
384+
func authMiddleware(authorizer *auth.Authorizer, handler http.Handler) http.Handler {
387385
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
388386
var ctx context.Context
389387
claims, err := authorizer.ExtractClaims(r)
@@ -400,13 +398,13 @@ func authDecoderMiddleware(authorizer *auth.Authorizer, handler http.Handler) ht
400398

401399
var errMsg string
402400
if err != nil {
401+
//remove the stacktrace using the formatting specifier "%#s"
403402
errMsg = fmt.Sprintf("%#s", err)
404403
}
405404

406405
ctx = context.WithValue(ctx, logging.CtxAuthKey{}, logging.CtxAuthValue{
407406
Subject: claims.Subject,
408-
//remove the stacktrace using the formatting specifier "%#s"
409-
ErrMsg: errMsg,
407+
ErrMsg: errMsg,
410408
})
411409

412410
handler.ServeHTTP(w, r.WithContext(ctx))

pkg/auth/auth.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,20 @@ type CtxAuthValue struct {
191191

192192
// Authorize extracts and verifies bearer tokens from a http.Request.
193193
func (a *Authorizer) Authorize(_ http.ResponseWriter, r *http.Request, authOptions []api.AuthorizationOption) api.AuthorizationResult {
194-
v := r.Context().Value(CtxAuthKey{}).(CtxAuthValue)
195-
if v.Error != nil {
196-
return api.AuthorizationResult{Error: stacktrace.PropagateWithCode(v.Error, dsserr.Unauthenticated, "Invalid access token")}
194+
authResults := r.Context().Value(CtxAuthKey{}).(CtxAuthValue)
195+
if authResults.Error != nil {
196+
return api.AuthorizationResult{Error: stacktrace.PropagateWithCode(authResults.Error, dsserr.Unauthenticated, "Invalid access token")}
197197
}
198198

199-
if pass, missing := validateScopes(authOptions, v.Claims.Scopes); !pass {
199+
if pass, missing := validateScopes(authOptions, authResults.Claims.Scopes); !pass {
200200
return api.AuthorizationResult{Error: stacktrace.NewErrorWithCode(dsserr.PermissionDenied,
201201
"Access token missing scopes (%v) while expecting %v and got %v",
202-
missing, describeAuthorizationExpectations(authOptions), strings.Join(v.Claims.Scopes.ToStringSlice(), ", "))}
202+
missing, describeAuthorizationExpectations(authOptions), strings.Join(authResults.Claims.Scopes.ToStringSlice(), ", "))}
203203
}
204204

205205
return api.AuthorizationResult{
206-
ClientID: &v.Claims.Subject,
207-
Scopes: v.Claims.Scopes.ToStringSlice(),
206+
ClientID: &authResults.Claims.Subject,
207+
Scopes: authResults.Claims.Scopes.ToStringSlice(),
208208
}
209209
}
210210

pkg/logging/http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func HTTPMiddleware(logger *zap.Logger, dump bool, handler http.Handler) http.Ha
7575
}
7676
}
7777

78-
v := r.Context().Value(CtxAuthKey{}).(CtxAuthValue)
79-
if v.ErrMsg != "" {
80-
logger = logger.With(zap.String("resp_sub_err", v.ErrMsg))
78+
authResults := r.Context().Value(CtxAuthKey{}).(CtxAuthValue)
79+
if authResults.ErrMsg != "" {
80+
logger = logger.With(zap.String("resp_sub_err", authResults.ErrMsg))
8181
} else {
82-
logger = logger.With(zap.String("req_sub", v.Subject))
82+
logger = logger.With(zap.String("req_sub", authResults.Subject))
8383
}
8484

8585
handler.ServeHTTP(trw, r)

0 commit comments

Comments
 (0)