1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package eduvpn
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type endpointList struct {
API string `json:"api_endpoint"`
Authorization string `json:"authorization_endpoint"`
Token string `json:"token_endpoint"`
}
// Struct that defines the json format for /.well-known/vpn-user-portal"
type EduVPNEndpoints 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(vpnState *EduVPNState) (*EduVPNEndpoints, error) {
url := vpnState.Server + "/.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 := &EduVPNEndpoints{}
jsonErr := json.Unmarshal(body, &structure)
if jsonErr != nil {
return nil, jsonErr
}
return structure, nil
}
func APIAuthenticatedInfo(vpnState *EduVPNState) (string, error) {
url := vpnState.Endpoints.API.V3.API + "/info"
client := &http.Client{}
req, reqErr := http.NewRequest(http.MethodGet, url, nil)
if reqErr != nil {
return "", reqErr
}
req.Header.Add("Authorization", "Bearer "+vpnState.OAuthToken.Access)
resp, reqErr := client.Do(req)
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
}
|