The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/apimachinery/identity/context.go

31 lines
815 B

package identity
import (
"context"
"fmt"
"reflect"
"github.com/grafana/authlib/claims"
)
type ctxUserKey struct{}
// WithRequester attaches the requester to the context.
func WithRequester(ctx context.Context, usr Requester) context.Context {
ctx = claims.WithClaims(ctx, usr) // also set the upstream auth info claims
return context.WithValue(ctx, ctxUserKey{}, usr)
}
// Get the Requester from context
func GetRequester(ctx context.Context) (Requester, error) {
// Set by appcontext.WithUser
u, ok := ctx.Value(ctxUserKey{}).(Requester)
if ok && !checkNilRequester(u) {
return u, nil
}
return nil, fmt.Errorf("a Requester was not found in the context")
}
func checkNilRequester(r Requester) bool {
return r == nil || (reflect.ValueOf(r).Kind() == reflect.Ptr && reflect.ValueOf(r).IsNil())
}