summaryrefslogtreecommitdiff
path: root/src/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.go')
-rw-r--r--src/api.go56
1 files changed, 47 insertions, 9 deletions
diff --git a/src/api.go b/src/api.go
index 5e11afd..cc84989 100644
--- a/src/api.go
+++ b/src/api.go
@@ -2,27 +2,28 @@ package eduvpn
import (
"fmt"
+ "encoding/json"
"net/http"
"net/url"
)
// Authenticated wrappers on top of HTTP
-func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) {
+func (server *Server) apiAuthenticatedWithOpts(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) {
// Ensure optional is not nil as we will fill it with headers
if opts == nil {
opts = &HTTPOptionalParams{}
}
- url := eduvpn.Server.Endpoints.API.V3.API + endpoint
+ url := server.Endpoints.API.V3.API + endpoint
// Ensure we have non-expired tokens
- oauthErr := eduvpn.EnsureTokensOAuth()
+ oauthErr := server.OAuth.EnsureTokens()
if oauthErr != nil {
return nil, nil, oauthErr
}
headerKey := "Authorization"
- headerValue := fmt.Sprintf("Bearer %s", eduvpn.Server.OAuth.Token.Access)
+ headerValue := fmt.Sprintf("Bearer %s", server.OAuth.Token.Access)
if opts.Headers != nil {
opts.Headers.Add(headerKey, headerValue)
} else {
@@ -35,7 +36,26 @@ func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string,
return header, body, nil
}
-func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, string, error) {
+func (server *Server) APIInfo() error {
+ _, body, bodyErr := server.apiAuthenticatedWithOpts(http.MethodGet, "/info", nil)
+ if bodyErr != nil {
+ return bodyErr
+ }
+ structure := &ServerProfileInfo{}
+ jsonErr := json.Unmarshal(body, structure)
+
+ if jsonErr != nil {
+ return jsonErr
+ }
+
+ server.Profiles = structure
+
+ // FIXME: Implement profile selection callback
+ server.Profiles.Current = 0
+ return nil
+}
+
+func (server *Server) APIConnectWireguard(profile_id string, pubkey string) (string, string, error) {
headers := &http.Header{
"content-type": {"application/x-www-form-urlencoded"},
"accept": {"application/x-wireguard-profile"},
@@ -45,11 +65,29 @@ func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, string, erro
"profile_id": {"default"},
"public_key": {pubkey},
}
- header, body, bodyErr := eduvpn.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm})
- if bodyErr != nil {
- return "", "", bodyErr
+ header, connectBody, connectErr := server.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm})
+ if connectErr != nil {
+ return "", "", connectErr
+ }
+
+ expires := header.Get("expires")
+ return string(connectBody), expires, nil
+}
+
+func (server *Server) APIConnectOpenVPN(profile_id string) (string, string, error) {
+ headers := &http.Header{
+ "content-type": {"application/x-www-form-urlencoded"},
+ "accept": {"application/x-openvpn-profile"},
+ }
+
+ urlForm := url.Values{
+ "profile_id": {"default"},
+ }
+ header, connectBody, connectErr := server.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm})
+ if connectErr != nil {
+ return "", "", connectErr
}
expires := header.Get("expires")
- return string(body), expires, nil
+ return string(connectBody), expires, nil
}