github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/userconfig.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 libkb
     5  
     6  import (
     7  	"encoding/hex"
     8  
     9  	"github.com/keybase/client/go/kbun"
    10  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    11  	jsonw "github.com/keybase/go-jsonw"
    12  )
    13  
    14  // ==================================================================
    15  
    16  // TODO (CORE-6576): Remove these aliases once everything outside of
    17  // this repo points to kbun.
    18  type NormalizedUsername = kbun.NormalizedUsername
    19  
    20  func NewNormalizedUsername(s string) NormalizedUsername {
    21  	return kbun.NewNormalizedUsername(s)
    22  }
    23  
    24  // ==================================================================
    25  
    26  type UserConfig struct {
    27  	ID              string                    `json:"id"`
    28  	Name            NormalizedUsername        `json:"name"`
    29  	Salt            string                    `json:"salt"`
    30  	Device          *string                   `json:"device"`
    31  	PassphraseState *keybase1.PassphraseState `json:"passphrase_state,omitempty"`
    32  
    33  	importedID       keybase1.UID
    34  	importedSalt     []byte
    35  	importedDeviceID keybase1.DeviceID
    36  
    37  	isOneshot bool
    38  }
    39  
    40  // ==================================================================
    41  
    42  func (u UserConfig) GetUID() keybase1.UID                          { return u.importedID }
    43  func (u UserConfig) GetUsername() NormalizedUsername               { return u.Name }
    44  func (u UserConfig) GetDeviceID() keybase1.DeviceID                { return u.importedDeviceID }
    45  func (u UserConfig) GetPassphraseState() *keybase1.PassphraseState { return u.PassphraseState }
    46  func (u UserConfig) IsOneshot() bool                               { return u.isOneshot }
    47  
    48  // ==================================================================
    49  
    50  func NewUserConfig(id keybase1.UID, name NormalizedUsername, salt []byte, dev keybase1.DeviceID) *UserConfig {
    51  	ret := &UserConfig{
    52  		ID:               id.String(),
    53  		Name:             name,
    54  		Salt:             hex.EncodeToString(salt),
    55  		Device:           nil,
    56  		importedID:       id,
    57  		importedSalt:     salt,
    58  		importedDeviceID: dev,
    59  	}
    60  	if dev.Exists() {
    61  		tmp := dev.String()
    62  		ret.Device = &tmp
    63  	}
    64  	return ret
    65  }
    66  
    67  func NewOneshotUserConfig(id keybase1.UID, name NormalizedUsername, salt []byte, dev keybase1.DeviceID) *UserConfig {
    68  	ret := NewUserConfig(id, name, salt, dev)
    69  	ret.isOneshot = true
    70  	return ret
    71  }
    72  
    73  // ==================================================================
    74  
    75  func (u *UserConfig) Import() (err error) {
    76  	var tmp keybase1.UID
    77  	if tmp, err = UIDFromHex(u.ID); err != nil {
    78  		return
    79  	}
    80  	u.importedID = tmp
    81  	if u.importedSalt, err = hex.DecodeString(u.Salt); err != nil {
    82  		return
    83  	}
    84  	if u.Device != nil {
    85  		if u.importedDeviceID, err = keybase1.DeviceIDFromString(*u.Device); err != nil {
    86  			return
    87  		}
    88  	}
    89  	return
    90  }
    91  
    92  // ==================================================================
    93  
    94  func ImportUserConfigFromJSONWrapper(jw *jsonw.Wrapper) (ret *UserConfig, err error) {
    95  	var tmp UserConfig
    96  	if jw == nil {
    97  		return
    98  	}
    99  	if err = jw.UnmarshalAgain(&tmp); err != nil {
   100  		return
   101  	}
   102  	if err = tmp.Import(); err != nil {
   103  		return
   104  	}
   105  	ret = &tmp
   106  	return
   107  }
   108  
   109  // ==================================================================
   110  
   111  func (u *UserConfig) SetDevice(d keybase1.DeviceID) {
   112  	u.importedDeviceID = d
   113  	var s *string
   114  	if d.Exists() {
   115  		tmp := d.String()
   116  		s = &tmp
   117  	}
   118  	u.Device = s
   119  }
   120  
   121  // ==================================================================
   122  
   123  func (u *UserConfig) SetPassphraseState(passphraseState keybase1.PassphraseState) {
   124  	u.PassphraseState = &passphraseState
   125  }
   126  
   127  // ==================================================================