summaryrefslogtreecommitdiff
path: root/internal/server
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-12-20 15:43:55 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-12-21 18:28:50 +0100
commit12838c19514459974cf0a71c42f1248b1cb9419c (patch)
treea4254d20bb7b0ef49a2fa6c12753eb4c5acb64d1 /internal/server
parent6981666c6d8f639a1ff9c09a3bc08769e19928af (diff)
Exports + OAuth + Server: Forward tokens to getting a config
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/server.go53
1 files changed, 41 insertions, 12 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 78f6472..1585264 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -77,6 +77,10 @@ func ShouldRenewButton(srv Server) bool {
return true
}
+func UpdateTokens(srv Server, t oauth.Token) {
+ srv.OAuth().UpdateTokens(t)
+}
+
func OAuthURL(srv Server, name string) (string, error) {
return srv.OAuth().AuthURL(name, srv.TemplateAuth())
}
@@ -134,22 +138,33 @@ func ValidProfiles(srv Server, wireguardSupport bool) (*ProfileInfo, error) {
return &ps, nil
}
-func wireguardGetConfig(srv Server, preferTCP bool, openVPNSupport bool) (string, string, error) {
+type ConfigData struct {
+ // The configuration
+ Config string
+
+ // The type of configuration
+ Type string
+
+ // The tokens
+ Tokens oauth.Token
+}
+
+func wireguardGetConfig(srv Server, preferTCP bool, openVPNSupport bool) (*ConfigData, error) {
b, err := srv.Base()
if err != nil {
- return "", "", err
+ return nil, err
}
pID := b.Profiles.Current
key, err := wireguard.GenerateKey()
if err != nil {
- return "", "", err
+ return nil, err
}
pub := key.PublicKey().String()
cfg, proto, exp, err := APIConnectWireguard(srv, pID, pub, preferTCP, openVPNSupport)
if err != nil {
- return "", "", err
+ return nil, err
}
// Store start and end time
@@ -164,25 +179,39 @@ func wireguardGetConfig(srv Server, preferTCP bool, openVPNSupport bool) (string
cfg = wireguard.ConfigAddKey(cfg, key)
}
- return cfg, proto, nil
+ t := oauth.Token{}
+
+ o := srv.OAuth()
+ if o != nil {
+ t = o.Token()
+ }
+
+ return &ConfigData{Config: cfg, Type: proto, Tokens: t}, nil
}
-func openVPNGetConfig(srv Server, preferTCP bool) (string, string, error) {
+func openVPNGetConfig(srv Server, preferTCP bool) (*ConfigData, error) {
b, err := srv.Base()
if err != nil {
- return "", "", err
+ return nil, err
}
pid := b.Profiles.Current
cfg, exp, err := APIConnectOpenVPN(srv, pid, preferTCP)
if err != nil {
- return "", "", err
+ return nil, err
}
// Store start and end time
b.StartTime = time.Now()
b.EndTime = exp
- return cfg, "openvpn", nil
+ t := oauth.Token{}
+
+ o := srv.OAuth()
+ if o != nil {
+ t = o.Token()
+ }
+
+ return &ConfigData{Config: cfg, Type: "openvpn", Tokens: t}, nil
}
func HasValidProfile(srv Server, wireguardSupport bool) (bool, error) {
@@ -237,10 +266,10 @@ func RefreshEndpoints(srv Server) error {
return b.InitializeEndpoints()
}
-func Config(server Server, wireguardSupport bool, preferTCP bool) (string, string, error) {
+func Config(server Server, wireguardSupport bool, preferTCP bool) (*ConfigData, error) {
p, err := CurrentProfile(server)
if err != nil {
- return "", "", err
+ return nil, err
}
ovpn := p.SupportsOpenVPN()
@@ -266,7 +295,7 @@ func Config(server Server, wireguardSupport bool, preferTCP bool) (string, strin
return openVPNGetConfig(server, preferTCP)
// The config supports no available protocol because the profile only supports WireGuard but the client doesn't
default:
- return "", "", errors.Errorf("no supported protocol found")
+ return nil, errors.Errorf("no supported protocol found")
}
}