Identity load and save params, invitation check

This commit is contained in:
ycc 2022-11-28 20:48:42 +01:00
parent f4ebb5cab1
commit b464a855ae
6 changed files with 46 additions and 14 deletions

View File

@ -31,6 +31,7 @@ type Config struct {
ChannelNotifications bool `json:"channel_notifications,omitempty"` ChannelNotifications bool `json:"channel_notifications,omitempty"`
// Inner // Inner
memoryPassword string `json:"memory_password,omitempty"` memoryPassword string `json:"memory_password,omitempty"`
identityFile string `json:"config_file,omitempty"`
} }
var instance *Config var instance *Config

View File

@ -55,6 +55,15 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI
return &myContactCard return &myContactCard
} }
func (id *Identity) CheckInvitation(ReceivedContact *meowlib.ContactCard) (isAnswer bool, proposedNick string, receivedNick string) {
for _, p := range id.Peers {
if p.InvitationId == ReceivedContact.InvitationId {
return true, p.Name, ReceivedContact.Name
}
}
return false, "", ReceivedContact.Name
}
func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageServerIdxs []int, ReceivedContact *meowlib.ContactCard) *meowlib.ContactCard { func (id *Identity) AnswerInvitation(MyName string, ContactName string, MessageServerIdxs []int, ReceivedContact *meowlib.ContactCard) *meowlib.ContactCard {
var peer Peer var peer Peer
var myContactCard meowlib.ContactCard var myContactCard meowlib.ContactCard
@ -93,13 +102,15 @@ func (id *Identity) FinalizeInvitation(ReceivedContact *meowlib.ContactCard) err
return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId) return errors.New("no matching contact found for invitationId " + ReceivedContact.InvitationId)
} }
func LoadIdentity(file string) (*Identity, error) { func LoadIdentity(filename string, password string) (*Identity, error) {
var id Identity var id Identity
indata, err := ioutil.ReadFile(file) GetConfig().memoryPassword = password
GetConfig().identityFile = filename
indata, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata)) pass, err := helper.DecryptMessageWithPassword([]byte(password), string(indata))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -107,12 +118,12 @@ func LoadIdentity(file string) (*Identity, error) {
return &id, err return &id, err
} }
func (id *Identity) Save(file string) error { func (id *Identity) Save() error {
b, _ := json.Marshal(id) b, _ := json.Marshal(id)
armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b)) armor, err := helper.EncryptMessageWithPassword([]byte(GetConfig().memoryPassword), string(b))
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(file, []byte(armor), 0644) err = ioutil.WriteFile(GetConfig().identityFile, []byte(armor), 0600)
return err return err
} }

View File

@ -2,20 +2,40 @@ package client
import ( import (
"log" "log"
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestCreate(t *testing.T) { func exists(filename string) bool {
id := CreateIdentity("myname") if _, err := os.Stat(filename); err == nil {
id.Save("test.id") return true
} else {
return false
}
} }
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
id, err := LoadIdentity("test.id") if exists("test.id") {
os.Remove("test.id")
}
id, err := LoadIdentity("test.id", "toto")
if err != nil {
id := CreateIdentity("myname")
id.Save()
}
id, err = LoadIdentity("test.id", "toto")
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
assert.Equal(t, err, nil, "2nd Load error")
assert.Equal(t, id.Nickname, "myname", "The two words should be the same.") assert.Equal(t, id.Nickname, "myname", "The two words should be the same.")
if exists("test.id") {
os.Remove("test.id")
}
}
func TestCreate(t *testing.T) {
} }

View File

@ -6,5 +6,5 @@ import (
func TestGetFromPublicKey(t *testing.T) { func TestGetFromPublicKey(t *testing.T) {
id := CreateIdentity("test") id := CreateIdentity("test")
id.Save("test.id") id.Save()
} }

View File

@ -16,7 +16,7 @@ func TestEndToEnd(t *testing.T) {
// Create my own identity // Create my own identity
// //
fmt.Println("Trying to load identity from file.") fmt.Println("Trying to load identity from file.")
Me, err := client.LoadIdentity("id.enc") Me, err := client.LoadIdentity("id.enc", "Test")
if err != nil { if err != nil {
fmt.Println("Failed : creating New identity...") fmt.Println("Failed : creating New identity...")
/////////////////////////// ///////////////////////////
@ -65,10 +65,10 @@ func TestEndToEnd(t *testing.T) {
// Finalize the contact with the invitation response // // Finalize the contact with the invitation response //
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
Me.FinalizeInvitation(&ReceivedContact) Me.FinalizeInvitation(&ReceivedContact)
/*err = Me.Save("id.enc") err = Me.Save()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
}*/ }
a, _ = json.Marshal(Me) a, _ = json.Marshal(Me)
ioutil.WriteFile("id.json", a, 0644) ioutil.WriteFile("id.json", a, 0644)

BIN
qrcode.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB