forked from keybase/client
/
userconfig.go
125 lines (102 loc) · 3.37 KB
/
userconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"encoding/hex"
"strings"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
//==================================================================
// NormalizedUsername is a username that has been normalized (toLowered)
// and therefore will compare correctly against other normalized usernames.
type NormalizedUsername string
// NewNormalizedUsername makes a normalized username out of a non-normalized
// plain string username
func NewNormalizedUsername(s string) NormalizedUsername {
return NormalizedUsername(strings.ToLower(s))
}
// Eq returns true if the given normalized usernames are equal
func (n NormalizedUsername) Eq(n2 NormalizedUsername) bool {
return string(n) == string(n2)
}
// String returns the normalized username as a string (in lower case)
func (n NormalizedUsername) String() string { return string(n) }
// IsNil returns true if the username is the empty string
func (n NormalizedUsername) IsNil() bool { return len(string(n)) == 0 }
//==================================================================
type UserConfig struct {
ID string `json:"id"`
Name NormalizedUsername `json:"name"`
Salt string `json:"salt"`
Device *string `json:"device"`
importedID keybase1.UID
importedSalt []byte
importedDeviceID keybase1.DeviceID
}
//==================================================================
func (u UserConfig) GetUID() keybase1.UID { return u.importedID }
func (u UserConfig) GetUsername() NormalizedUsername { return u.Name }
func (u UserConfig) GetSalt() []byte { return u.importedSalt }
func (u UserConfig) GetDeviceID() keybase1.DeviceID { return u.importedDeviceID }
//==================================================================
func NewUserConfig(id keybase1.UID, name NormalizedUsername, salt []byte, dev keybase1.DeviceID) *UserConfig {
ret := &UserConfig{
ID: id.String(),
Name: name,
Salt: hex.EncodeToString(salt),
Device: nil,
importedID: id,
importedSalt: salt,
importedDeviceID: dev,
}
if dev.Exists() {
tmp := dev.String()
ret.Device = &tmp
}
return ret
}
//==================================================================
func (u *UserConfig) Import() (err error) {
var tmp keybase1.UID
if tmp, err = UIDFromHex(u.ID); err != nil {
return
}
u.importedID = tmp
if u.importedSalt, err = hex.DecodeString(u.Salt); err != nil {
return
}
if u.Device != nil {
if u.importedDeviceID, err = keybase1.DeviceIDFromString(*u.Device); err != nil {
return
}
}
return
}
//==================================================================
func ImportUserConfigFromJSONWrapper(jw *jsonw.Wrapper) (ret *UserConfig, err error) {
var tmp UserConfig
if jw == nil {
return
}
if err = jw.UnmarshalAgain(&tmp); err != nil {
return
}
if err = tmp.Import(); err != nil {
return
}
ret = &tmp
return
}
//==================================================================
func (u *UserConfig) SetDevice(d keybase1.DeviceID) {
u.importedDeviceID = d
var s *string
if d.Exists() {
tmp := d.String()
s = &tmp
}
u.Device = s
return
}
//==================================================================