github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/handler.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  	"github.com/keybase/client/go/libkb"
     8  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     9  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  type BaseHandler struct {
    14  	xp        rpc.Transporter
    15  	cli       *rpc.Client
    16  	loginCli  *keybase1.LoginUiClient
    17  	secretCli *keybase1.SecretUiClient
    18  	logCli    *keybase1.LogUiClient
    19  }
    20  
    21  func NewBaseHandler(g *libkb.GlobalContext, xp rpc.Transporter) *BaseHandler {
    22  	h := &BaseHandler{xp: xp}
    23  	h.cli = rpc.NewClient(h.xp, libkb.NewContextifiedErrorUnwrapper(g), nil)
    24  	h.loginCli = &keybase1.LoginUiClient{Cli: h.cli}
    25  	h.secretCli = &keybase1.SecretUiClient{Cli: h.cli}
    26  	h.logCli = &keybase1.LogUiClient{Cli: h.cli}
    27  
    28  	return h
    29  }
    30  
    31  type LoginUI struct {
    32  	sessionID int
    33  	cli       *keybase1.LoginUiClient
    34  }
    35  
    36  func (u *LoginUI) GetEmailOrUsername(ctx context.Context, _ int) (string, error) {
    37  	return u.cli.GetEmailOrUsername(ctx, u.sessionID)
    38  }
    39  
    40  func (u *LoginUI) PromptRevokePaperKeys(ctx context.Context, arg keybase1.PromptRevokePaperKeysArg) (bool, error) {
    41  	arg.SessionID = u.sessionID
    42  	return u.cli.PromptRevokePaperKeys(ctx, arg)
    43  }
    44  
    45  func (u *LoginUI) DisplayPaperKeyPhrase(ctx context.Context, arg keybase1.DisplayPaperKeyPhraseArg) error {
    46  	arg.SessionID = u.sessionID
    47  	return u.cli.DisplayPaperKeyPhrase(ctx, arg)
    48  }
    49  
    50  func (u *LoginUI) DisplayPrimaryPaperKey(ctx context.Context, arg keybase1.DisplayPrimaryPaperKeyArg) error {
    51  	arg.SessionID = u.sessionID
    52  	return u.cli.DisplayPrimaryPaperKey(ctx, arg)
    53  }
    54  
    55  func (u *LoginUI) PromptResetAccount(ctx context.Context,
    56  	arg keybase1.PromptResetAccountArg) (keybase1.ResetPromptResponse, error) {
    57  	arg.SessionID = u.sessionID
    58  	return u.cli.PromptResetAccount(ctx, arg)
    59  }
    60  
    61  func (u *LoginUI) DisplayResetProgress(ctx context.Context, arg keybase1.DisplayResetProgressArg) error {
    62  	arg.SessionID = u.sessionID
    63  	return u.cli.DisplayResetProgress(ctx, arg)
    64  }
    65  
    66  func (u *LoginUI) ExplainDeviceRecovery(ctx context.Context, arg keybase1.ExplainDeviceRecoveryArg) error {
    67  	arg.SessionID = u.sessionID
    68  	return u.cli.ExplainDeviceRecovery(ctx, arg)
    69  }
    70  
    71  func (u *LoginUI) PromptPassphraseRecovery(ctx context.Context, arg keybase1.PromptPassphraseRecoveryArg) (bool, error) {
    72  	arg.SessionID = u.sessionID
    73  	return u.cli.PromptPassphraseRecovery(ctx, arg)
    74  }
    75  
    76  func (u *LoginUI) ChooseDeviceToRecoverWith(ctx context.Context, arg keybase1.ChooseDeviceToRecoverWithArg) (keybase1.DeviceID, error) {
    77  	arg.SessionID = u.sessionID
    78  	return u.cli.ChooseDeviceToRecoverWith(ctx, arg)
    79  }
    80  
    81  func (u *LoginUI) DisplayResetMessage(ctx context.Context, arg keybase1.DisplayResetMessageArg) error {
    82  	arg.SessionID = u.sessionID
    83  	return u.cli.DisplayResetMessage(ctx, arg)
    84  }
    85  
    86  type SecretUI struct {
    87  	sessionID int
    88  	cli       *keybase1.SecretUiClient
    89  	libkb.Contextified
    90  }
    91  
    92  // GetPassphrase gets the current keybase passphrase from delegated pinentry.
    93  func (u *SecretUI) GetPassphrase(pinentry keybase1.GUIEntryArg, terminal *keybase1.SecretEntryArg) (keybase1.GetPassphraseRes, error) {
    94  	u.G().Log.Debug("SecretUI:GetPassphrase, sessionID = %d", u.sessionID)
    95  	return u.cli.GetPassphrase(context.TODO(), keybase1.GetPassphraseArg{SessionID: u.sessionID, Pinentry: pinentry, Terminal: terminal})
    96  }
    97  
    98  func (h *BaseHandler) rpcClient() *rpc.Client {
    99  	return h.cli
   100  }
   101  
   102  func (h *BaseHandler) getLoginUICli() *keybase1.LoginUiClient {
   103  	return h.loginCli
   104  }
   105  
   106  func (h *BaseHandler) getLoginUI(sessionID int) libkb.LoginUI {
   107  	return &LoginUI{sessionID, h.getLoginUICli()}
   108  }
   109  
   110  func (h *BaseHandler) getGPGUI(sessionID int) libkb.GPGUI {
   111  	return NewRemoteGPGUI(sessionID, h.rpcClient())
   112  }
   113  
   114  func (h *BaseHandler) getSecretUICli() *keybase1.SecretUiClient {
   115  	return h.secretCli
   116  }
   117  
   118  func (h *BaseHandler) getSecretUI(sessionID int, g *libkb.GlobalContext) libkb.SecretUI {
   119  	return &SecretUI{
   120  		sessionID:    sessionID,
   121  		cli:          h.getSecretUICli(),
   122  		Contextified: libkb.NewContextified(g),
   123  	}
   124  }
   125  
   126  func (h *BaseHandler) getLogUICli() *keybase1.LogUiClient {
   127  	return h.logCli
   128  }
   129  
   130  func (h *BaseHandler) getLogUI(sessionID int) libkb.LogUI {
   131  	return &LogUI{sessionID, h.getLogUICli()}
   132  }
   133  
   134  func (h *BaseHandler) getProvisionUI(sessionID int) libkb.ProvisionUI {
   135  	return NewRemoteProvisionUI(sessionID, h.rpcClient())
   136  }
   137  
   138  func (h *BaseHandler) getPgpUI(sessionID int) libkb.PgpUI {
   139  	return NewRemotePgpUI(sessionID, h.rpcClient())
   140  }
   141  
   142  func (h *BaseHandler) getStreamUICli() *keybase1.StreamUiClient {
   143  	return &keybase1.StreamUiClient{Cli: h.rpcClient()}
   144  }
   145  
   146  func (h *BaseHandler) getSaltpackUI(sessionID int) libkb.SaltpackUI {
   147  	return NewRemoteSaltpackUI(sessionID, h.rpcClient())
   148  }
   149  
   150  func (h *BaseHandler) getChatUI(sessionID int) libkb.ChatUI {
   151  	return NewRemoteChatUI(sessionID, h.rpcClient())
   152  }
   153  
   154  func (h *BaseHandler) getTeamsUI(sessionID int) keybase1.TeamsUiInterface {
   155  	return NewRemoteTeamsUI(sessionID, h.rpcClient())
   156  }
   157  
   158  func (h *BaseHandler) NewRemoteIdentifyUI(sessionID int, g *libkb.GlobalContext) *RemoteIdentifyUI {
   159  	c := h.rpcClient()
   160  	return &RemoteIdentifyUI{
   161  		sessionID:    sessionID,
   162  		uicli:        keybase1.IdentifyUiClient{Cli: c},
   163  		logUI:        h.getLogUI(sessionID),
   164  		Contextified: libkb.NewContextified(g),
   165  	}
   166  }
   167  
   168  func (h *BaseHandler) NewRemoteSkipPromptIdentifyUI(sessionID int, g *libkb.GlobalContext) *RemoteIdentifyUI {
   169  	c := h.NewRemoteIdentifyUI(sessionID, g)
   170  	c.skipPrompt = true
   171  	return c
   172  }
   173  
   174  type RekeyUI struct {
   175  	libkb.Contextified
   176  	sessionID    int
   177  	cli          *keybase1.RekeyUIClient
   178  	connectionID libkb.ConnectionID
   179  }
   180  
   181  // DelegateRekeyUI shouldn't be called on this object since it
   182  // should already have a sessionID.
   183  func (r *RekeyUI) DelegateRekeyUI(ctx context.Context) (int, error) {
   184  	r.G().Log.Warning("service RekeyUI.DelegateRekeyUI() called to get session id after RekeyUI object created")
   185  	return r.cli.DelegateRekeyUI(ctx)
   186  }
   187  
   188  func (r *RekeyUI) Refresh(ctx context.Context, arg keybase1.RefreshArg) error {
   189  	arg.SessionID = r.sessionID
   190  	return r.cli.Refresh(ctx, arg)
   191  }
   192  
   193  func (r *RekeyUI) RekeySendEvent(ctx context.Context, arg keybase1.RekeySendEventArg) error {
   194  	arg.SessionID = r.sessionID
   195  	return r.cli.RekeySendEvent(ctx, arg)
   196  }