119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
|
|
|
|
type Identity struct {
|
|
Nickname string `json:"nickname,omitempty"`
|
|
RootKp meowlib.KeyPair `json:"id_kp,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
Peers PeerList `json:"peers,omitempty"`
|
|
KnownServers InternalServerList `json:"known_servers,omitempty"`
|
|
MessageServers InternalServerList `json:"message_servers,omitempty"`
|
|
ArchiveServers InternalServerList `json:"archive_servers,omitempty"`
|
|
OwnedServers InternalServerList `json:"owned_servers,omitempty"`
|
|
DefaultDbPassword string `json:"default_db_password,omitempty"`
|
|
DbPasswordStore bool `json:"db_password_store,omitempty"`
|
|
OwnedDevices PeerList `json:"owned_devices,omitempty"`
|
|
}
|
|
|
|
func CreateIdentity(nickname string) *Identity {
|
|
var id Identity
|
|
id.Nickname = nickname
|
|
id.RootKp = meowlib.NewKeyPair()
|
|
return &id
|
|
}
|
|
|
|
func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerIdxs []int) *meowlib.ContactCard {
|
|
var peer Peer
|
|
var myContactCard meowlib.ContactCard
|
|
peer.MyIdentity = meowlib.NewKeyPair()
|
|
peer.MyEncryptionKp = meowlib.NewKeyPair()
|
|
peer.MyLookupKp = meowlib.NewKeyPair()
|
|
peer.Name = ContactName
|
|
peer.InvitationId = uuid.New().String()
|
|
for _, i := range MessageServerIdxs {
|
|
srv := id.MessageServers.Servers[i].ServerData
|
|
myContactCard.PullServers = append(myContactCard.PullServers, &srv)
|
|
}
|
|
myContactCard.Name = MyName
|
|
myContactCard.ContactPublicKey = peer.MyIdentity.Public
|
|
myContactCard.EncryptionPublicKey = peer.MyEncryptionKp.Public
|
|
myContactCard.LookupPublicKey = peer.MyLookupKp.Public
|
|
myContactCard.InvitationId = peer.InvitationId
|
|
id.Peers = append(id.Peers, peer)
|
|
|
|
return &myContactCard
|
|
}
|
|
|
|
func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageServerIdxs []int, ReceivedContact *meowlib.ContactCard) *meowlib.ContactCard {
|
|
var peer Peer
|
|
var myContactCard meowlib.ContactCard
|
|
peer.MyIdentity = meowlib.NewKeyPair()
|
|
peer.MyEncryptionKp = meowlib.NewKeyPair()
|
|
peer.MyLookupKp = meowlib.NewKeyPair()
|
|
if ContactName != "" {
|
|
peer.Name = ContactName
|
|
} else {
|
|
peer.Name = ReceivedContact.Name
|
|
}
|
|
peer.Contact = *ReceivedContact
|
|
|
|
for _, i := range MessageServerIdxs {
|
|
srv := id.MessageServers.Servers[i].ServerData
|
|
myContactCard.PullServers = append(myContactCard.PullServers, &srv)
|
|
}
|
|
myContactCard.Name = MyName
|
|
myContactCard.ContactPublicKey = peer.MyIdentity.Public
|
|
myContactCard.EncryptionPublicKey = peer.MyEncryptionKp.Public
|
|
myContactCard.LookupPublicKey = peer.MyLookupKp.Public
|
|
myContactCard.InvitationId = peer.InvitationId
|
|
|
|
id.Peers = append(id.Peers, peer)
|
|
|
|
return &myContactCard
|
|
}
|
|
|
|
func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) error {
|
|
for i, p := range id.Peers {
|
|
if p.InvitationId == ReceivedContact.InvitationId {
|
|
id.Peers[i].Contact = *ReceivedContact
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId)
|
|
}
|
|
|
|
func LoadIdentity(file string) (*Identity, error) {
|
|
var id Identity
|
|
indata, err := ioutil.ReadFile(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = json.Unmarshal([]byte(pass), &id)
|
|
return &id, err
|
|
}
|
|
|
|
func (id *Identity) Save(file string) error {
|
|
b, _ := json.Marshal(id)
|
|
armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = ioutil.WriteFile(file, []byte(armor), 0644)
|
|
return err
|
|
}
|