2021-06-03 23:24:15 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-01-16 15:22:12 +00:00
|
|
|
authz_ctx "github.com/fleetdm/fleet/v4/server/contexts/authz"
|
|
|
|
hostctx "github.com/fleetdm/fleet/v4/server/contexts/host"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-06-03 23:24:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserContext returns a new context with the provided user as the viewer.
|
2022-11-15 14:08:05 +00:00
|
|
|
func UserContext(ctx context.Context, user *fleet.User) context.Context {
|
|
|
|
return viewer.NewContext(ctx, viewer.Viewer{User: user})
|
2021-06-03 23:24:15 +00:00
|
|
|
}
|
2023-01-16 15:22:12 +00:00
|
|
|
|
|
|
|
// HostContext returns a new context with the provided host as the
|
|
|
|
// device-authenticated host.
|
|
|
|
func HostContext(ctx context.Context, host *fleet.Host) context.Context {
|
|
|
|
authzCtx := &authz_ctx.AuthorizationContext{}
|
|
|
|
ctx = authz_ctx.NewContext(ctx, authzCtx)
|
|
|
|
ctx = hostctx.NewContext(ctx, host)
|
|
|
|
if ac, ok := authz_ctx.FromContext(ctx); ok {
|
|
|
|
ac.SetAuthnMethod(authz_ctx.AuthnDeviceToken)
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|