mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
This fixes the OriginalIP and RequestUserID in the main featurebase package, and the Access and Refresh tokens, the UserInfo, and the []string of Indexes passed with context.Context(s) in the authn package. An empty struct was used for all of these keys (and relevant helper functions we added) to avoid allocations where possible while still using the context functionality. Some of the logic in the server.GetIndexes function was fixed.
28 lines
934 B
Go
28 lines
934 B
Go
// Copyright 2022 Molecula Corp (DBA FeatureBase). All rights reserved.
|
|
package pilosa
|
|
|
|
import "context"
|
|
|
|
// Empty struct to avoid allocations
|
|
type contextKeyOriginalIP struct{}
|
|
type contextKeyRequestUserID struct{}
|
|
|
|
// OriginalIPFromContext gets the original IP from the context.
|
|
func OriginalIPFromContext(ctx context.Context) (originalIP string, ok bool) {
|
|
originalIP, ok = ctx.Value(contextKeyOriginalIP{}).(string)
|
|
return
|
|
}
|
|
|
|
// WithOriginalIP makes a new context with the originalIP in the context.
|
|
func WithOriginalIP(ctx context.Context, originalIP string) context.Context {
|
|
return context.WithValue(ctx, contextKeyOriginalIP{}, originalIP)
|
|
}
|
|
|
|
func UserIDFromContext(ctx context.Context) (userID string, ok bool) {
|
|
userID, ok = ctx.Value(contextKeyRequestUserID{}).(string)
|
|
return
|
|
}
|
|
|
|
func WithUserID(ctx context.Context, userID string) context.Context {
|
|
return context.WithValue(ctx, contextKeyRequestUserID{}, userID)
|
|
}
|