diff --git a/api/client.go b/api/client.go index c9f2bf336194b5cf4dd7169bbc6ca43b5a05bfa0..62ffb175be50ce5dafdbc3d12ba8ca8ea2c8073f 100644 --- a/api/client.go +++ b/api/client.go @@ -155,10 +155,6 @@ func (cl *Client) Login(password string) (*id.ID, error) { if session == nil { return nil, errors.New("Unable to load session, no error reported") } - if session.GetRegState() < user.KeyGenComplete { - return nil, errors.New("Cannot log a user in which has not " + - "completed registration ") - } cl.session = session @@ -172,6 +168,17 @@ func (cl *Client) Login(password string) (*id.ID, error) { } cl.sessionV2 = io.SessionV2 + // fixme fully remove the below + //if session.GetRegState() < user.KeyGenComplete { + regState, err := io.SessionV2.GetRegState() + if err != nil { + return nil, errors.Wrap(err, "Login: Could not login") + } + if regState < user.KeyGenComplete { + return nil, errors.New("Cannot log a user in which has not " + + "completed registration ") + } + newRm, err := io.NewReceptionManager(cl.rekeyChan, cl.session.GetCurrentUser().User, rsa.CreatePrivateKeyPem(cl.session.GetRSAPrivateKey()), rsa.CreatePublicKeyPem(cl.session.GetRSAPublicKey()), @@ -598,6 +605,12 @@ func (cl *Client) GetSession() user.Session { return cl.session } +// GetSession returns the session object for external access. Access at yourx +// own risk +func (cl *Client) GetSessionV2() *storage.Session { + return cl.sessionV2 +} + // ReceptionManager returns the comm manager object for external access. Access // at your own risk func (cl *Client) GetCommManager() *io.ReceptionManager { diff --git a/api/mockserver_test.go b/api/mockserver_test.go index 8be4e116dbbbb69eb87a2472e208d957c2aff9e7..65c77a40883d1b48a7e87cb187644c8071a88923 100644 --- a/api/mockserver_test.go +++ b/api/mockserver_test.go @@ -11,7 +11,7 @@ import ( "fmt" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/io" - "gitlab.com/elixxir/client/storage" + clientStorage "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/comms/gateway" pb "gitlab.com/elixxir/comms/mixmessages" @@ -21,6 +21,7 @@ import ( "gitlab.com/elixxir/primitives/ndf" "gitlab.com/xx_network/comms/connect" "os" + "path/filepath" "strings" "testing" "time" @@ -60,7 +61,7 @@ func TestMain(m *testing.M) { // Set logging params jww.SetLogThreshold(jww.LevelTrace) jww.SetStdoutThreshold(jww.LevelTrace) - io.SessionV2, _ = storage.Init(".ekvapi", "test") + io.SessionV2, _ = clientStorage.Init(".ekvapi", "test") os.Exit(testMainWrapper(m)) } @@ -81,8 +82,8 @@ func TestClient_StartMessageReceiver_MultipleMessages(t *testing.T) { } testDef.Nodes = def.Nodes - - storage := DummyStorage{LocationA: ".ekv-messagereceiver-multiple", StoreA: []byte{'a', 'b', 'c'}} + locA := ".ekv-messagereceiver-multiple" + storage := DummyStorage{LocationA: locA, StoreA: []byte{'a', 'b', 'c'}} client, err := NewClient(&storage, ".ekv-messagereceiver-multiple", "", testDef) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) @@ -99,6 +100,9 @@ func TestClient_StartMessageReceiver_MultipleMessages(t *testing.T) { if err != nil { t.Errorf("Could not generate Keys: %+v", err) } + dirname := filepath.Dir(locA) + + io.SessionV2, err = clientStorage.Init(dirname, "password") // Register with a valid registration code _, err = client.RegisterWithPermissioning(true, ValidRegCode) diff --git a/api/register.go b/api/register.go index 921738be22150f4fa06a187a0f375a622ce2aaa7..02d68e04c5b9c606cc0da469f53d8dbb847781c6 100644 --- a/api/register.go +++ b/api/register.go @@ -12,6 +12,7 @@ import ( "github.com/pkg/errors" "gitlab.com/elixxir/client/bots" "gitlab.com/elixxir/client/globals" + "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/hash" @@ -30,12 +31,18 @@ const SaltSize = 32 // Returns an error if registration fails. func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string) (*id.ID, error) { //Check the regState is in proper state for registration - if cl.session.GetRegState() != user.KeyGenComplete { + // fixme fully remove the below + //if cl.session.GetRegState() != user.KeyGenComplete { + regState, err := io.SessionV2.GetRegState() + if err != nil { + return nil, err + } + + if regState != user.KeyGenComplete { return nil, errors.Errorf("Attempting to register before key generation!") } usr := cl.session.GetCurrentUser() UID := usr.User - var err error //Initialized response from Registration Server regValidationSignature := make([]byte, 0) @@ -60,10 +67,15 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string cl.session.PushNodeKey(&n, k) } //update the state - err := cl.session.SetRegState(user.PermissioningComplete) + err = io.SessionV2.SetRegState(user.PermissioningComplete) if err != nil { - return nil, errors.Wrap(err, "Could not do precanned registration") + return &id.ZeroUser, err } + // fixme fully remove the below + //err := cl.session.SetRegState(user.PermissioningComplete) + //if err != nil { + // return nil, errors.Wrap(err, "Could not do precanned registration") + //} } else { // Or register with the permissioning server and generate user information @@ -73,11 +85,18 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string return &id.ZeroUser, err } //update the session with the registration - err = cl.session.RegisterPermissioningSignature(regValidationSignature) + err = io.SessionV2.SetRegState(user.PermissioningComplete) + if err != nil { + return nil, err + } + err = io.SessionV2.SetRegValidationSig(regValidationSignature) if err != nil { return nil, err } + // fixme fully remove the below + //err = cl.session.RegisterPermissioningSignature(regValidationSignature) + } //Set the registration secure state @@ -97,16 +116,19 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string // User discovery. Must be called after Register and InitNetwork. // It will fail if the user has already registered with UDB func (cl *Client) RegisterWithUDB(username string, timeout time.Duration) error { + // fixme fully remove the below - regState := cl.GetSession().GetRegState() + //regState := cl.GetSession().GetRegState() + regState, err := io.SessionV2.GetRegState() + if err != nil { + return err + } if regState != user.PermissioningComplete { return errors.New("Cannot register with UDB when registration " + "state is not PermissioningComplete") } - var err error - if username != "" { err := cl.session.ChangeUsername(username) if err != nil { @@ -129,7 +151,9 @@ func (cl *Client) RegisterWithUDB(username string, timeout time.Duration) error } //set the registration state - err = cl.session.SetRegState(user.UDBComplete) + err = io.SessionV2.SetRegState(user.UDBComplete) + // fixme fully remove the below + //err = cl.session.SetRegState(user.UDBComplete) if err != nil { return errors.Wrap(err, "UDB Registration Failed") @@ -169,7 +193,12 @@ func (cl *Client) RegisterWithNodes() error { UID := session.GetCurrentUser().User usr := session.GetCurrentUser() //Load the registration signature - regSignature := session.GetRegistrationValidationSignature() + // fixme: remove the bewow commented code + //regSignature := cl.session.GetRegistrationValidationSignature() + regSignature, err := io.SessionV2.GetRegValidationSig() + if err != nil { + return err + } // Storage of the registration signature was broken in previous releases. // get the signature again from permissioning if it is absent @@ -194,7 +223,13 @@ func (cl *Client) RegisterWithNodes() error { //update the session with the registration //HACK HACK HACK sesObj := cl.session.(*user.SessionObj) - sesObj.RegValidationSignature = regSignature + // fixme: remove the commented code + //sesObj.RegValidationSignature = regSignature + err = io.SessionV2.SetRegValidationSig(regSignature) + if err != nil { + return err + } + err = sesObj.StoreSession() if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 825cb3218ef90899680dff436a6f9d2f6478a3d5..49d072cafb231b23f5ca29da3669b5c9e21b9ffd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ import ( "gitlab.com/elixxir/client/api" "gitlab.com/elixxir/client/cmixproto" "gitlab.com/elixxir/client/globals" + "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/signature/rsa" @@ -441,7 +442,15 @@ var rootCmd = &cobra.Command{ // todo: since this is in the root cmd, would checking the regstate directly really be bad? // It's correct that it should be an error state for RegisterWithUDB, however for this, it's start up code - if username != "" && client.GetSession().GetRegState() == user.PermissioningComplete { + // fixme fully remove the below + //if username != "" && client.GetSession().GetRegState() == user.PermissioningComplete { + + regState, err := io.SessionV2.GetRegState() + if err != nil { + globals.Log.FATAL.Panicf("Could not retrieve registration state: %v", err) + } + + if username != "" && regState == user.PermissioningComplete { err := client.RegisterWithUDB(username, 2*time.Minute) if err != nil { globals.Log.ERROR.Printf("%+v", err) diff --git a/storage/registration.go b/storage/registration.go index 27fcfab2642530a37f5ee5970917956e03a73a21..338dbd7f052bb44b7e57314c3b40ec4f4ffefe92 100644 --- a/storage/registration.go +++ b/storage/registration.go @@ -11,7 +11,7 @@ import ( "time" ) -var currentVersion = uint64(0) +var currentRegistrationVersion = uint64(0) // SetRegValidationSig builds the versioned object and sets it in the key-value store func (s *Session) SetRegValidationSig(newVal []byte) error { @@ -25,13 +25,13 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { // Construct the versioned object vo := &VersionedObject{ - Version: currentVersion, + Version: currentRegistrationVersion, Timestamp: nowText, Data: newVal, } // Construct the key and place in the key-value store - key := MakeKeyPrefix("RegValidationSig", currentVersion) + key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) return s.kv.Set(key, vo) } @@ -39,7 +39,7 @@ func (s *Session) SetRegValidationSig(newVal []byte) error { // GetRegValidationSig pulls the versioned object by the key and parses // it into the requested registration signature func (s *Session) GetRegValidationSig() ([]byte, error) { - key := MakeKeyPrefix("RegValidationSig", currentVersion) + key := MakeKeyPrefix("RegValidationSig", currentRegistrationVersion) // Pull the object from the key-value store voData, err := s.kv.Get(key) @@ -47,9 +47,9 @@ func (s *Session) GetRegValidationSig() ([]byte, error) { return nil, err } - if voData.Version != currentVersion { + if voData.Version != currentRegistrationVersion { globals.Log.WARN.Printf("Session.GetRegValidationSig: got unexpected version %v, expected version %v", - voData.Version, currentVersion) + voData.Version, currentRegistrationVersion) } return voData.Data, nil @@ -63,7 +63,7 @@ func (s *Session) SetRegState(newVal int64) error { return err } - key := MakeKeyPrefix("RegState", currentVersion) + key := MakeKeyPrefix("RegState", currentRegistrationVersion) var data []byte data, err = json.Marshal(newVal) @@ -72,7 +72,7 @@ func (s *Session) SetRegState(newVal int64) error { } obj := VersionedObject{ - Version: currentVersion, + Version: currentRegistrationVersion, Timestamp: now, Data: data, } @@ -84,7 +84,7 @@ func (s *Session) SetRegState(newVal int64) error { // it into the requested registration signature func (s *Session) GetRegState() (int64, error) { // Construct the key from the - key := MakeKeyPrefix("RegState", currentVersion) + key := MakeKeyPrefix("RegState", currentRegistrationVersion) // Pull the object from the key-value store voData, err := s.kv.Get(key) @@ -92,9 +92,9 @@ func (s *Session) GetRegState() (int64, error) { return 0, err } - if voData.Version != currentVersion { + if voData.Version != currentRegistrationVersion { globals.Log.WARN.Printf("Session.GetRegState: got unexpected version %v, expected version %v", - voData.Version, currentVersion) + voData.Version, currentRegistrationVersion) } var data int64 diff --git a/storage/registration_test.go b/storage/registration_test.go index 074346f2c8d54d939e6c6a29abec5278f50ac633..db6fdc447b645bf77092ae80aaa58364fb999d9e 100644 --- a/storage/registration_test.go +++ b/storage/registration_test.go @@ -7,13 +7,11 @@ package storage import ( "bytes" - "gitlab.com/elixxir/ekv" "testing" ) func TestSession_RegState(t *testing.T) { - store := make(ekv.Memstore) - testSession := &Session{NewVersionedKV(store)} + testSession := InitTestingSession(t) expectedVal := int64(42) err := testSession.SetRegState(expectedVal) @@ -35,8 +33,7 @@ func TestSession_RegState(t *testing.T) { } func TestSession_RegValidation(t *testing.T) { - store := make(ekv.Memstore) - testSession := &Session{NewVersionedKV(store)} + testSession := InitTestingSession(t) expectedVal := []byte("testData") diff --git a/storage/session.go b/storage/session.go index d760776eff1e15b1c7f70b7a721793dc55cebd74..f8d33d5dbee77e00200d62b99629e8f6686c07e5 100644 --- a/storage/session.go +++ b/storage/session.go @@ -9,7 +9,10 @@ package storage import ( + "fmt" + "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/ekv" + "testing" "time" ) @@ -27,7 +30,7 @@ func Init(baseDir, password string) (*Session, error) { kv: NewVersionedKV(fs), } } - + fmt.Printf("key val: %v\n", s.kv) return s, err } @@ -62,3 +65,22 @@ func (s *Session) SetLastMessageId(id string) error { } return s.kv.Set("LastMessageID", vo) } + +// Initializes a Session object wrapped around a MemStore object. +// FOR TESTING ONLY +func InitTestingSession(i interface{}) *Session { + switch i.(type) { + case *testing.T: + break + case *testing.M: + break + case *testing.B: + break + default: + globals.Log.FATAL.Panicf("InitTestingSession is restricted to testing only. Got %T", i) + } + + store := make(ekv.Memstore) + return &Session{NewVersionedKV(store)} + +} diff --git a/user/session.go b/user/session.go index 04665d6579c272144b08d222d7d7e911c882881b..67f6050bf81701cd6ed56e0725fc42eb63edd001 100644 --- a/user/session.go +++ b/user/session.go @@ -394,15 +394,17 @@ func (s *SessionObj) PushNodeKey(id *id.ID, key NodeKeys) { func (s *SessionObj) RegisterPermissioningSignature(sig []byte) error { s.LockStorage() defer s.UnlockStorage() - err := s.SetRegState(PermissioningComplete) - if err != nil { - return errors.Wrap(err, "Could not store permissioning signature") - } - s.RegValidationSignature = sig + // fixme remove the below + //err := s.SetRegState(PermissioningComplete) + //if err != nil { + // return errors.Wrap(err, "Could not store permissioning signature") + //} + // + //s.RegValidationSignature = sig //storing to ensure we never loose the signature - err = s.storeSession() + err := s.storeSession() return err }