28 lines
657 B
Go
28 lines
657 B
Go
package auth
|
|
|
|
import "context"
|
|
|
|
type requestMetaKey struct{}
|
|
|
|
// RequestMeta carries client audit fields injected by handlers.
|
|
type RequestMeta struct {
|
|
ClientIP string
|
|
UserAgent string
|
|
}
|
|
|
|
// WithRequestMeta attaches client metadata to context.
|
|
func WithRequestMeta(ctx context.Context, meta RequestMeta) context.Context {
|
|
return context.WithValue(ctx, requestMetaKey{}, meta)
|
|
}
|
|
|
|
// RequestMetaFromContext reads client metadata from context.
|
|
func RequestMetaFromContext(ctx context.Context) RequestMeta {
|
|
if ctx == nil {
|
|
return RequestMeta{}
|
|
}
|
|
if meta, ok := ctx.Value(requestMetaKey{}).(RequestMeta); ok {
|
|
return meta
|
|
}
|
|
return RequestMeta{}
|
|
}
|