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