github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/unlock.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 engine
     5  
     6  import (
     7  	"github.com/keybase/client/go/libkb"
     8  )
     9  
    10  // Unlock is an engine.
    11  type Unlock struct {
    12  	libkb.Contextified
    13  	passphrase string
    14  }
    15  
    16  // NewUnlock creates a Unlock engine.
    17  func NewUnlock(g *libkb.GlobalContext) *Unlock {
    18  	return &Unlock{
    19  		Contextified: libkb.NewContextified(g),
    20  	}
    21  }
    22  
    23  // NewUnlock creates a Unlock engine.
    24  func NewUnlockWithPassphrase(g *libkb.GlobalContext, passphrase string) *Unlock {
    25  	return &Unlock{
    26  		Contextified: libkb.NewContextified(g),
    27  		passphrase:   passphrase,
    28  	}
    29  }
    30  
    31  // Name is the unique engine name.
    32  func (e *Unlock) Name() string {
    33  	return "Unlock"
    34  }
    35  
    36  // GetPrereqs returns the engine prereqs.
    37  func (e *Unlock) Prereqs() Prereqs {
    38  	return Prereqs{}
    39  }
    40  
    41  // RequiredUIs returns the required UIs.
    42  func (e *Unlock) RequiredUIs() []libkb.UIKind {
    43  	if e.passphrase != "" {
    44  		return nil
    45  	}
    46  	return []libkb.UIKind{libkb.SecretUIKind}
    47  }
    48  
    49  // SubConsumers returns the other UI consumers for this engine.
    50  func (e *Unlock) SubConsumers() []libkb.UIConsumer {
    51  	return nil
    52  }
    53  
    54  // Run starts the engine.
    55  func (e *Unlock) Run(m libkb.MetaContext) (err error) {
    56  	defer m.Trace("Unlock#Run", &err)()
    57  
    58  	un := m.CurrentUsername()
    59  	m.Debug("Active device: %+v", *m.ActiveDevice())
    60  	if un.IsNil() {
    61  		return libkb.NewNoUsernameError()
    62  	}
    63  	m = m.WithNewProvisionalLoginContext()
    64  	if e.passphrase == "" {
    65  		err = libkb.PassphraseLoginPromptThenSecretStore(m, un.String(), 5, false /* failOnStoreError */)
    66  	} else {
    67  		err = libkb.PassphraseLoginNoPromptThenSecretStore(m, un.String(), e.passphrase, false /* failOnStoreError */)
    68  	}
    69  	if err != nil {
    70  		return err
    71  	}
    72  	m.CommitProvisionalLogin()
    73  
    74  	return nil
    75  }