1package identity
2
3import (
4 "crypto/rand"
5 "encoding/json"
6 "fmt"
7 "strings"
8
9 "github.com/MichaelMure/git-bug/repository"
10 "github.com/MichaelMure/git-bug/util/git"
11 "github.com/MichaelMure/git-bug/util/lamport"
12 "github.com/MichaelMure/git-bug/util/text"
13 "github.com/pkg/errors"
14)
15
16const formatVersion = 1
17
18// Version is a complete set of information about an Identity at a point in time.
19type Version struct {
20 // The lamport time at which this version become effective
21 // The reference time is the bug edition lamport clock
22 // It must be the first field in this struct due to https://github.com/golang/go/issues/599
23 time lamport.Time
24 unixTime int64
25
26 name string
27 email string // TODO: remove ? keep and use metadata for bridges ?
28 login string // TODO: remove
29 avatarURL string
30
31 // The set of keys valid at that time, from this version onward, until they get removed
32 // in a new version. This allow to have multiple key for the same identity (e.g. one per
33 // device) as well as revoke key.
34 keys []Key
35
36 // This optional array is here to ensure a better randomness of the identity id to avoid collisions.
37 // It has no functional purpose and should be ignored.
38 // It is advised to fill this array if there is not enough entropy, e.g. if there is no keys.
39 nonce []byte
40
41 // A set of arbitrary key/value to store metadata about a version or about an Identity in general.
42 metadata map[string]string
43
44 // Not serialized
45 commitHash git.Hash
46}
47
48type VersionJSON struct {
49 // Additional field to version the data
50 FormatVersion uint `json:"version"`
51
52 Time lamport.Time `json:"time"`
53 UnixTime int64 `json:"unix_time"`
54 Name string `json:"name,omitempty"`
55 Email string `json:"email,omitempty"`
56 Login string `json:"login,omitempty"`
57 AvatarUrl string `json:"avatar_url,omitempty"`
58 Keys []Key `json:"pub_keys,omitempty"`
59 Nonce []byte `json:"nonce,omitempty"`
60 Metadata map[string]string `json:"metadata,omitempty"`
61}
62
63type VersionMutator struct {
64 Name string
65 Email string
66 Login string
67 AvatarUrl string
68 Keys []Key
69}
70
71func (v *Version) MarshalJSON() ([]byte, error) {
72 return json.Marshal(VersionJSON{
73 FormatVersion: formatVersion,
74 Time: v.time,
75 UnixTime: v.unixTime,
76 Name: v.name,
77 Email: v.email,
78 Login: v.login,
79 AvatarUrl: v.avatarURL,
80 Keys: v.keys,
81 Nonce: v.nonce,
82 Metadata: v.metadata,
83 })
84}
85
86func (v *Version) UnmarshalJSON(data []byte) error {
87 var aux VersionJSON
88
89 if err := json.Unmarshal(data, &aux); err != nil {
90 return err
91 }
92
93 if aux.FormatVersion != formatVersion {
94 return fmt.Errorf("unknown format version %v", aux.FormatVersion)
95 }
96
97 v.time = aux.Time
98 v.unixTime = aux.UnixTime
99 v.name = aux.Name
100 v.email = aux.Email
101 v.login = aux.Login
102 v.avatarURL = aux.AvatarUrl
103 v.keys = aux.Keys
104 v.nonce = aux.Nonce
105 v.metadata = aux.Metadata
106
107 return nil
108}
109
110func (v *Version) Validate() error {
111 // time must be set after a commit
112 if v.commitHash != "" && v.unixTime == 0 {
113 return fmt.Errorf("unix time not set")
114 }
115 if v.commitHash != "" && v.time == 0 {
116 return fmt.Errorf("lamport time not set")
117 }
118
119 if text.Empty(v.name) && text.Empty(v.login) {
120 return fmt.Errorf("either name or login should be set")
121 }
122
123 if strings.Contains(v.name, "\n") {
124 return fmt.Errorf("name should be a single line")
125 }
126
127 if !text.Safe(v.name) {
128 return fmt.Errorf("name is not fully printable")
129 }
130
131 if strings.Contains(v.login, "\n") {
132 return fmt.Errorf("login should be a single line")
133 }
134
135 if !text.Safe(v.login) {
136 return fmt.Errorf("login is not fully printable")
137 }
138
139 if strings.Contains(v.email, "\n") {
140 return fmt.Errorf("email should be a single line")
141 }
142
143 if !text.Safe(v.email) {
144 return fmt.Errorf("email is not fully printable")
145 }
146
147 if v.avatarURL != "" && !text.ValidUrl(v.avatarURL) {
148 return fmt.Errorf("avatarUrl is not a valid URL")
149 }
150
151 if len(v.nonce) > 64 {
152 return fmt.Errorf("nonce is too big")
153 }
154
155 for _, k := range v.keys {
156 if err := k.Validate(); err != nil {
157 return errors.Wrap(err, "invalid key")
158 }
159 }
160
161 return nil
162}
163
164// Write will serialize and store the Version as a git blob and return
165// its hash
166func (v *Version) Write(repo repository.Repo) (git.Hash, error) {
167 // make sure we don't write invalid data
168 err := v.Validate()
169 if err != nil {
170 return "", errors.Wrap(err, "validation error")
171 }
172
173 data, err := json.Marshal(v)
174
175 if err != nil {
176 return "", err
177 }
178
179 hash, err := repo.StoreData(data)
180
181 if err != nil {
182 return "", err
183 }
184
185 return hash, nil
186}
187
188func makeNonce(len int) []byte {
189 result := make([]byte, len)
190 _, err := rand.Read(result)
191 if err != nil {
192 panic(err)
193 }
194 return result
195}
196
197// SetMetadata store arbitrary metadata about a version or an Identity in general
198// If the Version has been commit to git already, it won't be overwritten.
199func (v *Version) SetMetadata(key string, value string) {
200 if v.metadata == nil {
201 v.metadata = make(map[string]string)
202 }
203
204 v.metadata[key] = value
205}
206
207// GetMetadata retrieve arbitrary metadata about the Version
208func (v *Version) GetMetadata(key string) (string, bool) {
209 val, ok := v.metadata[key]
210 return val, ok
211}
212
213// AllMetadata return all metadata for this Identity
214func (v *Version) AllMetadata() map[string]string {
215 return v.metadata
216}