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

     1  package libkb
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  
     7  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     8  )
     9  
    10  type ProvisionalLoginContext struct {
    11  	MetaContextified
    12  	username     NormalizedUsername
    13  	uv           keybase1.UserVersion
    14  	salt         []byte
    15  	streamCache  *PassphraseStreamCache
    16  	localSession *Session
    17  	loginSession *LoginSession
    18  	skbKeyring   *SKBKeyringFile
    19  	secretSyncer *SecretSyncer
    20  }
    21  
    22  var _ LoginContext = (*ProvisionalLoginContext)(nil)
    23  
    24  func newProvisionalLoginContext(m MetaContext) *ProvisionalLoginContext {
    25  	return &ProvisionalLoginContext{
    26  		MetaContextified: NewMetaContextified(m),
    27  		localSession:     newSession(m.G()),
    28  		secretSyncer:     NewSecretSyncer(m.G()),
    29  	}
    30  }
    31  
    32  func newProvisionalLoginContextWithUserVersionAndUsername(m MetaContext, uv keybase1.UserVersion, un NormalizedUsername) *ProvisionalLoginContext {
    33  	ret := newProvisionalLoginContext(m)
    34  	ret.uv = uv
    35  	ret.username = un
    36  	return ret
    37  }
    38  
    39  func (p *ProvisionalLoginContext) Dump(m MetaContext, prefix string) {
    40  	m.Debug("%sUsername: %s", prefix, p.username)
    41  	m.Debug("%sUserVersion: %v", prefix, p.uv)
    42  	if p.salt != nil {
    43  		m.Debug("%sSalt: %s", prefix, hex.EncodeToString(p.salt))
    44  	}
    45  	m.Debug("%sPassphraseCache: %v", prefix, (p.streamCache != nil))
    46  	m.Debug("%sLocalSession: %v", prefix, (p.localSession != nil))
    47  	m.Debug("%sLoginSession: %v", prefix, (p.loginSession != nil))
    48  }
    49  
    50  func (p *ProvisionalLoginContext) LoggedInLoad() (bool, error) {
    51  	if p.localSession != nil {
    52  		return p.localSession.IsLoggedIn(), nil
    53  	}
    54  	return false, nil
    55  }
    56  func (p *ProvisionalLoginContext) CreateStreamCache(tsec Triplesec, pps *PassphraseStream) {
    57  	p.streamCache = NewPassphraseStreamCache(tsec, pps)
    58  }
    59  func (p *ProvisionalLoginContext) SetStreamCache(c *PassphraseStreamCache) {
    60  	p.streamCache = c
    61  }
    62  func (p *ProvisionalLoginContext) PassphraseStreamCache() *PassphraseStreamCache {
    63  	return p.streamCache
    64  }
    65  func (p *ProvisionalLoginContext) PassphraseStream() *PassphraseStream {
    66  	if p.PassphraseStreamCache() == nil {
    67  		return nil
    68  	}
    69  	return p.PassphraseStreamCache().PassphraseStream()
    70  }
    71  func (p *ProvisionalLoginContext) CreateLoginSessionWithSalt(emailOrUsername string, salt []byte) error {
    72  	if salt != nil {
    73  		p.salt = append([]byte{}, salt...)
    74  	}
    75  	return nil
    76  }
    77  func (p *ProvisionalLoginContext) LoginSession() *LoginSession {
    78  	return p.loginSession
    79  }
    80  func (p *ProvisionalLoginContext) SetLoginSession(l *LoginSession) {
    81  	p.loginSession = l
    82  }
    83  func (p *ProvisionalLoginContext) LocalSession() *Session {
    84  	return p.localSession.Clone()
    85  }
    86  func (p *ProvisionalLoginContext) GetUID() keybase1.UID {
    87  	return p.uv.Uid
    88  }
    89  func (p *ProvisionalLoginContext) GetUserVersion() keybase1.UserVersion {
    90  	return p.uv
    91  }
    92  func (p *ProvisionalLoginContext) GetUsername() NormalizedUsername {
    93  	return p.username
    94  }
    95  
    96  func (p *ProvisionalLoginContext) SetUsernameUserVersion(username NormalizedUsername, uv keybase1.UserVersion) error {
    97  	if err := p.assertNotReused(username, uv); err != nil {
    98  		return err
    99  	}
   100  	p.username = username
   101  	p.uv = uv
   102  	return nil
   103  }
   104  
   105  func (p *ProvisionalLoginContext) assertNotReused(un NormalizedUsername, uv keybase1.UserVersion) error {
   106  	if !(p.uv.IsNil() || p.uv.Eq(uv)) || !(p.username.IsNil() || p.username.Eq(un)) {
   107  		return errors.New("can't reuse a ProvisionalLoginContext!")
   108  	}
   109  	return nil
   110  }
   111  
   112  func (p *ProvisionalLoginContext) SaveState(sessionID, csrf string, username NormalizedUsername, uv keybase1.UserVersion, deviceID keybase1.DeviceID) (err error) {
   113  	defer p.M().Trace("ProvisionalLoginContext#SaveState", &err)()
   114  	if err := p.assertNotReused(username, uv); err != nil {
   115  		return err
   116  	}
   117  	p.uv = uv
   118  	p.username = username
   119  	return p.localSession.SetLoggedIn(sessionID, csrf, username, uv.Uid, deviceID)
   120  }
   121  
   122  func (p *ProvisionalLoginContext) Keyring(m MetaContext) (ret *SKBKeyringFile, err error) {
   123  	defer m.Trace("ProvisionalLoginContext#Keyring", &err)()
   124  	if p.skbKeyring != nil {
   125  		return p.skbKeyring, nil
   126  	}
   127  	if p.username.IsNil() {
   128  		p.M().Info("ProvisionalLoginContext#Keyring: no username set")
   129  		return nil, NewNoUsernameError()
   130  	}
   131  	p.M().Debug("Account: loading keyring for %s", p.username)
   132  	ret, err = LoadSKBKeyring(p.M(), p.username)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	p.skbKeyring = ret
   137  	return ret, nil
   138  }
   139  func (p *ProvisionalLoginContext) ClearKeyring() {
   140  	p.skbKeyring = nil
   141  }
   142  func (p *ProvisionalLoginContext) SecretSyncer() *SecretSyncer {
   143  	return p.secretSyncer
   144  }
   145  func (p *ProvisionalLoginContext) RunSecretSyncer(m MetaContext, uid keybase1.UID) error {
   146  	if uid.IsNil() {
   147  		uid = p.GetUID()
   148  	}
   149  	m = m.WithLoginContext(p)
   150  	return RunSyncer(m, p.secretSyncer, uid, (p.localSession != nil), false /* forceReload */)
   151  }
   152  func (p *ProvisionalLoginContext) GetUnlockedPaperEncKey() GenericKey {
   153  	return nil
   154  }
   155  func (p *ProvisionalLoginContext) GetUnlockedPaperSigKey() GenericKey {
   156  	return nil
   157  }
   158  func (p *ProvisionalLoginContext) Salt() []byte {
   159  	if len(p.salt) > 0 {
   160  		return p.salt
   161  	}
   162  	if p.loginSession == nil {
   163  		return nil
   164  	}
   165  	return p.loginSession.salt
   166  }