diff --git a/client/config.go b/client/config.go index 382ce56..da9d576 100644 --- a/client/config.go +++ b/client/config.go @@ -31,6 +31,7 @@ type Config struct { ChannelNotifications bool `json:"channel_notifications,omitempty"` // Inner memoryPassword string `json:"memory_password,omitempty"` + identityFile string `json:"config_file,omitempty"` } var instance *Config diff --git a/client/identity.go b/client/identity.go index 6908595..1d0ba85 100644 --- a/client/identity.go +++ b/client/identity.go @@ -55,6 +55,15 @@ func (id *Identity) InvitePeer(MyName string, ContactName string, MessageServerI 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 { var peer Peer 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) } -func LoadIdentity(file string) (*Identity, error) { +func LoadIdentity(filename string, password string) (*Identity, error) { var id Identity - indata, err := ioutil.ReadFile(file) + GetConfig().memoryPassword = password + GetConfig().identityFile = filename + indata, err := ioutil.ReadFile(filename) if err != nil { return nil, err } - pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata)) + pass, err := helper.DecryptMessageWithPassword([]byte(password), string(indata)) if err != nil { return nil, err } @@ -107,12 +118,12 @@ func LoadIdentity(file string) (*Identity, error) { return &id, err } -func (id *Identity) Save(file string) error { +func (id *Identity) Save() error { b, _ := json.Marshal(id) - armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b)) + armor, err := helper.EncryptMessageWithPassword([]byte(GetConfig().memoryPassword), string(b)) if err != nil { return err } - err = ioutil.WriteFile(file, []byte(armor), 0644) + err = ioutil.WriteFile(GetConfig().identityFile, []byte(armor), 0600) return err } diff --git a/client/identity_test.go b/client/identity_test.go index eae3a5d..e13fc74 100644 --- a/client/identity_test.go +++ b/client/identity_test.go @@ -2,20 +2,40 @@ package client import ( "log" + "os" "testing" "github.com/stretchr/testify/assert" ) -func TestCreate(t *testing.T) { - id := CreateIdentity("myname") - id.Save("test.id") +func exists(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + } else { + return false + } } 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 { 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.") + if exists("test.id") { + os.Remove("test.id") + } +} + +func TestCreate(t *testing.T) { + } diff --git a/client/peer_test.go b/client/peer_test.go index 41aa8b4..2288d42 100644 --- a/client/peer_test.go +++ b/client/peer_test.go @@ -6,5 +6,5 @@ import ( func TestGetFromPublicKey(t *testing.T) { id := CreateIdentity("test") - id.Save("test.id") + id.Save() } diff --git a/endtoend_test.go b/endtoend_test.go index 13b27ae..43487c5 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -16,7 +16,7 @@ func TestEndToEnd(t *testing.T) { // Create my own identity // fmt.Println("Trying to load identity from file.") - Me, err := client.LoadIdentity("id.enc") + Me, err := client.LoadIdentity("id.enc", "Test") if err != nil { fmt.Println("Failed : creating New identity...") /////////////////////////// @@ -65,10 +65,10 @@ func TestEndToEnd(t *testing.T) { // Finalize the contact with the invitation response // /////////////////////////////////////////////////////// Me.FinalizeInvitation(&ReceivedContact) - /*err = Me.Save("id.enc") + err = Me.Save() if err != nil { fmt.Println(err.Error()) - }*/ + } a, _ = json.Marshal(Me) ioutil.WriteFile("id.json", a, 0644) diff --git a/qrcode.png b/qrcode.png new file mode 100644 index 0000000..a0a1658 Binary files /dev/null and b/qrcode.png differ