github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/track.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package service
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/keybase/client/go/engine"
    10  	"github.com/keybase/client/go/libkb"
    11  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  // TrackHandler is the RPC handler for the track interface.
    17  type TrackHandler struct {
    18  	*BaseHandler
    19  	libkb.Contextified
    20  }
    21  
    22  var _ keybase1.TrackInterface = (*TrackHandler)(nil)
    23  
    24  // NewTrackHandler creates a TrackHandler for the xp transport.
    25  func NewTrackHandler(xp rpc.Transporter, g *libkb.GlobalContext) *TrackHandler {
    26  	return &TrackHandler{
    27  		BaseHandler:  NewBaseHandler(g, xp),
    28  		Contextified: libkb.NewContextified(g),
    29  	}
    30  }
    31  
    32  // Track creates a TrackEngine and runs it.
    33  func (h *TrackHandler) Track(ctx context.Context, arg keybase1.TrackArg) (keybase1.ConfirmResult, error) {
    34  	earg := engine.TrackEngineArg{
    35  		UserAssertion:    arg.UserAssertion,
    36  		Options:          arg.Options,
    37  		ForceRemoteCheck: arg.ForceRemoteCheck,
    38  	}
    39  	uis := libkb.UIs{
    40  		IdentifyUI: h.NewRemoteIdentifyUI(arg.SessionID, h.G()),
    41  		SecretUI:   h.getSecretUI(arg.SessionID, h.G()),
    42  		SessionID:  arg.SessionID,
    43  	}
    44  	eng := engine.NewTrackEngine(h.G(), &earg)
    45  	m := libkb.NewMetaContext(ctx, h.G()).WithUIs(uis)
    46  	err := engine.RunEngine2(m, eng)
    47  	res := eng.ConfirmResult()
    48  	return res, err
    49  }
    50  
    51  func (h *TrackHandler) TrackWithToken(ctx context.Context, arg keybase1.TrackWithTokenArg) error {
    52  	earg := engine.TrackTokenArg{
    53  		Token:   arg.TrackToken,
    54  		Options: arg.Options,
    55  	}
    56  	uis := libkb.UIs{
    57  		IdentifyUI: h.NewRemoteIdentifyUI(arg.SessionID, h.G()),
    58  		SecretUI:   h.getSecretUI(arg.SessionID, h.G()),
    59  		SessionID:  arg.SessionID,
    60  	}
    61  	eng := engine.NewTrackToken(h.G(), &earg)
    62  	m := libkb.NewMetaContext(ctx, h.G()).WithUIs(uis)
    63  	return engine.RunEngine2(m, eng)
    64  }
    65  
    66  func (h *TrackHandler) DismissWithToken(ctx context.Context, arg keybase1.DismissWithTokenArg) error {
    67  	outcome, err := h.G().TrackCache().Get(arg.TrackToken)
    68  	if err != nil {
    69  		h.G().Log.Error("Failed to get track token", err)
    70  		return err
    71  	}
    72  	if outcome.ResponsibleGregorItem == nil {
    73  		h.G().Log.Debug("No responsible gregor item found for track token %s", arg.TrackToken)
    74  		return nil
    75  	}
    76  
    77  	return h.G().GregorState.DismissItem(ctx, nil, outcome.ResponsibleGregorItem.Metadata().MsgID())
    78  }
    79  
    80  // Untrack creates an UntrackEngine and runs it.
    81  func (h *TrackHandler) Untrack(ctx context.Context, arg keybase1.UntrackArg) error {
    82  	earg := engine.UntrackEngineArg{
    83  		Username: libkb.NewNormalizedUsername(arg.Username),
    84  	}
    85  	uis := libkb.UIs{
    86  		SecretUI:  h.getSecretUI(arg.SessionID, h.G()),
    87  		SessionID: arg.SessionID,
    88  	}
    89  	eng := engine.NewUntrackEngine(h.G(), &earg)
    90  	m := libkb.NewMetaContext(ctx, h.G()).WithUIs(uis)
    91  	return engine.RunEngine2(m, eng)
    92  }
    93  
    94  func (h *TrackHandler) CheckTracking(_ context.Context, sessionID int) error {
    95  	if !h.G().RateLimits.GetPermission(libkb.CheckTrackingRateLimit, libkb.TrackingRateLimitSeconds*time.Second) {
    96  		h.G().Log.Debug("Skipping CheckTracking due to rate limit.")
    97  		return nil
    98  	}
    99  	return libkb.CheckTracking(h.G())
   100  }
   101  
   102  func (h *TrackHandler) FakeTrackingChanged(_ context.Context, arg keybase1.FakeTrackingChangedArg) error {
   103  	user, err := libkb.LoadUser(libkb.NewLoadUserArg(h.G()).WithName(arg.Username))
   104  	if err != nil {
   105  		return err
   106  	}
   107  	h.G().NotifyRouter.HandleTrackingChanged(user.GetUID(), user.GetNormalizedName(), arg.IsTracking)
   108  	return nil
   109  }