diff --git a/api/client.go b/api/client.go index bedc02b12e888ac8fc6c3e2b34abecf6ca828128..b3469d2206e8ac45c76c87f0f818f131d836e41a 100644 --- a/api/client.go +++ b/api/client.go @@ -33,6 +33,7 @@ import ( "gitlab.com/elixxir/primitives/switchboard" "gitlab.com/xx_network/comms/connect" goio "io" + "os" "path/filepath" "strings" "testing" @@ -169,9 +170,10 @@ func (cl *Client) Login(password string) (*id.ID, error) { cl.sessionV2 = io.SessionV2 regState, err := io.SessionV2.GetRegState() - if err != nil { - return nil, errors.Wrap(err, "Login: Could not login") + if err != nil && os.IsNotExist(err) { + return nil, errors.Wrap(err, "Login: Could not login: Could not get regState") } + if regState < user.KeyGenComplete { return nil, errors.New("Cannot log a user in which has not " + "completed registration ") diff --git a/api/client_test.go b/api/client_test.go index 190efcefc6096cd7dbf4b7e3085418a0eb546a0a..a273a15453c8e61cf78332d4f6cfde4628aa0897 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -759,6 +759,8 @@ func TestClient_LogoutAndLoginAgain(t *testing.T) { t.Fatalf("InitNetwork should have succeeded when creating second client %v", err) } + io.SessionV2.SetRegState(user.PermissioningComplete) + _, err = tc.Login("password") if err != nil { t.Logf("Login failed %+v", err) diff --git a/api/mockserver_test.go b/api/mockserver_test.go index 65c77a40883d1b48a7e87cb187644c8071a88923..69caf73c486b669bc0a0ba51d1ad058bf03d084d 100644 --- a/api/mockserver_test.go +++ b/api/mockserver_test.go @@ -21,7 +21,6 @@ import ( "gitlab.com/elixxir/primitives/ndf" "gitlab.com/xx_network/comms/connect" "os" - "path/filepath" "strings" "testing" "time" @@ -61,6 +60,7 @@ func TestMain(m *testing.M) { // Set logging params jww.SetLogThreshold(jww.LevelTrace) jww.SetStdoutThreshold(jww.LevelTrace) + fmt.Printf("\n\n\n\n\n\nindeed\n\n\n") io.SessionV2, _ = clientStorage.Init(".ekvapi", "test") os.Exit(testMainWrapper(m)) } @@ -100,9 +100,8 @@ 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") + io.SessionV2.SetRegState(user.KeyGenComplete) // Register with a valid registration code _, err = client.RegisterWithPermissioning(true, ValidRegCode) @@ -162,7 +161,7 @@ func TestRegister_ValidPrecannedRegCodeReturnsZeroID(t *testing.T) { if err != nil { t.Errorf("Could not generate Keys: %+v", err) } - + io.SessionV2.SetRegState(user.KeyGenComplete) // Register precanned user with all gateways regRes, err := client.RegisterWithPermissioning(true, ValidRegCode) @@ -297,7 +296,7 @@ func TestSend(t *testing.T) { } err = client.GenerateKeys(nil, "password") - + io.SessionV2.SetRegState(user.KeyGenComplete) // Register with a valid registration code userID, err := client.RegisterWithPermissioning(true, ValidRegCode) @@ -398,6 +397,8 @@ func TestLogout(t *testing.T) { t.Errorf("Could not generate Keys: %+v", err) } + io.SessionV2.SetRegState(user.KeyGenComplete) + // Register with a valid registration code _, err = client.RegisterWithPermissioning(true, ValidRegCode) diff --git a/api/register.go b/api/register.go index 69332cecc0a92ccff1d16120c44bf9712f78760f..748407de3bb242acd5793786d62825e0458f0f22 100644 --- a/api/register.go +++ b/api/register.go @@ -21,6 +21,7 @@ import ( "gitlab.com/elixxir/crypto/tls" "gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/ndf" + "os" "sync" "time" ) @@ -64,6 +65,7 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string for n, k := range nodeKeyMap { cl.session.PushNodeKey(&n, k) } + //update the state err = io.SessionV2.SetRegState(user.PermissioningComplete) if err != nil { @@ -179,8 +181,8 @@ func (cl *Client) RegisterWithNodes() error { usr := session.GetCurrentUser() //Load the registration signature regSignature, err := io.SessionV2.GetRegValidationSig() - if err != nil { - return err + if err != nil && !os.IsNotExist(err){ + return errors.Errorf("Failed to get registration signature: %v", err) } // Storage of the registration signature was broken in previous releases. diff --git a/api/register_test.go b/api/register_test.go index 291c655ba337e403603cce19de94268e12272a3e..f3fd438f7cdb0833f48b54a11c2afa1bb0c41900 100644 --- a/api/register_test.go +++ b/api/register_test.go @@ -8,6 +8,7 @@ package api import ( "crypto/sha256" "gitlab.com/elixxir/client/globals" + "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/primitives/id" "gitlab.com/xx_network/comms/connect" @@ -31,6 +32,8 @@ func TestRegistrationGob(t *testing.T) { t.Errorf("Could not generate Keys: %+v", err) } + io.SessionV2.SetRegState(user.KeyGenComplete) + // populate a gob in the store _, err = testClient.RegisterWithPermissioning(true, "WTROXJ33") if err != nil { @@ -74,6 +77,8 @@ func TestClient_Register(t *testing.T) { t.Errorf("Could not generate Keys: %+v", err) } + // fixme please (and all other places where this call is above RegisterWithPermissioning in tests) + io.SessionV2.SetRegState(user.KeyGenComplete) // populate a gob in the store _, err = testClient.RegisterWithPermissioning(true, "WTROXJ33") if err != nil { @@ -146,6 +151,7 @@ func TestRegister_ValidRegParams___(t *testing.T) { t.Errorf("%+v", err) } + io.SessionV2.SetRegState(user.KeyGenComplete) // Register precanned user with all gateways regRes, err := client.RegisterWithPermissioning(false, ValidRegCode) if err != nil { diff --git a/bindings/client.go b/bindings/client.go index 480a55fe8ec810be1e112cb91265b90f8e99d13f..07fc8ee9b4e7486e4cb221507d1209e7a6bad5bf 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -9,10 +9,13 @@ package bindings import ( "crypto/rand" "errors" + "fmt" "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/api" "gitlab.com/elixxir/client/globals" + clientIo "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/parse" + "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/primitives/id" "io" @@ -254,6 +257,14 @@ func (cl *Client) backoff(backoffCount int) { func (cl *Client) ChangeUsername(un string) error { globals.Log.INFO.Printf("Binding call: ChangeUsername()\n"+ " username: %s", un) + regState, err := clientIo.SessionV2.GetRegState() + if err != nil { + return errors.New(fmt.Sprintf("Could not get reg state: %v", err)) + } + if regState != user.PermissioningComplete { + return errors.New("Can only change username during " + + "PermissioningComplete registration state") + } return cl.client.GetSession().ChangeUsername(un) } diff --git a/bindings/client_test.go b/bindings/client_test.go index cddfb41381e04481f5e9fbca3deae8a47b761dee..196244830cd73cef012e7eb989a8b822732afbc5 100644 --- a/bindings/client_test.go +++ b/bindings/client_test.go @@ -141,6 +141,8 @@ func TestRegister(t *testing.T) { t.Errorf("Could not generate Keys: %+v", err) } + io.SessionV2.SetRegState(user.KeyGenComplete) + regRes, err := client.RegisterWithPermissioning(true, ValidRegCode) if err != nil { t.Errorf("Registration failed: %s", err.Error()) @@ -303,6 +305,7 @@ func TestClient_GetRegState(t *testing.T) { if err != nil { t.Errorf("Could not generate Keys: %+v", err) } + io.SessionV2.SetRegState(user.KeyGenComplete) // Register with a valid registration code _, err = testClient.RegisterWithPermissioning(true, ValidRegCode) @@ -311,11 +314,14 @@ func TestClient_GetRegState(t *testing.T) { t.Errorf("Register with permissioning failed: %s", err.Error()) } - if testClient.GetRegState() != int64(user.PermissioningComplete) { + regState, _ := io.SessionV2.GetRegState() + if regState != int64(user.PermissioningComplete) { t.Errorf("Unexpected reg state: Expected PermissioningComplete (%d), recieved: %d", user.PermissioningComplete, testClient.GetRegState()) } + io.SessionV2.SetRegValidationSig([]byte("test")) + err = testClient.RegisterWithNodes() if err != nil { t.Errorf("Register with nodes failed: %v", err.Error()) diff --git a/user/session.go b/user/session.go index 67f6050bf81701cd6ed56e0725fc42eb63edd001..6c2742926a5d4c078b45457952993da80617665b 100644 --- a/user/session.go +++ b/user/session.go @@ -493,11 +493,8 @@ func (s *SessionObj) SetRegState(rs uint32) error { } func (s *SessionObj) ChangeUsername(username string) error { - b := s.GetRegState() - if b != PermissioningComplete { - return errors.New("Can only change username during " + - "PermissioningComplete registration state") - } + + s.CurrentUser.Username = username return nil }