summaryrefslogtreecommitdiff
path: root/src/api.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-03-21 14:58:58 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-20 20:29:52 +0200
commitfc56f8770923ec1997444a8318a18be0a8397520 (patch)
tree3c6522b9b6e44ca2ad6cd94b074da78eed2c1028 /src/api.go
parentd45f5df4dc5fa9ad8abdc47c940f6baf96fdbe45 (diff)
Wireguard: Add basic support
Diffstat (limited to 'src/api.go')
-rw-r--r--src/api.go42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/api.go b/src/api.go
index d485728..dae9457 100644
--- a/src/api.go
+++ b/src/api.go
@@ -1,23 +1,57 @@
package eduvpn
import (
+ "fmt"
"net/http"
+ "net/url"
)
-func (eduvpn *VPNState) APIAuthenticatedGet(endpoint string) (string, error) {
+// Authenticated wrappers on top of HTTP
+func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, opts *HTTPOptionalParams) ([]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
// Ensure we have non-expired tokens
oauthErr := eduvpn.EnsureTokensOAuth()
if oauthErr != nil {
- return "", oauthErr
+ return nil, oauthErr
}
- headers := &http.Header{"Authorization": {"Bearer " + eduvpn.Server.OAuth.Token.Access}}
- body, bodyErr := HTTPGetWithOptionalParams(url, &HTTPOptionalParams{Headers: headers})
+ headerKey := "Authorization"
+ headerValue := fmt.Sprintf("Bearer %s", eduvpn.Server.OAuth.Token.Access)
+ if opts.Headers != nil {
+ opts.Headers.Add(headerKey, headerValue)
+ } else {
+ opts.Headers = &http.Header{headerKey: {headerValue}}
+ }
+ body, bodyErr := HTTPMethodWithOpts(method, url, opts)
+ if bodyErr != nil {
+ return nil, bodyErr
+ }
+ return body, nil
+}
+
+func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, error) {
+ headers := &http.Header{
+ "content-type": {"application/x-www-form-urlencoded"},
+ "accept": {"application/x-wireguard-profile"},
+ }
+
+ urlForm := url.Values{
+ "profile_id": {"default"},
+ "public_key": {pubkey},
+ }
+ body, bodyErr := eduvpn.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm})
if bodyErr != nil {
return "", bodyErr
}
return string(body), nil
}
+
+func (eduvpn *VPNState) APIInfo() ([]byte, error) {
+ return eduvpn.apiAuthenticatedWithOpts(http.MethodGet, "/info", nil)
+}