meta.go 918 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package audit
  2. import (
  3. "context"
  4. )
  5. // RequestMeta carries per-request caller identity parsed from HTTP headers.
  6. // udeServer has no auth layer yet, so the fields stay empty unless a future
  7. // middleware fills them in.
  8. type RequestMeta struct {
  9. UserId string
  10. UserName string
  11. UserToken string
  12. ApiKey string
  13. ApiSecret string
  14. ApiHmac string
  15. }
  16. type ctxKeyRequestMeta struct{}
  17. // RequestMetaKey is the context key of RequestMeta.
  18. var RequestMetaKey = ctxKeyRequestMeta{}
  19. // WithRequestMeta stores RequestMeta in ctx.
  20. func WithRequestMeta(ctx context.Context, m RequestMeta) context.Context {
  21. return context.WithValue(ctx, RequestMetaKey, m)
  22. }
  23. // GetRequestMeta returns the RequestMeta carried by ctx, or the zero value.
  24. func GetRequestMeta(ctx context.Context) RequestMeta {
  25. if v := ctx.Value(RequestMetaKey); v != nil {
  26. if m, ok := v.(RequestMeta); ok {
  27. return m
  28. }
  29. }
  30. return RequestMeta{}
  31. }