Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth middleware #126

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions basculehttp/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ type MetricListenerIn struct {
// ChainIn is used for uber fx wiring.
type ChainIn struct {
fx.In
SetLogger alice.Constructor `name:"alice_set_logger"`
Constructor alice.Constructor `name:"alice_constructor"`
Enforcer alice.Constructor `name:"alice_enforcer"`
Listener alice.Constructor `name:"alice_listener"`
SetLogger alice.Constructor `name:"alice_set_logger"`
Constructor alice.Constructor `name:"alice_constructor"`
Enforcer alice.Constructor `name:"alice_enforcer"`
Listener alice.Constructor `name:"alice_listener"`
SetLoggerInfo alice.Constructor `name:"alice_set_logger_info"`
}

// Build provides the alice constructors chained together in a set order.
func (c ChainIn) Build() alice.Chain {
return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener)
return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener, c.SetLoggerInfo)
}

// ProvideServerChain builds the alice middleware and then provides them
Expand Down
17 changes: 13 additions & 4 deletions basculehttp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,35 @@ func ProvideLogger() fx.Option {
return WithELogger(getZapLogger(getLogger))
},
},

// add info to logger
fx.Annotated{
Name: "alice_set_logger_info",
Target: SetBasculeInfo,
},
),
)
}

// SetBasculeInfo takes the logger and bascule Auth out of the context and adds
// SetBasculeInfo creates an alice constructor that takes
// the logger and bascule Auth and adds
// relevant bascule information to the logger before putting the logger
// back in the context.
func SetBasculeInfo(ctx context.Context) alice.Constructor {
func SetBasculeInfo() alice.Constructor {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := sallust.Get(ctx)

var satClientID = "N/A"
// retrieve satClientID from request context
if auth, ok := bascule.FromContext(r.Context()); ok {
satClientID = auth.Token.Principal()
}

logger := sallust.Get(ctx)
logger.With(zap.String("satClientID", satClientID))

sallust.With(ctx, logger)
r = r.WithContext(sallust.With(ctx, logger))

delegate.ServeHTTP(w, r)
})
Expand Down