summaryrefslogtreecommitdiff
path: root/src/api.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-03-07 15:43:07 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-05 12:26:13 +0200
commit56548c511163b4dd22d9a96a2f5ae647f1627a7b (patch)
tree20ebfa8641840cd09e026a960e1eca6b60976381 /src/api.go
parentb2228bda5528ad69d0d915e4dc9a15e2291818c8 (diff)
Refactor: Simplify API by using a state as context
Diffstat (limited to 'src/api.go')
-rw-r--r--src/api.go30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/api.go b/src/api.go
index 9e082cc..8bd2847 100644
--- a/src/api.go
+++ b/src/api.go
@@ -2,6 +2,7 @@ package eduvpn
import (
"encoding/json"
+ "errors"
"io/ioutil"
"net/http"
)
@@ -13,7 +14,7 @@ type endpointList struct {
}
// Struct that defines the json format for /.well-known/vpn-user-portal"
-type PortalEndpoints struct {
+type EduVPNEndpoints struct {
API struct {
V2 endpointList `json:"http://eduvpn.org/api#2"`
V3 endpointList `json:"http://eduvpn.org/api#3"`
@@ -21,8 +22,8 @@ type PortalEndpoints struct {
V string `json:"v"`
}
-func APIGetEndpoints(baseURL string) (*PortalEndpoints, error) {
- url := baseURL + "/.well-known/vpn-user-portal"
+func APIGetEndpoints(vpnState *EduVPNState) (*EduVPNEndpoints, error) {
+ url := vpnState.Server + "/.well-known/vpn-user-portal"
resp, reqErr := http.Get(url)
if reqErr != nil {
return nil, reqErr
@@ -40,7 +41,7 @@ func APIGetEndpoints(baseURL string) (*PortalEndpoints, error) {
return nil, readErr
}
- structure := &PortalEndpoints{}
+ structure := &EduVPNEndpoints{}
jsonErr := json.Unmarshal(body, &structure)
if jsonErr != nil {
@@ -49,3 +50,24 @@ func APIGetEndpoints(baseURL string) (*PortalEndpoints, error) {
return structure, nil
}
+
+func APIAuthenticatedInfo(vpnState *EduVPNState) (string, error) {
+ url := vpnState.Endpoints.API.V3.Endpoint + "/info"
+ resp, reqErr := vpnState.OAuth.client.Get(url)
+ if reqErr != nil {
+ return "", reqErr
+ }
+ // Close the response body at the end
+ defer resp.Body.Close()
+
+ // Check if http response code is ok
+ if resp.StatusCode != http.StatusOK {
+ return "", errors.New("HTTP code not ok")
+ }
+ // Read the body
+ body, readErr := ioutil.ReadAll(resp.Body)
+ if readErr != nil {
+ return "", readErr
+ }
+ return string(body), nil
+}