version.go

  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	if v.unixTime == 0 {
104		return fmt.Errorf("unix time not set")
105	}
106
107	if text.Empty(v.name) && text.Empty(v.login) {
108		return fmt.Errorf("either name or login should be set")
109	}
110
111	if strings.Contains(v.name, "\n") {
112		return fmt.Errorf("name should be a single line")
113	}
114
115	if !text.Safe(v.name) {
116		return fmt.Errorf("name is not fully printable")
117	}
118
119	if strings.Contains(v.login, "\n") {
120		return fmt.Errorf("login should be a single line")
121	}
122
123	if !text.Safe(v.login) {
124		return fmt.Errorf("login is not fully printable")
125	}
126
127	if strings.Contains(v.email, "\n") {
128		return fmt.Errorf("email should be a single line")
129	}
130
131	if !text.Safe(v.email) {
132		return fmt.Errorf("email is not fully printable")
133	}
134
135	if v.avatarURL != "" && !text.ValidUrl(v.avatarURL) {
136		return fmt.Errorf("avatarUrl is not a valid URL")
137	}
138
139	if len(v.nonce) > 64 {
140		return fmt.Errorf("nonce is too big")
141	}
142
143	for _, k := range v.keys {
144		if err := k.Validate(); err != nil {
145			return errors.Wrap(err, "invalid key")
146		}
147	}
148
149	return nil
150}
151
152// Write will serialize and store the Version as a git blob and return
153// its hash
154func (v *Version) Write(repo repository.Repo) (git.Hash, error) {
155	// make sure we don't write invalid data
156	err := v.Validate()
157	if err != nil {
158		return "", errors.Wrap(err, "validation error")
159	}
160
161	data, err := json.Marshal(v)
162
163	if err != nil {
164		return "", err
165	}
166
167	hash, err := repo.StoreData(data)
168
169	if err != nil {
170		return "", err
171	}
172
173	return hash, nil
174}
175
176func makeNonce(len int) []byte {
177	result := make([]byte, len)
178	_, err := rand.Read(result)
179	if err != nil {
180		panic(err)
181	}
182	return result
183}
184
185// SetMetadata store arbitrary metadata about a version or an Identity in general
186// If the Version has been commit to git already, it won't be overwritten.
187func (v *Version) SetMetadata(key string, value string) {
188	if v.metadata == nil {
189		v.metadata = make(map[string]string)
190	}
191
192	v.metadata[key] = value
193}
194
195// GetMetadata retrieve arbitrary metadata about the Version
196func (v *Version) GetMetadata(key string) (string, bool) {
197	val, ok := v.metadata[key]
198	return val, ok
199}
200
201// AllMetadata return all metadata for this Identity
202func (v *Version) AllMetadata() map[string]string {
203	return v.metadata
204}