diff options
| author | Jeroen Wijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-07 12:54:26 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-04-05 12:26:13 +0200 |
| commit | c0f9daee7f1054bb9df63af7a953d4e84dcb01f6 (patch) | |
| tree | 0c01db18707ca5541d145110b41cb76947cadb92 /src/api.go | |
| parent | d356e7e4389ff62a14421cd62ee6d0b1b36fcbe1 (diff) | |
Add a file for the vpn API
Diffstat (limited to 'src/api.go')
| -rw-r--r-- | src/api.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/api.go b/src/api.go new file mode 100644 index 0000000..9e082cc --- /dev/null +++ b/src/api.go @@ -0,0 +1,51 @@ +package eduvpn + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +type endpointList struct { + Endpoint string `json:"api_endpoint"` + AuthorizationEndpoint string `json:"authorization_endpoint"` + TokenEndpoint string `json:"token_endpoint"` +} + +// Struct that defines the json format for /.well-known/vpn-user-portal" +type PortalEndpoints struct { + API struct { + V2 endpointList `json:"http://eduvpn.org/api#2"` + V3 endpointList `json:"http://eduvpn.org/api#3"` + } `json:"api"` + V string `json:"v"` +} + +func APIGetEndpoints(baseURL string) (*PortalEndpoints, error) { + url := baseURL + "/.well-known/vpn-user-portal" + resp, reqErr := http.Get(url) + if reqErr != nil { + return nil, reqErr + } + // Close the response body at the end + defer resp.Body.Close() + + // Check if http response code is ok + if resp.StatusCode != http.StatusOK { + panic("http code not ok") + } + // Read the body + body, readErr := ioutil.ReadAll(resp.Body) + if readErr != nil { + return nil, readErr + } + + structure := &PortalEndpoints{} + jsonErr := json.Unmarshal(body, &structure) + + if jsonErr != nil { + return nil, jsonErr + } + + return structure, nil +} |
