diff --git a/api/authenticatedChannel.go b/api/authenticatedChannel.go index d1b5aa3ef75e573c9e39c9aaff8001656ef5e476..8b898acaf44a3a7b5378e00c66cf33fad952a5f3 100644 --- a/api/authenticatedChannel.go +++ b/api/authenticatedChannel.go @@ -10,11 +10,11 @@ import ( // so this user can send messages to the desired recipient Contact. // To receive confirmation from the remote user, clients must // register a listener to do that. -func (c *Client) CreateAuthenticatedChannel(recipient contact.Contact, - payload []byte) error { - jww.INFO.Printf("CreateAuthenticatedChannel(%v, %v)", - recipient, payload) - return nil +func (c *Client) CreateAuthenticatedChannel(recipient contact.Contact) error { + jww.INFO.Printf("CreateAuthenticatedChannel(%v)", recipient) + sesParam := e2e.GetDefaultSessionParams() + return c.storage.E2e().AddPartner(recipient.ID, recipient.DhPubKey, + sesParam, sesParam) } // RegisterAuthConfirmationCb registers a callback for channel @@ -32,15 +32,15 @@ func (c *Client) RegisterAuthRequestCb(cb func(contact contact.Contact, } // Create an insecure e2e relationship with a precanned user -func (c *Client) MakePrecannedAuthenticatedChannel(precannedID uint) contact.Contact { +func (c *Client) MakePrecannedAuthenticatedChannel(precannedID uint) (contact.Contact, error) { precan := c.MakePrecannedContact(precannedID) //add the precanned user as a e2e contact sesParam := e2e.GetDefaultSessionParams() - c.storage.E2e().AddPartner(precan.ID, precan.DhPubKey, sesParam, sesParam) + err := c.storage.E2e().AddPartner(precan.ID, precan.DhPubKey, sesParam, sesParam) - return precan + return precan, err } // Create an insecure e2e contact object for a precanned user diff --git a/cmd/root.go b/cmd/root.go index deb74b6d5e365466ba458db6a4dc38e4e275bb31..227d38d598fa1c068e2829da29aa17bbfafaad6b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -103,6 +103,8 @@ var rootCmd = &cobra.Command{ pass := viper.GetString("password") storeDir := viper.GetString("session") regCode := viper.GetString("regcode") + precannedID := viper.GetUint("precannedID") + precannedPartner := viper.GetUint("precannedPartner") //create a new client if none exist if _, err := os.Stat(storeDir); os.IsNotExist(err) { @@ -113,8 +115,14 @@ var rootCmd = &cobra.Command{ jww.FATAL.Panicf(err.Error()) } - err = api.NewClient(string(ndfJSON), storeDir, - []byte(pass), regCode) + if precannedID != 0 { + err = api.NewPrecannedClient(precannedID, string(ndfJSON), + storeDir, []byte(pass)) + } else { + err = api.NewClient(string(ndfJSON), storeDir, + []byte(pass), regCode) + } + if err != nil { jww.FATAL.Panicf("%+v", err) } @@ -126,6 +134,10 @@ var rootCmd = &cobra.Command{ jww.FATAL.Panicf("%+v", err) } + if precannedPartner != 0 { + client.MakePrecannedContact(precannedPartner) + } + user := client.GetUser() jww.INFO.Printf("%s", user.ID) diff --git a/storage/e2e/store.go b/storage/e2e/store.go index 03f1cf27721fd0e34073201221ea293f12f03aca..b30d1c47913cbad1f5b885ad85b28a622d3d7957 100644 --- a/storage/e2e/store.go +++ b/storage/e2e/store.go @@ -144,10 +144,14 @@ func (s *Store) save() error { } func (s *Store) AddPartner(partnerID *id.ID, partnerPubKey *cyclic.Int, - sendParams, receiveParams SessionParams) { + sendParams, receiveParams SessionParams) error { s.mux.Lock() defer s.mux.Unlock() + if _, ok := s.managers[*partnerID]; ok { + return errors.New("Cannot overwrite existing partner") + } + m := newManager(s.context, s.kv, partnerID, s.dhPrivateKey, partnerPubKey, sendParams, receiveParams) @@ -156,6 +160,8 @@ func (s *Store) AddPartner(partnerID *id.ID, partnerPubKey *cyclic.Int, jww.FATAL.Printf("Failed to add Parter %s: Save of store failed: %s", partnerID, err) } + + return nil } func (s *Store) GetPartner(partnerID *id.ID) (*Manager, error) {