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
28 login string
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"`
55 Email string `json:"email"`
56 Login string `json:"login"`
57 AvatarUrl string `json:"avatar_url"`
58 Keys []Key `json:"pub_keys"`
59 Nonce []byte `json:"nonce,omitempty"`
60 Metadata map[string]string `json:"metadata,omitempty"`
61}
62
63func (v *Version) MarshalJSON() ([]byte, error) {
64 return json.Marshal(VersionJSON{
65 FormatVersion: formatVersion,
66 Time: v.time,
67 UnixTime: v.unixTime,
68 Name: v.name,
69 Email: v.email,
70 Login: v.login,
71 AvatarUrl: v.avatarURL,
72 Keys: v.keys,
73 Nonce: v.nonce,
74 Metadata: v.metadata,
75 })
76}
77
78func (v *Version) UnmarshalJSON(data []byte) error {
79 var aux VersionJSON
80
81 if err := json.Unmarshal(data, &aux); err != nil {
82 return err
83 }
84
85 if aux.FormatVersion != formatVersion {
86 return fmt.Errorf("unknown format version %v", aux.FormatVersion)
87 }
88
89 v.time = aux.Time
90 v.unixTime = aux.UnixTime
91 v.name = aux.Name
92 v.email = aux.Email
93 v.login = aux.Login
94 v.avatarURL = aux.AvatarUrl
95 v.keys = aux.Keys
96 v.nonce = aux.Nonce
97 v.metadata = aux.Metadata
98
99 return nil
100}
101
102func (v *Version) Validate() error {
103 // time must be set after a commit
104 if v.commitHash != "" && v.unixTime == 0 {
105 return fmt.Errorf("unix time not set")
106 }
107 if v.commitHash != "" && v.time == 0 {
108 return fmt.Errorf("lamport time not set")
109 }
110
111 if text.Empty(v.name) && text.Empty(v.login) {
112 return fmt.Errorf("either name or login should be set")
113 }
114
115 if strings.Contains(v.name, "\n") {
116 return fmt.Errorf("name should be a single line")
117 }
118
119 if !text.Safe(v.name) {
120 return fmt.Errorf("name is not fully printable")
121 }
122
123 if strings.Contains(v.login, "\n") {
124 return fmt.Errorf("login should be a single line")
125 }
126
127 if !text.Safe(v.login) {
128 return fmt.Errorf("login is not fully printable")
129 }
130
131 if strings.Contains(v.email, "\n") {
132 return fmt.Errorf("email should be a single line")
133 }
134
135 if !text.Safe(v.email) {
136 return fmt.Errorf("email is not fully printable")
137 }
138
139 if v.avatarURL != "" && !text.ValidUrl(v.avatarURL) {
140 return fmt.Errorf("avatarUrl is not a valid URL")
141 }
142
143 if len(v.nonce) > 64 {
144 return fmt.Errorf("nonce is too big")
145 }
146
147 for _, k := range v.keys {
148 if err := k.Validate(); err != nil {
149 return errors.Wrap(err, "invalid key")
150 }
151 }
152
153 return nil
154}
155
156// Write will serialize and store the Version as a git blob and return
157// its hash
158func (v *Version) Write(repo repository.Repo) (git.Hash, error) {
159 // make sure we don't write invalid data
160 err := v.Validate()
161 if err != nil {
162 return "", errors.Wrap(err, "validation error")
163 }
164
165 data, err := json.Marshal(v)
166
167 if err != nil {
168 return "", err
169 }
170
171 hash, err := repo.StoreData(data)
172
173 if err != nil {
174 return "", err
175 }
176
177 return hash, nil
178}
179
180func makeNonce(len int) []byte {
181 result := make([]byte, len)
182 _, err := rand.Read(result)
183 if err != nil {
184 panic(err)
185 }
186 return result
187}
188
189// SetMetadata store arbitrary metadata about a version or an Identity in general
190// If the Version has been commit to git already, it won't be overwritten.
191func (v *Version) SetMetadata(key string, value string) {
192 if v.metadata == nil {
193 v.metadata = make(map[string]string)
194 }
195
196 v.metadata[key] = value
197}
198
199// GetMetadata retrieve arbitrary metadata about the Version
200func (v *Version) GetMetadata(key string) (string, bool) {
201 val, ok := v.metadata[key]
202 return val, ok
203}
204
205// AllMetadata return all metadata for this Identity
206func (v *Version) AllMetadata() map[string]string {
207 return v.metadata
208}