summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-05 17:11:37 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-02-19 14:15:07 +0100
commit18b985ac49b433cd15a1182719692c4be2584101 (patch)
treea414ae99349c1cc8e8db2adbc762c7f5129ad01e
parent06904ad6c8de153b9174f178aaa226c3b8ceb622 (diff)
Client: Split client id and discovery functions
-rw-r--r--client/client.go72
-rw-r--r--client/discovery.go48
-rw-r--r--client/id.go68
3 files changed, 116 insertions, 72 deletions
diff --git a/client/client.go b/client/client.go
index 2901ca9..4550ef0 100644
--- a/client/client.go
+++ b/client/client.go
@@ -24,78 +24,6 @@ import (
"github.com/go-errors/errors"
)
-// isAllowedClientID checks if the 'clientID' is in the list of allowed client IDs
-func isAllowedClientID(clientID string) bool {
- allowList := []string{
- // eduVPN
- "org.eduvpn.app.windows",
- "org.eduvpn.app.android",
- "org.eduvpn.app.ios",
- "org.eduvpn.app.macos",
- "org.eduvpn.app.linux",
- // Let's Connect!
- "org.letsconnect-vpn.app.windows",
- "org.letsconnect-vpn.app.android",
- "org.letsconnect-vpn.app.ios",
- "org.letsconnect-vpn.app.macos",
- "org.letsconnect-vpn.app.linux",
- // govVPN
- "org.govvpn.app.windows",
- "org.govvpn.app.android",
- "org.govvpn.app.ios",
- "org.govvpn.app.macos",
- "org.govvpn.app.linux",
- }
- for _, x := range allowList {
- if x == clientID {
- return true
- }
- }
- return false
-}
-
-func userAgentName(clientID string) string {
- switch clientID {
- case "org.eduvpn.app.windows":
- return "eduVPN for Windows"
- case "org.eduvpn.app.android":
- return "eduVPN for Android"
- case "org.eduvpn.app.ios":
- return "eduVPN for iOS"
- case "org.eduvpn.app.macos":
- return "eduVPN for macOS"
- case "org.eduvpn.app.linux":
- return "eduVPN for Linux"
- case "org.letsconnect-vpn.app.windows":
- return "Let's Connect! for Windows"
- case "org.letsconnect-vpn.app.android":
- return "Let's Connect! for Android"
- case "org.letsconnect-vpn.app.ios":
- return "Let's Connect! for iOS"
- case "org.letsconnect-vpn.app.macos":
- return "Let's Connect! for macOS"
- case "org.letsconnect-vpn.app.linux":
- return "Let's Connect! for Linux"
- case "org.govvpn.app.windows":
- return "govVPN for Windows"
- case "org.govvpn.app.android":
- return "govVPN for Android"
- case "org.govvpn.app.ios":
- return "govVPN for iOS"
- case "org.govvpn.app.macos":
- return "govVPN for macOS"
- case "org.govvpn.app.linux":
- return "govVPN for Linux"
- default:
- return "unknown"
- }
-}
-
-func (c *Client) hasDiscovery() bool {
- // see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php
- return strings.HasPrefix(c.Name, "org.eduvpn.app")
-}
-
// Client is the main struct for the VPN client.
type Client struct {
// The name of the client
diff --git a/client/discovery.go b/client/discovery.go
new file mode 100644
index 0000000..ccd8f0a
--- /dev/null
+++ b/client/discovery.go
@@ -0,0 +1,48 @@
+package client
+
+import (
+ "strings"
+
+ "github.com/eduvpn/eduvpn-common/i18nerr"
+ "github.com/eduvpn/eduvpn-common/types/cookie"
+ discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
+)
+
+func (c *Client) hasDiscovery() bool {
+ // see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php
+ return strings.HasPrefix(c.Name, "org.eduvpn.app")
+}
+
+// DiscoOrganizations gets the organizations list from the discovery server
+// 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(ck *cookie.Cookie) (orgs *discotypes.Organizations, err error) {
+ // Not supported with Let's Connect! & govVPN
+ if !c.hasDiscovery() {
+ return nil, i18nerr.NewInternal("Server/organization discovery with this client ID is not supported")
+ }
+
+ orgs, err = c.cfg.Discovery().Organizations(ck.Context())
+ if err != nil {
+ err = i18nerr.Wrap(err, "An error occurred after getting the discovery files for the list of organizations")
+ }
+ return
+}
+
+// DiscoServers gets the servers list from the discovery server
+// 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(ck *cookie.Cookie) (dss *discotypes.Servers, err error) {
+ // Not supported with Let's Connect! & govVPN
+ if !c.hasDiscovery() {
+ return nil, i18nerr.NewInternal("Server/organization discovery with this client ID is not supported")
+ }
+
+ dss, err = c.cfg.Discovery().Servers(ck.Context())
+ if err != nil {
+ err = i18nerr.Wrap(err, "An error occurred after getting the discovery files for the list of servers")
+ }
+ return
+}
diff --git a/client/id.go b/client/id.go
new file mode 100644
index 0000000..b4e1670
--- /dev/null
+++ b/client/id.go
@@ -0,0 +1,68 @@
+package client
+
+// isAllowedClientID checks if the 'clientID' is in the list of allowed client IDs
+func isAllowedClientID(clientID string) bool {
+ allowList := []string{
+ // eduVPN
+ "org.eduvpn.app.windows",
+ "org.eduvpn.app.android",
+ "org.eduvpn.app.ios",
+ "org.eduvpn.app.macos",
+ "org.eduvpn.app.linux",
+ // Let's Connect!
+ "org.letsconnect-vpn.app.windows",
+ "org.letsconnect-vpn.app.android",
+ "org.letsconnect-vpn.app.ios",
+ "org.letsconnect-vpn.app.macos",
+ "org.letsconnect-vpn.app.linux",
+ // govVPN
+ "org.govvpn.app.windows",
+ "org.govvpn.app.android",
+ "org.govvpn.app.ios",
+ "org.govvpn.app.macos",
+ "org.govvpn.app.linux",
+ }
+ for _, x := range allowList {
+ if x == clientID {
+ return true
+ }
+ }
+ return false
+}
+
+func userAgentName(clientID string) string {
+ switch clientID {
+ case "org.eduvpn.app.windows":
+ return "eduVPN for Windows"
+ case "org.eduvpn.app.android":
+ return "eduVPN for Android"
+ case "org.eduvpn.app.ios":
+ return "eduVPN for iOS"
+ case "org.eduvpn.app.macos":
+ return "eduVPN for macOS"
+ case "org.eduvpn.app.linux":
+ return "eduVPN for Linux"
+ case "org.letsconnect-vpn.app.windows":
+ return "Let's Connect! for Windows"
+ case "org.letsconnect-vpn.app.android":
+ return "Let's Connect! for Android"
+ case "org.letsconnect-vpn.app.ios":
+ return "Let's Connect! for iOS"
+ case "org.letsconnect-vpn.app.macos":
+ return "Let's Connect! for macOS"
+ case "org.letsconnect-vpn.app.linux":
+ return "Let's Connect! for Linux"
+ case "org.govvpn.app.windows":
+ return "govVPN for Windows"
+ case "org.govvpn.app.android":
+ return "govVPN for Android"
+ case "org.govvpn.app.ios":
+ return "govVPN for iOS"
+ case "org.govvpn.app.macos":
+ return "govVPN for macOS"
+ case "org.govvpn.app.linux":
+ return "govVPN for Linux"
+ default:
+ return "unknown"
+ }
+}