2018-11-21 20:56:12 +03:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2019-01-20 17:41:27 +03:00
|
|
|
"github.com/pkg/errors"
|
2018-11-21 20:56:12 +03:00
|
|
|
)
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
const formatVersion = 1
|
|
|
|
|
2019-01-17 04:05:50 +03:00
|
|
|
// Version is a complete set of information about an Identity at a point in time.
|
2018-11-21 20:56:12 +03:00
|
|
|
type Version struct {
|
|
|
|
// The lamport time at which this version become effective
|
|
|
|
// The reference time is the bug edition lamport clock
|
2019-02-19 02:19:27 +03:00
|
|
|
// It must be the first field in this struct due to https://github.com/golang/go/issues/599
|
|
|
|
time lamport.Time
|
|
|
|
unixTime int64
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
name string
|
|
|
|
email string
|
|
|
|
login string
|
|
|
|
avatarURL string
|
2018-11-21 20:56:12 +03:00
|
|
|
|
|
|
|
// The set of keys valid at that time, from this version onward, until they get removed
|
|
|
|
// in a new version. This allow to have multiple key for the same identity (e.g. one per
|
|
|
|
// device) as well as revoke key.
|
2019-02-07 00:06:42 +03:00
|
|
|
keys []Key
|
2018-11-21 20:56:12 +03:00
|
|
|
|
|
|
|
// This optional array is here to ensure a better randomness of the identity id to avoid collisions.
|
|
|
|
// It has no functional purpose and should be ignored.
|
|
|
|
// It is advised to fill this array if there is not enough entropy, e.g. if there is no keys.
|
2019-02-07 00:06:42 +03:00
|
|
|
nonce []byte
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
// A set of arbitrary key/value to store metadata about a version or about an Identity in general.
|
2019-02-07 00:06:42 +03:00
|
|
|
metadata map[string]string
|
2019-02-19 02:19:27 +03:00
|
|
|
|
|
|
|
// Not serialized
|
|
|
|
commitHash git.Hash
|
2019-01-20 17:41:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type VersionJSON struct {
|
|
|
|
// Additional field to version the data
|
|
|
|
FormatVersion uint `json:"version"`
|
|
|
|
|
|
|
|
Time lamport.Time `json:"time"`
|
2019-02-19 02:19:27 +03:00
|
|
|
UnixTime int64 `json:"unix_time"`
|
2019-02-24 20:49:12 +03:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Email string `json:"email,omitempty"`
|
|
|
|
Login string `json:"login,omitempty"`
|
|
|
|
AvatarUrl string `json:"avatar_url,omitempty"`
|
|
|
|
Keys []Key `json:"pub_keys,omitempty"`
|
2019-01-20 17:41:27 +03:00
|
|
|
Nonce []byte `json:"nonce,omitempty"`
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Version) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(VersionJSON{
|
|
|
|
FormatVersion: formatVersion,
|
2019-02-07 00:06:42 +03:00
|
|
|
Time: v.time,
|
2019-02-19 02:19:27 +03:00
|
|
|
UnixTime: v.unixTime,
|
2019-02-07 00:06:42 +03:00
|
|
|
Name: v.name,
|
|
|
|
Email: v.email,
|
|
|
|
Login: v.login,
|
|
|
|
AvatarUrl: v.avatarURL,
|
|
|
|
Keys: v.keys,
|
|
|
|
Nonce: v.nonce,
|
|
|
|
Metadata: v.metadata,
|
2019-01-20 17:41:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Version) UnmarshalJSON(data []byte) error {
|
|
|
|
var aux VersionJSON
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aux.FormatVersion != formatVersion {
|
|
|
|
return fmt.Errorf("unknown format version %v", aux.FormatVersion)
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
v.time = aux.Time
|
2019-02-19 02:19:27 +03:00
|
|
|
v.unixTime = aux.UnixTime
|
2019-02-07 00:06:42 +03:00
|
|
|
v.name = aux.Name
|
|
|
|
v.email = aux.Email
|
|
|
|
v.login = aux.Login
|
|
|
|
v.avatarURL = aux.AvatarUrl
|
|
|
|
v.keys = aux.Keys
|
|
|
|
v.nonce = aux.Nonce
|
|
|
|
v.metadata = aux.Metadata
|
2019-01-20 17:41:27 +03:00
|
|
|
|
|
|
|
return nil
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Version) Validate() error {
|
2019-02-19 03:44:21 +03:00
|
|
|
// time must be set after a commit
|
|
|
|
if v.commitHash != "" && v.unixTime == 0 {
|
2019-02-19 02:19:27 +03:00
|
|
|
return fmt.Errorf("unix time not set")
|
|
|
|
}
|
2019-02-19 03:44:21 +03:00
|
|
|
if v.commitHash != "" && v.time == 0 {
|
|
|
|
return fmt.Errorf("lamport time not set")
|
|
|
|
}
|
2019-02-19 02:19:27 +03:00
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if text.Empty(v.name) && text.Empty(v.login) {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("either name or login should be set")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if strings.Contains(v.name, "\n") {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("name should be a single line")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if !text.Safe(v.name) {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("name is not fully printable")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if strings.Contains(v.login, "\n") {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("login should be a single line")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if !text.Safe(v.login) {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("login is not fully printable")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if strings.Contains(v.email, "\n") {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("email should be a single line")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if !text.Safe(v.email) {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("email is not fully printable")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if v.avatarURL != "" && !text.ValidUrl(v.avatarURL) {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("avatarUrl is not a valid URL")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
if len(v.nonce) > 64 {
|
2018-11-21 20:56:12 +03:00
|
|
|
return fmt.Errorf("nonce is too big")
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, k := range v.keys {
|
2019-01-20 17:41:27 +03:00
|
|
|
if err := k.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "invalid key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write will serialize and store the Version as a git blob and return
|
|
|
|
// its hash
|
|
|
|
func (v *Version) Write(repo repository.Repo) (git.Hash, error) {
|
2019-01-20 17:41:27 +03:00
|
|
|
// make sure we don't write invalid data
|
|
|
|
err := v.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "validation error")
|
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
data, err := json.Marshal(v)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := repo.StoreData(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeNonce(len int) []byte {
|
|
|
|
result := make([]byte, len)
|
|
|
|
_, err := rand.Read(result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
// SetMetadata store arbitrary metadata about a version or an Identity in general
|
|
|
|
// If the Version has been commit to git already, it won't be overwritten.
|
|
|
|
func (v *Version) SetMetadata(key string, value string) {
|
2019-02-07 00:06:42 +03:00
|
|
|
if v.metadata == nil {
|
|
|
|
v.metadata = make(map[string]string)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-07 00:06:42 +03:00
|
|
|
v.metadata[key] = value
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMetadata retrieve arbitrary metadata about the Version
|
|
|
|
func (v *Version) GetMetadata(key string) (string, bool) {
|
2019-02-07 00:06:42 +03:00
|
|
|
val, ok := v.metadata[key]
|
2019-01-17 04:05:50 +03:00
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllMetadata return all metadata for this Identity
|
|
|
|
func (v *Version) AllMetadata() map[string]string {
|
2019-02-07 00:06:42 +03:00
|
|
|
return v.metadata
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|