diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-03-20 13:59:06 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2023-09-25 09:43:37 +0200 |
| commit | 3618f2337bf0099d1fe8e4782cda3677ea4175be (patch) | |
| tree | 638fa68f28c20178a729ef991e470d8d8aa9ee64 | |
| parent | 8ccb4486cdb03cd3b10606b4bd77b7bcb4107e6d (diff) | |
Types: Split discovery into its own package
| -rw-r--r-- | client/client.go | 5 | ||||
| -rw-r--r-- | client/server.go | 5 | ||||
| -rw-r--r-- | internal/discovery/discovery.go | 26 | ||||
| -rw-r--r-- | internal/server/secureinternet.go | 6 | ||||
| -rw-r--r-- | internal/server/servers.go | 14 | ||||
| -rw-r--r-- | types/discovery/discovery.go | 65 | ||||
| -rw-r--r-- | types/types.go | 64 |
7 files changed, 94 insertions, 91 deletions
diff --git a/client/client.go b/client/client.go index c788cb2..78b451d 100644 --- a/client/client.go +++ b/client/client.go @@ -15,6 +15,7 @@ import ( "github.com/eduvpn/eduvpn-common/internal/oauth" "github.com/eduvpn/eduvpn-common/internal/server" "github.com/eduvpn/eduvpn-common/types" + discotypes "github.com/eduvpn/eduvpn-common/types/discovery" "github.com/eduvpn/eduvpn-common/types/protocol" "github.com/go-errors/errors" ) @@ -238,7 +239,7 @@ func (c *Client) askProfile(srv server.Server) error { // If the list cannot be retrieved an error is returned. // If this is the case then a previous version of the list is returned if there is any. // This takes into account the frequency of updates, see: https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md#organization-list. -func (c *Client) DiscoOrganizations() (orgs *types.DiscoveryOrganizations, err error) { +func (c *Client) DiscoOrganizations() (orgs *discotypes.Organizations, err error) { defer func() { if err != nil { c.logError(err) @@ -262,7 +263,7 @@ func (c *Client) DiscoOrganizations() (orgs *types.DiscoveryOrganizations, err e // If the list cannot be retrieved an error is returned. // If this is the case then a previous version of the list is returned if there is any. // This takes into account the frequency of updates, see: https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md#server-list. -func (c *Client) DiscoServers() (dss *types.DiscoveryServers, err error) { +func (c *Client) DiscoServers() (dss *discotypes.Servers, err error) { defer func() { if err != nil { c.logError(err) diff --git a/client/server.go b/client/server.go index a0de9f3..74caff1 100644 --- a/client/server.go +++ b/client/server.go @@ -9,6 +9,7 @@ import ( "github.com/eduvpn/eduvpn-common/internal/oauth" "github.com/eduvpn/eduvpn-common/internal/server" "github.com/eduvpn/eduvpn-common/types" + discotypes "github.com/eduvpn/eduvpn-common/types/discovery" "github.com/eduvpn/eduvpn-common/types/protocol" "github.com/go-errors/errors" ) @@ -265,7 +266,7 @@ func (c *Client) AddInstituteServer(url string) (err error) { // FIXME: Do nothing with discovery here as the client already has it // So pass a server as the parameter - var dSrv *types.DiscoveryServer + var dSrv *discotypes.Server dSrv, err = c.Discovery.ServerByURL(url, "institute_access") if err != nil { c.goBackInternal() @@ -378,7 +379,7 @@ func (c *Client) AddCustomServer(url string) (err error) { // Indicate that we're loading the server c.FSM.GoTransition(StateLoadingServer) - customServer := &types.DiscoveryServer{ + customServer := &discotypes.Server{ BaseURL: url, DisplayName: map[string]string{"en": url}, Type: "custom_server", diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 788bca2..ae7a307 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -8,7 +8,7 @@ import ( "github.com/eduvpn/eduvpn-common/internal/http" "github.com/eduvpn/eduvpn-common/internal/verify" - "github.com/eduvpn/eduvpn-common/types" + discotypes "github.com/eduvpn/eduvpn-common/types/discovery" "github.com/go-errors/errors" ) @@ -21,10 +21,10 @@ type Discovery struct { httpClient *http.Client // OrganizationList represents the organizations that are returned by the discovery server - OrganizationList types.DiscoveryOrganizations `json:"organizations"` + OrganizationList discotypes.Organizations `json:"organizations"` // ServerList represents the servers that are returned by the discovery server - ServerList types.DiscoveryServers `json:"servers"` + ServerList discotypes.Servers `json:"servers"` } var DiscoURL = "https://disco.eduvpn.org/v2/" @@ -115,7 +115,7 @@ func (discovery *Discovery) SecureLocationList() []string { func (discovery *Discovery) ServerByURL( baseURL string, srvType string, -) (*types.DiscoveryServer, error) { +) (*discotypes.Server, error) { for _, currentServer := range discovery.ServerList.List { if currentServer.BaseURL == baseURL && currentServer.Type == srvType { return ¤tServer, nil @@ -126,7 +126,7 @@ func (discovery *Discovery) ServerByURL( // ServerByCountryCode returns the discovery server by the country code // An error is returned if and only if nil is returned for the server. -func (discovery *Discovery) ServerByCountryCode(countryCode string) (*types.DiscoveryServer, error) { +func (discovery *Discovery) ServerByCountryCode(countryCode string) (*discotypes.Server, error) { for _, srv := range discovery.ServerList.List { if srv.CountryCode == countryCode && srv.Type == "secure_internet" { return &srv, nil @@ -137,7 +137,7 @@ func (discovery *Discovery) ServerByCountryCode(countryCode string) (*types.Disc // orgByID returns the discovery organization by the organization ID // An error is returned if and only if nil is returned for the organization. -func (discovery *Discovery) orgByID(orgID string) (*types.DiscoveryOrganization, error) { +func (discovery *Discovery) orgByID(orgID string) (*discotypes.Organization, error) { for _, org := range discovery.OrganizationList.List { if org.OrgID == orgID { return &org, nil @@ -150,7 +150,7 @@ func (discovery *Discovery) orgByID(orgID string) (*types.DiscoveryOrganization, // - The organization it belongs to // - The secure internet server itself // An error is returned if and only if nil is returned for the organization. -func (discovery *Discovery) SecureHomeArgs(orgID string) (*types.DiscoveryOrganization, *types.DiscoveryServer, error) { +func (discovery *Discovery) SecureHomeArgs(orgID string) (*discotypes.Organization, *discotypes.Server, error) { org, err := discovery.orgByID(orgID) if err != nil { return nil, nil, err @@ -178,7 +178,7 @@ func (discovery *Discovery) DetermineServersUpdate() bool { return !time.Now().Before(upd) } -func (discovery *Discovery) previousOrganizations() (*types.DiscoveryOrganizations, error) { +func (discovery *Discovery) previousOrganizations() (*discotypes.Organizations, error) { // If the version field is not zero then we have a cached struct // We also immediately return this copy if we have no embedded JSON if discovery.OrganizationList.Version != 0 || !HasCache { @@ -186,7 +186,7 @@ func (discovery *Discovery) previousOrganizations() (*types.DiscoveryOrganizatio } // We do not have a cached struct, this we need to get it using the embedded JSON - var eo types.DiscoveryOrganizations + var eo discotypes.Organizations if err := json.Unmarshal(eOrganizations, &eo); err != nil { return nil, errors.WrapPrefix(err, "failed parsing discovery organizations from the embedded cache", 0) } @@ -194,7 +194,7 @@ func (discovery *Discovery) previousOrganizations() (*types.DiscoveryOrganizatio return &eo, nil } -func (discovery *Discovery) previousServers() (*types.DiscoveryServers, error) { +func (discovery *Discovery) previousServers() (*discotypes.Servers, error) { // If the version field is not zero then we have a cached struct // We also immediately return this copy if we have no embedded JSON if discovery.ServerList.Version != 0 || !HasCache { @@ -202,7 +202,7 @@ func (discovery *Discovery) previousServers() (*types.DiscoveryServers, error) { } // We do not have a cached struct, this we need to get it using the embedded JSON - var es types.DiscoveryServers + var es discotypes.Servers if err := json.Unmarshal(eServers, &es); err != nil { return nil, errors.WrapPrefix(err, "failed parsing discovery servers from the embedded cache", 0) } @@ -212,7 +212,7 @@ func (discovery *Discovery) previousServers() (*types.DiscoveryServers, error) { // Organizations returns the discovery organizations // If there was an error, a cached copy is returned if available. -func (discovery *Discovery) Organizations() (*types.DiscoveryOrganizations, error) { +func (discovery *Discovery) Organizations() (*discotypes.Organizations, error) { if !discovery.DetermineOrganizationsUpdate() { return &discovery.OrganizationList, nil } @@ -230,7 +230,7 @@ func (discovery *Discovery) Organizations() (*types.DiscoveryOrganizations, erro // Servers returns the discovery servers // If there was an error, a cached copy is returned if available. -func (discovery *Discovery) Servers() (*types.DiscoveryServers, error) { +func (discovery *Discovery) Servers() (*discotypes.Servers, error) { if !discovery.DetermineServersUpdate() { return &discovery.ServerList, nil } diff --git a/internal/server/secureinternet.go b/internal/server/secureinternet.go index 3c40253..4b42303 100644 --- a/internal/server/secureinternet.go +++ b/internal/server/secureinternet.go @@ -4,7 +4,7 @@ import ( "github.com/eduvpn/eduvpn-common/internal/discovery" "github.com/eduvpn/eduvpn-common/internal/oauth" "github.com/eduvpn/eduvpn-common/internal/util" - "github.com/eduvpn/eduvpn-common/types" + discotypes "github.com/eduvpn/eduvpn-common/types/discovery" "github.com/go-errors/errors" ) @@ -82,7 +82,7 @@ func (ss *Servers) HasSecureLocation() bool { return ss.SecureInternetHomeServer.CurrentLocation != "" } -func (s *SecureInternetHomeServer) addLocation(locSrv *types.DiscoveryServer) (*Base, error) { +func (s *SecureInternetHomeServer) addLocation(locSrv *discotypes.Server) (*Base, error) { // Initialize the base map if it is non-nil if s.BaseMap == nil { s.BaseMap = make(map[string]*Base) @@ -109,7 +109,7 @@ func (s *SecureInternetHomeServer) addLocation(locSrv *types.DiscoveryServer) (* // Initializes the home server and adds its own location. func (s *SecureInternetHomeServer) init( - homeOrg *types.DiscoveryOrganization, homeLoc *types.DiscoveryServer, + homeOrg *discotypes.Organization, homeLoc *discotypes.Server, ) error { if s.HomeOrganizationID != homeOrg.OrgID { // New home organisation, clear everything diff --git a/internal/server/servers.go b/internal/server/servers.go index 9a9e413..60c993d 100644 --- a/internal/server/servers.go +++ b/internal/server/servers.go @@ -1,7 +1,7 @@ package server import ( - "github.com/eduvpn/eduvpn-common/types" + discotypes "github.com/eduvpn/eduvpn-common/types/discovery" "github.com/go-errors/errors" ) @@ -20,8 +20,8 @@ func (ss *Servers) HasSecureInternet() bool { } func (ss *Servers) AddSecureInternet( - secureOrg *types.DiscoveryOrganization, - secureServer *types.DiscoveryServer, + secureOrg *discotypes.Organization, + secureServer *discotypes.Server, ) (Server, error) { // If we have specified an organization ID // We also need to get an authorization template @@ -60,7 +60,7 @@ func (ss *Servers) GetCurrentServer() (Server, error) { } func (ss *Servers) addInstituteAndCustom( - discoServer *types.DiscoveryServer, + discoServer *discotypes.Server, isCustom bool, ) (Server, error) { URL := discoServer.BaseURL @@ -92,13 +92,13 @@ func (ss *Servers) addInstituteAndCustom( } func (ss *Servers) AddInstituteAccessServer( - instituteServer *types.DiscoveryServer, + instituteServer *discotypes.Server, ) (Server, error) { return ss.addInstituteAndCustom(instituteServer, false) } func (ss *Servers) AddCustomServer( - customServer *types.DiscoveryServer, + customServer *discotypes.Server, ) (Server, error) { return ss.addInstituteAndCustom(customServer, true) } @@ -108,7 +108,7 @@ func (ss *Servers) GetSecureLocation() string { } func (ss *Servers) SetSecureLocation( - chosenLocationServer *types.DiscoveryServer, + chosenLocationServer *discotypes.Server, ) error { // Make sure to add the current location diff --git a/types/discovery/discovery.go b/types/discovery/discovery.go new file mode 100644 index 0000000..390cb43 --- /dev/null +++ b/types/discovery/discovery.go @@ -0,0 +1,65 @@ +package discovery + +import ( + "encoding/json" + "time" +) + +// TODO: Discovery here is the same as the upstream discovery format, should we separate this as well? +// Defined in URL: "https://disco.eduvpn.org/v2/organization_list.json" +type Organizations struct { + Version uint64 `json:"v"` + List []Organization `json:"organization_list,omitempty"` + Timestamp time.Time `json:"go_timestamp"` +} + +type Organization struct { + DisplayName MapOrString `json:"display_name,omitempty"` + OrgID string `json:"org_id"` + SecureInternetHome string `json:"secure_internet_home,omitempty"` + KeywordList MapOrString `json:"keyword_list,omitempty"` +} + +// Structs that define the json format for +// url: "https://disco.eduvpn.org/v2/server_list.json" +type Servers struct { + Version uint64 `json:"v"` + List []Server `json:"server_list,omitempty"` + Timestamp time.Time `json:"go_timestamp"` +} + +type MapOrString map[string]string + +// The display name can either be a map or a string in the server list +// Unmarshal it by first trying a string and then the map. +func (displayName *MapOrString) UnmarshalJSON(data []byte) error { + var displayNameString string + + err := json.Unmarshal(data, &displayNameString) + + if err == nil { + *displayName = map[string]string{"en": displayNameString} + return nil + } + + var resultingMap map[string]string + + err = json.Unmarshal(data, &resultingMap) + + if err == nil { + *displayName = resultingMap + return nil + } + return err +} + +type Server struct { + AuthenticationURLTemplate string `json:"authentication_url_template"` + BaseURL string `json:"base_url"` + CountryCode string `json:"country_code"` + DisplayName MapOrString `json:"display_name,omitempty"` + KeywordList MapOrString `json:"keyword_list,omitempty"` + PublicKeyList []string `json:"public_key_list"` + Type string `json:"server_type"` + SupportContact []string `json:"support_contact"` +} diff --git a/types/types.go b/types/types.go index 4161108..bb04fdd 100644 --- a/types/types.go +++ b/types/types.go @@ -2,73 +2,9 @@ package types import ( - "encoding/json" - "time" - "github.com/eduvpn/eduvpn-common/types/protocol" ) -// TODO: Discovery here is the same as the upstream discovery format, should we separate this as well? -// Shared server types -// Structs that define the json format for -// url: "https://disco.eduvpn.org/v2/organization_list.json" -type DiscoveryOrganizations struct { - Version uint64 `json:"v"` - List []DiscoveryOrganization `json:"organization_list,omitempty"` - Timestamp time.Time `json:"go_timestamp"` -} - -type DiscoveryOrganization struct { - DisplayName DiscoMapOrString `json:"display_name,omitempty"` - OrgID string `json:"org_id"` - SecureInternetHome string `json:"secure_internet_home,omitempty"` - KeywordList DiscoMapOrString `json:"keyword_list,omitempty"` -} - -// Structs that define the json format for -// url: "https://disco.eduvpn.org/v2/server_list.json" -type DiscoveryServers struct { - Version uint64 `json:"v"` - List []DiscoveryServer `json:"server_list,omitempty"` - Timestamp time.Time `json:"go_timestamp"` -} - -type DiscoMapOrString map[string]string - -// The display name can either be a map or a string in the server list -// Unmarshal it by first trying a string and then the map. -func (displayName *DiscoMapOrString) UnmarshalJSON(data []byte) error { - var displayNameString string - - err := json.Unmarshal(data, &displayNameString) - - if err == nil { - *displayName = map[string]string{"en": displayNameString} - return nil - } - - var resultingMap map[string]string - - err = json.Unmarshal(data, &resultingMap) - - if err == nil { - *displayName = resultingMap - return nil - } - return err -} - -type DiscoveryServer struct { - AuthenticationURLTemplate string `json:"authentication_url_template"` - BaseURL string `json:"base_url"` - CountryCode string `json:"country_code"` - DisplayName DiscoMapOrString `json:"display_name,omitempty"` - KeywordList DiscoMapOrString `json:"keyword_list,omitempty"` - PublicKeyList []string `json:"public_key_list"` - Type string `json:"server_type"` - SupportContact []string `json:"support_contact"` -} - type Expiry struct { StartTime int64 `json:"start_time"` EndTime int64 `json:"end_time"` |
