func authenticatedUser(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
	authUserFunc := func(ctx context.Context, request interface{}) (interface{}, error) {
		// first check if already successfully set
		if v, ok := viewer.FromContext(ctx); ok {
			if v.User.IsAdminForcedPasswordReset() {
				return nil, fleet.ErrPasswordResetRequired
			}

			return next(ctx, request)
		}

		// if not succesful, try again this time with errors
		sessionKey, ok := token.FromContext(ctx)
		if !ok {
			return nil, fleet.NewAuthHeaderRequiredError("no auth token")
		}

		v, err := authViewer(ctx, string(sessionKey), svc)
		if err != nil {
			return nil, err
		}

		if v.User.IsAdminForcedPasswordReset() {
			return nil, fleet.ErrPasswordResetRequired
		}

		ctx = viewer.NewContext(ctx, *v)
		return next(ctx, request)
	}

	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		res, err := authUserFunc(ctx, request)
		if err != nil {
			logging.WithErr(ctx, err)
			return nil, err
		}
		return res, nil
	}
}
// authenticatedUser wraps an endpoint, requires that the Fleet user is
// authenticated, and populates the context with a Viewer struct for that user.
//
// If auth fails or the user must reset their password, an error is returned.
authenticatedUser wraps an endpoint, requires that the Fleet user is
authenticated, and populates the context with a Viewer struct for that user.

If auth fails or the user must reset their password, an error is returned.
// authenticatedUser wraps an endpoint requires that the Fleet user is // authenticated and populates the context with a Viewer struct for that user . // // If auth fails or the user must reset their password an error is returned .
func authenticatedUser(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
	authUserFunc := func(ctx context.Context, request interface{}) (interface{}, error) {
		if v, ok := viewer.FromContext(ctx); ok {
			if v.User.IsAdminForcedPasswordReset() {
				return nil, fleet.ErrPasswordResetRequired
			}
			return next(ctx, request)
		}
		sessionKey, ok := token.FromContext(ctx)
		if !ok {
			return nil, fleet.NewAuthHeaderRequiredError("no auth token")
		}
		v, err := authViewer(ctx, string(sessionKey), svc)
		if err != nil {
			return nil, err
		}
		if v.User.IsAdminForcedPasswordReset() {
			return nil, fleet.ErrPasswordResetRequired
		}
		ctx = viewer.NewContext(ctx, *v)
		return next(ctx, request)
	}
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		res, err := authUserFunc(ctx, request)
		if err != nil {
			logging.WithErr(ctx, err)
			return nil, err
		}
		return res, nil
	}
}
func authenticatedUser ( svc fleet . Service , next endpoint . Endpoint ) endpoint . Endpoint { authUserFunc := func ( ctx context . Context , request interface { } ) ( interface { } , error ) { if v , ok := viewer . FromContext ( ctx ) ; ok { if v . User . IsAdminForcedPasswordReset ( ) { return nil , fleet . ErrPasswordResetRequired } return next ( ctx , request ) } sessionKey , ok := token . FromContext ( ctx ) if ! ok { return nil , fleet . NewAuthHeaderRequiredError ( " " ) } v , err := authViewer ( ctx , string ( sessionKey ) , svc ) if err != nil { return nil , err } if v . User . IsAdminForcedPasswordReset ( ) { return nil , fleet . ErrPasswordResetRequired } ctx = viewer . NewContext ( ctx , * v ) return next ( ctx , request ) } return func ( ctx context . Context , request interface { } ) ( response interface { } , err error ) { res , err := authUserFunc ( ctx , request ) if err != nil { logging . WithErr ( ctx , err ) return nil , err } return res , nil } }
authenticatedUser wraps an endpoint, requires that the Fleet user is
authenticated, and populates the context with a Viewer struct for that user.
authenticatedUser wraps an endpoint requires that the Fleet user is authenticated and populates the context with a Viewer struct for that user .
// first check if already successfully set // if not succesful, try again this time with errors