github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/account_delete.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  // This engine deletes the user's account.
     5  
     6  package engine
     7  
     8  import (
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  )
    12  
    13  // AccountDelete is an engine.
    14  type AccountDelete struct {
    15  	libkb.Contextified
    16  	passphrase *string
    17  }
    18  
    19  // NewAccountDelete creates a AccountDelete engine.
    20  func NewAccountDelete(g *libkb.GlobalContext, passphrase *string) *AccountDelete {
    21  	return &AccountDelete{
    22  		Contextified: libkb.NewContextified(g),
    23  		passphrase:   passphrase,
    24  	}
    25  }
    26  
    27  // Name is the unique engine name.
    28  func (e *AccountDelete) Name() string {
    29  	return "AccountDelete"
    30  }
    31  
    32  // Prereqs returns the engine prereqs.
    33  func (e *AccountDelete) Prereqs() Prereqs {
    34  	return Prereqs{
    35  		Device: true,
    36  	}
    37  }
    38  
    39  // RequiredUIs returns the required UIs.
    40  func (e *AccountDelete) RequiredUIs() []libkb.UIKind {
    41  	return []libkb.UIKind{}
    42  }
    43  
    44  // SubConsumers returns the other UI consumers for this engine.
    45  func (e *AccountDelete) SubConsumers() []libkb.UIConsumer {
    46  	return nil
    47  }
    48  
    49  // Run starts the engine.
    50  func (e *AccountDelete) Run(m libkb.MetaContext) error {
    51  	username := m.G().GetEnv().GetUsername()
    52  
    53  	passphraseState, err := libkb.LoadPassphraseState(m)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	var passphrase *string
    59  	if e.passphrase == nil && passphraseState == keybase1.PassphraseState_KNOWN {
    60  		// Passphrase is required to create PDPKA, but that's not required for
    61  		// randomPW users.
    62  		arg := libkb.DefaultPassphrasePromptArg(m, username.String())
    63  		res, err := m.UIs().SecretUI.GetPassphrase(arg, nil)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		passphrase = &res.Passphrase
    68  	} else if passphraseState == keybase1.PassphraseState_KNOWN {
    69  		passphrase = e.passphrase
    70  	}
    71  
    72  	err = libkb.DeleteAccount(m, username, passphrase)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	m.Debug("account deleted, logging out")
    77  	return m.LogoutWithOptions(libkb.LogoutOptions{KeepSecrets: false, Force: true})
    78  }