2018-11-21 20:56:12 +03:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-11-08 21:13:55 +03:00
|
|
|
"time"
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-12-10 16:12:45 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
|
|
|
)
|
|
|
|
|
2020-07-01 21:00:53 +03:00
|
|
|
// 1: original format
|
2020-11-08 21:13:55 +03:00
|
|
|
// 2: Identity Ids are generated from the first version serialized data instead of from the first git commit
|
2020-11-09 01:56:32 +03:00
|
|
|
// + Identity hold multiple lamport clocks from other entities, instead of just bug edit
|
2020-11-08 21:13:55 +03:00
|
|
|
const formatVersion = 2
|
|
|
|
|
|
|
|
// version is a complete set of information about an Identity at a point in time.
|
|
|
|
type version struct {
|
2019-02-07 00:06:42 +03:00
|
|
|
name string
|
2020-02-25 23:35:57 +03:00
|
|
|
email string // as defined in git or from a bridge when importing the identity
|
|
|
|
login string // from a bridge when importing the identity
|
2019-02-07 00:06:42 +03:00
|
|
|
avatarURL string
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// The lamport times of the other entities at which this version become effective
|
|
|
|
times map[string]lamport.Time
|
|
|
|
unixTime int64
|
|
|
|
|
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.
|
2020-01-24 02:30:13 +03:00
|
|
|
keys []*Key
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// mandatory random bytes to ensure a better randomness of the data of the first
|
2021-03-14 20:39:04 +03:00
|
|
|
// version of an identity, used to later generate the ID
|
2020-11-08 21:13:55 +03:00
|
|
|
// len(Nonce) should be > 20 and < 64 bytes
|
2018-11-21 20:56:12 +03:00
|
|
|
// It has no functional purpose and should be ignored.
|
2020-11-09 01:56:32 +03:00
|
|
|
// TODO: optional after first version?
|
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
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// Not serialized. Store the version's id in memory.
|
|
|
|
id entity.Id
|
2019-02-19 02:19:27 +03:00
|
|
|
// Not serialized
|
2020-07-01 20:39:02 +03:00
|
|
|
commitHash repository.Hash
|
2019-01-20 17:41:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func newVersion(repo repository.RepoClock, name string, email string, login string, avatarURL string, keys []*Key) (*version, error) {
|
|
|
|
clocks, err := repo.AllClocks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
times := make(map[string]lamport.Time)
|
|
|
|
for name, clock := range clocks {
|
|
|
|
times[name] = clock.Time()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &version{
|
|
|
|
id: entity.UnsetId,
|
|
|
|
name: name,
|
|
|
|
email: email,
|
|
|
|
login: login,
|
|
|
|
avatarURL: avatarURL,
|
|
|
|
times: times,
|
|
|
|
unixTime: time.Now().Unix(),
|
|
|
|
keys: keys,
|
|
|
|
nonce: makeNonce(20),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type versionJSON struct {
|
2019-01-20 17:41:27 +03:00
|
|
|
// Additional field to version the data
|
|
|
|
FormatVersion uint `json:"version"`
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
Times map[string]lamport.Time `json:"times"`
|
|
|
|
UnixTime int64 `json:"unix_time"`
|
|
|
|
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"`
|
|
|
|
Nonce []byte `json:"nonce"`
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id return the identifier of the version
|
|
|
|
func (v *version) Id() entity.Id {
|
|
|
|
if v.id == "" {
|
|
|
|
// something went really wrong
|
|
|
|
panic("version's id not set")
|
|
|
|
}
|
|
|
|
if v.id == entity.UnsetId {
|
|
|
|
// This means we are trying to get the version's Id *before* it has been stored.
|
|
|
|
// As the Id is computed based on the actual bytes written on the disk, we are going to predict
|
|
|
|
// those and then get the Id. This is safe as it will be the exact same code writing on disk later.
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-09 02:34:48 +03:00
|
|
|
v.id = entity.DeriveId(data)
|
2020-11-08 21:13:55 +03:00
|
|
|
}
|
|
|
|
return v.id
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:49:33 +03:00
|
|
|
// Make a deep copy
|
2020-11-08 21:13:55 +03:00
|
|
|
func (v *version) Clone() *version {
|
|
|
|
// copy direct fields
|
|
|
|
clone := *v
|
|
|
|
|
2020-11-09 01:56:32 +03:00
|
|
|
// reset some fields
|
|
|
|
clone.commitHash = ""
|
|
|
|
clone.id = entity.UnsetId
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
clone.times = make(map[string]lamport.Time)
|
|
|
|
for name, t := range v.times {
|
|
|
|
clone.times[name] = t
|
2020-01-21 20:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
clone.keys = make([]*Key, len(v.keys))
|
2020-01-24 02:30:13 +03:00
|
|
|
for i, key := range v.keys {
|
|
|
|
clone.keys[i] = key.Clone()
|
2020-01-21 20:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
clone.nonce = make([]byte, len(v.nonce))
|
|
|
|
copy(clone.nonce, v.nonce)
|
|
|
|
|
|
|
|
// not copying metadata
|
|
|
|
|
|
|
|
return &clone
|
2020-01-15 00:01:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func (v *version) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(versionJSON{
|
2019-01-20 17:41:27 +03:00
|
|
|
FormatVersion: formatVersion,
|
2020-11-08 21:13:55 +03:00
|
|
|
Times: v.times,
|
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,
|
2020-02-25 23:35:57 +03:00
|
|
|
Login: v.login,
|
2019-02-07 00:06:42 +03:00
|
|
|
AvatarUrl: v.avatarURL,
|
|
|
|
Keys: v.keys,
|
|
|
|
Nonce: v.nonce,
|
|
|
|
Metadata: v.metadata,
|
2019-01-20 17:41:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
func (v *version) UnmarshalJSON(data []byte) error {
|
|
|
|
var aux versionJSON
|
2019-01-20 17:41:27 +03:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-14 14:24:40 +03:00
|
|
|
if aux.FormatVersion != formatVersion {
|
|
|
|
return entity.NewErrInvalidFormat(aux.FormatVersion, formatVersion)
|
2019-01-20 17:41:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 02:34:48 +03:00
|
|
|
v.id = entity.DeriveId(data)
|
2020-11-08 21:13:55 +03:00
|
|
|
v.times = aux.Times
|
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
|
2020-02-25 23:35:57 +03:00
|
|
|
v.login = aux.Login
|
2019-02-07 00:06:42 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +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")
|
|
|
|
}
|
|
|
|
|
2020-02-25 23:35:57 +03:00
|
|
|
if text.Empty(v.name) && text.Empty(v.login) {
|
|
|
|
return fmt.Errorf("either name or login should be set")
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
2021-04-17 18:40:11 +03:00
|
|
|
if !text.SafeOneLine(v.name) {
|
|
|
|
return fmt.Errorf("name has unsafe characters")
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 18:40:11 +03:00
|
|
|
if !text.SafeOneLine(v.login) {
|
|
|
|
return fmt.Errorf("login has unsafe characters")
|
2020-02-25 23:35:57 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 18:40:11 +03:00
|
|
|
if !text.SafeOneLine(v.email) {
|
|
|
|
return fmt.Errorf("email has unsafe characters")
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
2020-11-08 21:13:55 +03:00
|
|
|
if len(v.nonce) < 20 {
|
|
|
|
return fmt.Errorf("nonce is too small")
|
|
|
|
}
|
2018-11-21 20:56:12 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// Write will serialize and store the version as a git blob and return
|
2018-11-21 20:56:12 +03:00
|
|
|
// its hash
|
2020-11-08 21:13:55 +03:00
|
|
|
func (v *version) Write(repo repository.Repo) (repository.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
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// make sure we set the Id when writing in the repo
|
2020-11-09 02:34:48 +03:00
|
|
|
v.id = entity.DeriveId(data)
|
2020-11-08 21:13:55 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
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
|
2020-11-08 21:13:55 +03:00
|
|
|
// If the version has been commit to git already, it won't be overwritten.
|
|
|
|
// Beware: changing the metadata on a version will change it's ID
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +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
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
// AllMetadata return all metadata for this version
|
|
|
|
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
|
|
|
}
|