From 324202a50e440cac36a756d6a2628ebadd2f8d70 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Mon, 21 Mar 2022 17:16:28 +0100 Subject: Update python and add basic config support --- src/config.go | 34 ++++++++++++++++++++++++++++++++++ src/oauth.go | 8 ++++---- src/server.go | 6 +++--- src/state.go | 10 ++++++---- 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/config.go (limited to 'src') diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..0b7c1c7 --- /dev/null +++ b/src/config.go @@ -0,0 +1,34 @@ +package eduvpn + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path" +) + +func (eduvpn *VPNState) GetConfigName() string { + pathString := path.Join(eduvpn.ConfigDirectory, eduvpn.Name) + return fmt.Sprintf("%s.json", pathString) +} + +func (eduvpn *VPNState) WriteConfig() error { + mkdirErr := os.MkdirAll(eduvpn.ConfigDirectory, os.ModePerm) + if mkdirErr != nil { + return mkdirErr + } + jsonString, marshalErr := json.Marshal(eduvpn) + if marshalErr != nil { + return marshalErr + } + return ioutil.WriteFile(eduvpn.GetConfigName(), jsonString, 0644) +} + +func (eduvpn *VPNState) LoadConfig() error { + bytes, readErr := ioutil.ReadFile(eduvpn.GetConfigName()) + if readErr != nil { + return readErr + } + return json.Unmarshal(bytes, eduvpn) +} diff --git a/src/oauth.go b/src/oauth.go index 063034b..eb93c00 100644 --- a/src/oauth.go +++ b/src/oauth.go @@ -54,9 +54,9 @@ func genVerifier() (string, error) { } type OAuth struct { - Session *OAuthExchangeSession - Token *OAuthToken - TokenURL string + Session *OAuthExchangeSession `json:"-"` + Token *OAuthToken `json:"token"` + TokenURL string `json:"token_url"` } // This structure gets passed to the callback for easy access to the current state @@ -85,7 +85,7 @@ type OAuthToken struct { Refresh string `json:"refresh_token"` Type string `json:"token_type"` Expires int64 `json:"expires_in"` - ExpiredTimestamp int64 + ExpiredTimestamp int64 `json:"expires_in_timestamp"` } // Gets an authenticated HTTP client by obtaining refresh and access tokens diff --git a/src/server.go b/src/server.go index 6f809c6..627843f 100644 --- a/src/server.go +++ b/src/server.go @@ -5,9 +5,9 @@ import ( ) type Server struct { - BaseURL string - Endpoints *ServerEndpoints - OAuth *OAuth + BaseURL string `json:"base_url"` + Endpoints *ServerEndpoints `json:"endpoints"` + OAuth *OAuth `json:"oauth"` } type ServerEndpointList struct { diff --git a/src/state.go b/src/state.go index 582dd5a..6f06860 100644 --- a/src/state.go +++ b/src/state.go @@ -2,16 +2,18 @@ package eduvpn type VPNState struct { // Info passed by the client - Name string + ConfigDirectory string `json:"-"` + Name string `json:"-"` // The chosen server - Server *Server + Server *Server `json:"server"` } -func Register(state *VPNState, name string, stateCallback func(string, string)) error { +func Register(state *VPNState, name string, directory string, stateCallback func(string, string, string)) error { state.Name = name + state.ConfigDirectory = directory - stateCallback("START", "REGISTER") + stateCallback("START", "REGISTERED", "test data") return nil } -- cgit v1.2.3