summaryrefslogtreecommitdiff
path: root/cli/main.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-03-07 12:55:06 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-05 12:26:13 +0200
commitb2228bda5528ad69d0d915e4dc9a15e2291818c8 (patch)
treed5fe5a4ff1320ac950344cf725574750b99484e6 /cli/main.go
parentc0f9daee7f1054bb9df63af7a953d4e84dcb01f6 (diff)
Add a cli that returns the authenticated /info
Diffstat (limited to 'cli/main.go')
-rw-r--r--cli/main.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/cli/main.go b/cli/main.go
new file mode 100644
index 0000000..fe7dc1d
--- /dev/null
+++ b/cli/main.go
@@ -0,0 +1,94 @@
+package main
+
+import (
+ "flag"
+ eduvpn "github.com/jwijenbergh/eduvpn-common/src"
+ "golang.org/x/oauth2"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "os/exec"
+ "strings"
+)
+
+func openBrowser(urlString string) {
+ log.Printf("OAuth: Initialized with AuthURL %s\n", urlString)
+ log.Println("OAuth: Opening browser with xdg-open...")
+ exec.Command("xdg-open", urlString).Start()
+}
+
+func constructConfig(urlString string) (*oauth2.Config, string) {
+ // Get the endpoints
+ endpoints, err := eduvpn.APIGetEndpoints(urlString)
+ if err != nil {
+ log.Fatal("Error API: cannot get endpoints. Message: ", err)
+ }
+ log.Printf("API: Got endpoints:\n- V3 API %s\n- V3 Authorization %s\n- V3 Token %s\n", endpoints.API.V3.Endpoint, endpoints.API.V3.AuthorizationEndpoint, endpoints.API.V3.TokenEndpoint)
+
+ // Start the OAuth procedure
+ config := &oauth2.Config{
+ RedirectURL: "http://127.0.0.1:8000/callback",
+ ClientID: "org.eduvpn.app.linux",
+ Scopes: []string{"config"},
+ Endpoint: oauth2.Endpoint{
+ AuthURL: endpoints.API.V3.AuthorizationEndpoint,
+ TokenURL: endpoints.API.V3.TokenEndpoint,
+ },
+ }
+ return config, endpoints.API.V3.Endpoint
+}
+
+func auth(urlString string) (*http.Client, string) {
+ // Get the config
+ oauthConfig, apiString := constructConfig(urlString)
+
+ // Initialize oauth with the config
+ eduOAuth, err := eduvpn.InitializeOAuth(oauthConfig)
+ if err != nil {
+ log.Fatal("Error OAuth: cannot initialize OAuth. Message: ", err)
+ }
+
+ // Open the browser
+ openBrowser(eduOAuth.AuthURL)
+
+ // Get and return authenticated client
+ client, err := eduOAuth.GetHTTPTokenClient()
+ if err != nil {
+ log.Fatal("Error OAUth: cannot get authenticated HTTP client. Message: ", err)
+ }
+ return client, apiString
+}
+
+func show_info(client *http.Client, apiString string) {
+ log.Println("OAUth: Got authenticated HTTP client")
+
+ resp, err := client.Get(apiString + "/info")
+ if err != nil {
+ panic(err)
+ }
+ defer resp.Body.Close()
+
+ bodyBytes, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ panic(err)
+ }
+ log.Println(string(bodyBytes))
+}
+
+func main() {
+ urlArg := flag.String("url", "", "The url of the vpn")
+ flag.Parse()
+
+ urlString := *urlArg
+
+ if urlString == "" {
+ log.Fatal("Error: -url is required")
+ }
+
+ if !strings.HasPrefix(urlString, "https://") {
+ urlString = "https://" + urlString
+ }
+
+ client, apiString := auth(urlString)
+ show_info(client, apiString)
+}