Skip to content

Commit

Permalink
Move user ID handling to userid package
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Feb 28, 2024
1 parent 7e5f109 commit 06696e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
20 changes: 20 additions & 0 deletions internal/userid/userid.go
@@ -0,0 +1,20 @@
package userid

import "context"

type userIDKey struct{}

// NewContext returns a new context with the given user ID added to the
// context.
// TODO(hs): this doesn't seem to be used / set currently; implement
// when/where it makes sense.
func NewContext(ctx context.Context, userID string) context.Context {
return context.WithValue(ctx, userIDKey{}, userID)
}

// FromContext returns the user ID from the context if it exists
// and is not empty.
func FromContext(ctx context.Context) (string, bool) {
v, ok := ctx.Value(userIDKey{}).(string)
return v, ok && v != ""
}
19 changes: 0 additions & 19 deletions logging/context.go

This file was deleted.

3 changes: 2 additions & 1 deletion logging/handler.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/smallstep/certificates/internal/requestid"
"github.com/smallstep/certificates/internal/userid"
)

// LoggerHandler creates a logger handler
Expand Down Expand Up @@ -60,7 +61,7 @@ func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Tim
if v, ok := requestid.FromContext(ctx); ok {
requestID = v
}
if v, ok := GetUserID(ctx); ok && v != "" {
if v, ok := userid.FromContext(ctx); ok {
userID = v
}

Expand Down

0 comments on commit 06696e6

Please sign in to comment.