diff options
| -rw-r--r-- | cmd/cli/main.go | 24 | ||||
| -rw-r--r-- | exports/exports.go | 7 | ||||
| -rw-r--r-- | internal/discovery/discovery.go | 11 | ||||
| -rw-r--r-- | internal/fsm/fsm.go | 6 | ||||
| -rw-r--r-- | internal/server/common.go | 32 | ||||
| -rw-r--r-- | state.go | 25 | ||||
| -rw-r--r-- | state_test.go | 19 | ||||
| -rw-r--r-- | wrappers/python/tests.py | 5 |
8 files changed, 54 insertions, 75 deletions
diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 80f4baf..c062303 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -12,6 +12,7 @@ import ( "github.com/jwijenbergh/eduvpn-common" "github.com/jwijenbergh/eduvpn-common/internal/fsm" + "github.com/jwijenbergh/eduvpn-common/internal/server" ) type ServerTypes int8 @@ -23,7 +24,12 @@ const ( ) // Open a browser with xdg-open -func openBrowser(urlString string) { +func openBrowser(url interface{}) { + urlString, ok := url.(string) + + if !ok { + return + } fmt.Printf("OAuth: Initialized with AuthURL %s\n", urlString) fmt.Println("OAuth: Opening browser with xdg-open...") exec.Command("xdg-open", urlString).Start() @@ -45,14 +51,12 @@ type ServerProfileInfo struct { } // Ask for a profile in the command line -func sendProfile(state *eduvpn.VPNState, data string) { +func sendProfile(state *eduvpn.VPNState, data interface{}) { fmt.Printf("Multiple VPN profiles found. Please select a profile by entering e.g. 1") - serverProfiles := &ServerProfileInfo{} + serverProfiles, ok := data.(*server.ServerProfileInfo) - jsonErr := json.Unmarshal([]byte(data), &serverProfiles) - - if jsonErr != nil { - fmt.Println("\nFailed to get profile list", jsonErr) + if !ok { + fmt.Errorf("Invalid data type") return } @@ -87,7 +91,7 @@ func sendProfile(state *eduvpn.VPNState, data string) { // If OAuth is started we open the browser with the Auth URL // If we ask for a profile, we send the profile using command line input // Note that this has an additional argument, the vpn state which was wrapped into this callback function below -func stateCallback(state *eduvpn.VPNState, oldState eduvpn.VPNStateID, newState eduvpn.VPNStateID, data string) { +func stateCallback(state *eduvpn.VPNState, oldState eduvpn.VPNStateID, newState eduvpn.VPNStateID, data interface{}) { // TODO: Remove internal usage of fsm if newState == fsm.OAUTH_STARTED { openBrowser(data) @@ -164,7 +168,7 @@ func storeSecureInternetConfig(state *eduvpn.VPNState, url string, directory str func getSecureInternetAll(homeURL string) { state := &eduvpn.VPNState{} - state.Register("org.eduvpn.app.linux", "configs", func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configs", func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data interface{}) { stateCallback(state, old, new, data) }, true) @@ -205,7 +209,7 @@ func getSecureInternetAll(homeURL string) { func printConfig(url string, serverType ServerTypes) { state := &eduvpn.VPNState{} - state.Register("org.eduvpn.app.linux", "configs", func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configs", func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data interface{}) { stateCallback(state, old, new, data) }, true) diff --git a/exports/exports.go b/exports/exports.go index 5e4aab3..7f842ec 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -26,7 +26,7 @@ var P_StateCallbacks map[string]C.PythonCB var VPNStates map[string]*eduvpn.VPNState -func StateCallback(name string, old_state eduvpn.VPNStateID, new_state eduvpn.VPNStateID, data string) { +func StateCallback(name string, old_state eduvpn.VPNStateID, new_state eduvpn.VPNStateID, data interface{}) { P_StateCallback, exists := P_StateCallbacks[name] if !exists || P_StateCallback == nil { return @@ -34,7 +34,8 @@ func StateCallback(name string, old_state eduvpn.VPNStateID, new_state eduvpn.VP name_c := C.CString(name) oldState_c := C.int(old_state) newState_c := C.int(new_state) - data_c := C.CString(data) + data_json, _ := json.Marshal(data) + data_c := C.CString(string(data_json)) C.call_callback(P_StateCallback, name_c, oldState_c, newState_c, data_c) C.free(unsafe.Pointer(name_c)) C.free(unsafe.Pointer(data_c)) @@ -65,7 +66,7 @@ func Register(name *C.char, config_directory *C.char, stateCallback C.PythonCB, } VPNStates[nameStr] = state P_StateCallbacks[nameStr] = stateCallback - registerErr := state.Register(nameStr, C.GoString(config_directory), func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data string) { + registerErr := state.Register(nameStr, C.GoString(config_directory), func(old eduvpn.VPNStateID, new eduvpn.VPNStateID, data interface{}) { StateCallback(nameStr, old, new, data) }, debug != 0) diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 733b446..79e7230 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -65,21 +65,14 @@ func (discovery *Discovery) DetermineOrganizationsUpdate() bool { return discovery.Organizations.Timestamp.IsZero() } -func (discovery *Discovery) GetSecureLocationList() (string, error) { +func (discovery *Discovery) GetSecureLocationList() []string { var locations []string for _, server := range discovery.Servers.List { if server.Type == "secure_internet" { locations = append(locations, server.CountryCode) } } - - jsonBytes, jsonErr := json.Marshal(locations) - - if jsonErr != nil { - return "", &types.WrappedErrorMessage{Message: "failed getting Secure Internet locations list", Err: jsonErr} - } - - return string(jsonBytes), nil + return locations } func (discovery *Discovery) GetServerByURL(url string, _type string) (*types.DiscoveryServer, error) { diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go index 84c39d5..3c51ce7 100644 --- a/internal/fsm/fsm.go +++ b/internal/fsm/fsm.go @@ -124,12 +124,12 @@ type FSM struct { // Info to be passed from the parent state Name string - StateCallback func(FSMStateID, FSMStateID, string) + StateCallback func(FSMStateID, FSMStateID, interface{}) Directory string Debug bool } -func (fsm *FSM) Init(name string, callback func(FSMStateID, FSMStateID, string), directory string, debug bool) { +func (fsm *FSM) Init(name string, callback func(FSMStateID, FSMStateID, interface{}), directory string, debug bool) { fsm.States = FSMStates{ DEREGISTERED: FSMState{Transitions: []FSMTransition{{NO_SERVER, "Client registers"}}}, NO_SERVER: FSMState{Transitions: []FSMTransition{{CHOSEN_SERVER, "User chooses a server"}, {SEARCH_SERVER, "The user is trying to choose a Server in the UI"}, {CONNECTED, "The user is already connected"}, {ASK_LOCATION, "Change the location in the main screen"}}}, @@ -191,7 +191,7 @@ func (fsm *FSM) GoBack() { fsm.GoTransition(fsm.States[fsm.Current].BackState) } -func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data string, background bool) bool { +func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data interface{}, background bool) bool { ok := fsm.HasTransition(newState) if ok { diff --git a/internal/server/common.go b/internal/server/common.go index e8c837a..7ee8be6 100644 --- a/internal/server/common.go +++ b/internal/server/common.go @@ -1,7 +1,6 @@ package server import ( - "encoding/json" "fmt" "time" @@ -147,9 +146,7 @@ func getServerInfoScreen(base ServerBase) ServerInfoScreen { return serverInfoScreen } -func (servers *Servers) GetServersConfiguredJSON() (string, error) { - errorMessage := "failed getting configured servers JSON" - +func (servers *Servers) GetServersConfigured() (*ServersConfiguredScreen) { customServersInfo := []ServerInfoScreen{} instituteServersInfo := []ServerInfoScreen{} var secureInternetServerInfo *ServerInfoScreen = nil @@ -174,28 +171,21 @@ func (servers *Servers) GetServersConfiguredJSON() (string, error) { secureInternetServerInfo.CountryCode = servers.SecureInternetHomeServer.CurrentLocation } - serversConfiguredScreen := &ServersConfiguredScreen{CustomServers: customServersInfo, InstituteAccessServers: instituteServersInfo, SecureInternetServer: secureInternetServerInfo} - - bytes, bytesErr := json.Marshal(serversConfiguredScreen) - - if bytesErr != nil { - return "{}", &types.WrappedErrorMessage{Message: errorMessage, Err: bytesErr} - } - return string(bytes), nil + return &ServersConfiguredScreen{CustomServers: customServersInfo, InstituteAccessServers: instituteServersInfo, SecureInternetServer: secureInternetServerInfo} } -func (servers *Servers) GetCurrentServerInfoJSON() (string, error) { - errorMessage := "failed getting JSON for server" +func (servers *Servers) GetCurrentServerInfo() (*ServerInfoScreen, error) { + errorMessage := "failed getting current server info" currentServer, currentServerErr := servers.GetCurrentServer() if currentServerErr != nil { - return "{}", &types.WrappedErrorMessage{Message: errorMessage, Err: currentServerErr} + return nil, &types.WrappedErrorMessage{Message: errorMessage, Err: currentServerErr} } base, baseErr := currentServer.GetBase() if baseErr != nil { - return "{}", &types.WrappedErrorMessage{Message: errorMessage, Err: baseErr} + return nil, &types.WrappedErrorMessage{Message: errorMessage, Err: baseErr} } serverInfoScreen := getServerInfoScreen(*base) @@ -205,13 +195,7 @@ func (servers *Servers) GetCurrentServerInfoJSON() (string, error) { serverInfoScreen.CountryCode = servers.SecureInternetHomeServer.CurrentLocation } - bytes, bytesErr := json.Marshal(serverInfoScreen) - - if bytesErr != nil { - return "{}", &types.WrappedErrorMessage{Message: errorMessage, Err: bytesErr} - } - - return string(bytes), nil + return &serverInfoScreen, nil } func (servers *Servers) addInstituteAndCustom(discoServer *types.DiscoveryServer, isCustom bool, fsm *fsm.FSM) (Server, error) { @@ -491,7 +475,7 @@ func askForProfileID(server Server) error { if !base.FSM.HasTransition(fsm.ASK_PROFILE) { return &types.WrappedErrorMessage{Message: errorMessage, Err: fsm.WrongStateTransitionError{Got: base.FSM.Current, Want: fsm.ASK_PROFILE}.CustomError()} } - base.FSM.GoTransitionWithData(fsm.ASK_PROFILE, base.ProfilesRaw, false) + base.FSM.GoTransitionWithData(fsm.ASK_PROFILE, &base.Profiles, false) return nil } @@ -35,17 +35,11 @@ type VPNState struct { Debug bool `json:"-"` } -func (state *VPNState) GetSavedServers() string { - serversJSON, serversJSONErr := state.Servers.GetServersConfiguredJSON() - - if serversJSONErr != nil { - return "{}" - } - - return serversJSON +func (state *VPNState) GetSavedServers() *server.ServersConfiguredScreen { + return state.Servers.GetServersConfigured() } -func (state *VPNState) Register(name string, directory string, stateCallback func(VPNStateID, VPNStateID, string), debug bool) error { +func (state *VPNState) Register(name string, directory string, stateCallback func(VPNStateID, VPNStateID, interface{}), debug bool) error { errorMessage := "failed to register with the GO library" if !state.FSM.InState(fsm.DEREGISTERED) { return &types.WrappedErrorMessage{Message: errorMessage, Err: fsm.DeregisteredError{}.CustomError()} @@ -159,11 +153,7 @@ func (state *VPNState) SetSecureLocation(countryCode string) error { } func (state *VPNState) askSecureLocation() error { - errorMessage := "failed asking Secure Internet location" - locations, locationsErr := state.Discovery.GetSecureLocationList() - if locationsErr != nil { - return &types.WrappedErrorMessage{Message: errorMessage, Err: locationsErr} - } + locations := state.Discovery.GetSecureLocationList() // Ask for the location in the callback state.FSM.GoTransitionWithData(fsm.ASK_LOCATION, locations, false) @@ -353,9 +343,10 @@ func (state *VPNState) SetSearchServer() error { return nil } -func (state *VPNState) getServerInfoData() string { - jsonString, _ := state.Servers.GetCurrentServerInfoJSON() - return jsonString +func (state *VPNState) getServerInfoData() *server.ServerInfoScreen { + info, _ := state.Servers.GetCurrentServerInfo() + // TODO: Log error + return info } func (state *VPNState) SetConnected() error { diff --git a/state_test.go b/state_test.go index 082a93c..688057b 100644 --- a/state_test.go +++ b/state_test.go @@ -57,9 +57,14 @@ func loginOAuthSelenium(t *testing.T, url string, state *VPNState) { } } -func stateCallback(t *testing.T, oldState VPNStateID, newState VPNStateID, data string, state *VPNState) { +func stateCallback(t *testing.T, oldState VPNStateID, newState VPNStateID, data interface{}, state *VPNState) { if newState == fsm.OAUTH_STARTED { - loginOAuthSelenium(t, data, state) + url, ok := data.(string) + + if !ok { + t.Fatalf("data is not a string for OAuth URL") + } + loginOAuthSelenium(t, url, state) } } @@ -68,7 +73,7 @@ func Test_server(t *testing.T) { state := &VPNState{} ensureLocalWellKnown() - state.Register("org.eduvpn.app.linux", "configstest", func(old VPNStateID, new VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configstest", func(old VPNStateID, new VPNStateID, data interface{}) { stateCallback(t, old, new, data, state) }, false) @@ -84,7 +89,7 @@ func test_connect_oauth_parameter(t *testing.T, parameters httpw.URLParameters, state := &VPNState{} configDirectory := "test_oauth_parameters" - state.Register("org.eduvpn.app.linux", configDirectory, func(oldState VPNStateID, newState VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", configDirectory, func(oldState VPNStateID, newState VPNStateID, data interface{}) { if newState == fsm.OAUTH_STARTED { baseURL := "http://127.0.0.1:8000/callback" url, err := httpw.HTTPConstructURL(baseURL, parameters) @@ -153,7 +158,7 @@ func Test_token_expired(t *testing.T) { // Get a vpn state state := &VPNState{} - state.Register("org.eduvpn.app.linux", "configsexpired", func(old VPNStateID, new VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configsexpired", func(old VPNStateID, new VPNStateID, data interface{}) { stateCallback(t, old, new, data, state) }, false) @@ -201,7 +206,7 @@ func Test_token_invalid(t *testing.T) { ensureLocalWellKnown() - state.Register("org.eduvpn.app.linux", "configsinvalid", func(old VPNStateID, new VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configsinvalid", func(old VPNStateID, new VPNStateID, data interface{}) { stateCallback(t, old, new, data, state) }, false) @@ -251,7 +256,7 @@ func Test_invalid_profile_corrected(t *testing.T) { ensureLocalWellKnown() - state.Register("org.eduvpn.app.linux", "configscancelprofile", func(old VPNStateID, new VPNStateID, data string) { + state.Register("org.eduvpn.app.linux", "configscancelprofile", func(old VPNStateID, new VPNStateID, data interface{}) { stateCallback(t, old, new, data, state) }, false) diff --git a/wrappers/python/tests.py b/wrappers/python/tests.py index 5cf5c25..60d3cce 100644 --- a/wrappers/python/tests.py +++ b/wrappers/python/tests.py @@ -6,6 +6,7 @@ from eduvpn_common.state import State, StateType import webbrowser import sys import os +import json # Import project root directory where the selenium python utility is sys.path.append( @@ -22,8 +23,8 @@ class ConfigTests(unittest.TestCase): _eduvpn.register() @_eduvpn.event.on(State.OAUTH_STARTED, StateType.Enter) - def oauth_initialized(old_state, url): - login_eduvpn(url) + def oauth_initialized(old_state, url_json): + login_eduvpn(json.loads(url_json)) server_uri = os.getenv("SERVER_URI") if not server_uri: |
