Identity load and save params, invitation check
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,5 +6,5 @@ import (
 | 
			
		||||
 | 
			
		||||
func TestGetFromPublicKey(t *testing.T) {
 | 
			
		||||
	id := CreateIdentity("test")
 | 
			
		||||
	id.Save("test.id")
 | 
			
		||||
	id.Save()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user