summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-04-29 15:08:32 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-29 15:08:32 +0200
commit0e1f9826f2aea1a059529f9c3d1c921d7d4ac3d4 (patch)
tree2d26bd6dbd33abde910bff00078f520dad890a4d /internal
parent6c7a1c7a9245cf457a86fd15bdc14bc93b55d508 (diff)
Secure Internet: Basic implementation and add support to cli
Diffstat (limited to 'internal')
-rw-r--r--internal/discovery.go4
-rw-r--r--internal/fsm.go6
-rw-r--r--internal/oauth.go3
-rw-r--r--internal/server.go45
4 files changed, 46 insertions, 12 deletions
diff --git a/internal/discovery.go b/internal/discovery.go
index 8c0acc7..59281bd 100644
--- a/internal/discovery.go
+++ b/internal/discovery.go
@@ -57,8 +57,8 @@ type ServersList struct {
type Discovery struct {
Organizations OrganizationList
Servers ServersList
- FSM *FSM
- Logger *FileLogger
+ FSM *FSM
+ Logger *FileLogger
}
// Helper function that gets a disco json
diff --git a/internal/fsm.go b/internal/fsm.go
index 6997d92..1bcc479 100644
--- a/internal/fsm.go
+++ b/internal/fsm.go
@@ -4,8 +4,8 @@ import (
"fmt"
"os"
"os/exec"
- "sort"
"path"
+ "sort"
)
type (
@@ -94,7 +94,7 @@ type FSM struct {
Current FSMStateID
// Info to be passed from the parent state
- Name string
+ Name string
StateCallback func(string, string, string)
Logger *FileLogger
Directory string
@@ -107,7 +107,7 @@ func (fsm *FSM) Init(name string, callback func(string, string, string), logger
NO_SERVER: {{CHOSEN_SERVER, "User chooses a server"}},
CHOSEN_SERVER: {{AUTHORIZED, "Found tokens in config"}, {OAUTH_STARTED, "No tokens found in config"}},
OAUTH_STARTED: {{AUTHORIZED, "User authorizes with browser"}, {CHOSEN_SERVER, "Cancel OAuth"}},
- AUTHORIZED: {{OAUTH_STARTED, "Re-authorize with OAuth"}, {REQUEST_CONFIG, "Client requests a config"}},
+ AUTHORIZED: {{OAUTH_STARTED, "Re-authorize with OAuth"}, {REQUEST_CONFIG, "Client requests a config"}},
REQUEST_CONFIG: {{ASK_PROFILE, "Multiple profiles found"}, {HAS_CONFIG, "Success, only one profile"}},
ASK_PROFILE: {{HAS_CONFIG, "User chooses profile and success"}},
HAS_CONFIG: {{CONNECTED, "OS reports connected"}},
diff --git a/internal/oauth.go b/internal/oauth.go
index 9d17777..98af5a4 100644
--- a/internal/oauth.go
+++ b/internal/oauth.go
@@ -326,8 +326,7 @@ func (oauth *OAuth) NeedsRelogin() bool {
return true
}
-type OAuthCancelledCallbackError struct {
-}
+type OAuthCancelledCallbackError struct{}
func (e *OAuthCancelledCallbackError) Error() string {
return fmt.Sprintf("Client cancelled OAuth")
diff --git a/internal/server.go b/internal/server.go
index aa21a97..1d6f1e1 100644
--- a/internal/server.go
+++ b/internal/server.go
@@ -17,8 +17,9 @@ type Server struct {
}
type Servers struct {
- List map[string]*Server `json:"list"`
- Current string `json:"current"`
+ List map[string]*Server `json:"list"`
+ Current string `json:"current"`
+ SecureHome string `json:"secure_home"`
}
func (servers *Servers) GetCurrentServer() (*Server, error) {
@@ -57,7 +58,10 @@ func (server *Server) EnsureTokens() error {
return nil
}
-func (servers *Servers) EnsureServer(url string, fsm *FSM, logger *FileLogger) (*Server, error) {
+func (servers *Servers) EnsureServer(url string, fsm *FSM, logger *FileLogger, makeCurrent bool) (*Server, error) {
+ if url == "" {
+ return nil, errors.New("Emtpy URL to ensure Server")
+ }
if servers.List == nil {
servers.List = make(map[string]*Server)
}
@@ -73,10 +77,41 @@ func (servers *Servers) EnsureServer(url string, fsm *FSM, logger *FileLogger) (
return nil, serverInitErr
}
servers.List[url] = server
- servers.Current = url
+
+ if makeCurrent {
+ servers.Current = url
+ }
+ return server, nil
+}
+
+func (servers *Servers) getSecureInternetHome() (*Server, error) {
+ server, exists := servers.List[servers.SecureHome]
+
+ if !exists || server == nil {
+ return nil, errors.New("No secure internet home found")
+ }
+
return server, nil
}
+func (servers *Servers) EnsureSecureHome(server *Server) {
+ if servers.SecureHome == "" {
+ servers.SecureHome = server.BaseURL
+ }
+}
+
+func (servers *Servers) CopySecureInternetOAuth(server *Server) error {
+ secureHome, secureHomeErr := servers.getSecureInternetHome()
+
+ if secureHomeErr != nil {
+ return secureHomeErr
+ }
+
+ // Forward token properties
+ server.OAuth = secureHome.OAuth
+ return nil
+}
+
type ServerProfile struct {
ID string `json:"profile_id"`
DisplayName string `json:"display_name"`
@@ -151,7 +186,7 @@ func (server *Server) getCurrentProfile() (*ServerProfile, error) {
return &profile, nil
}
}
- return nil, errors.New("no profile found for id")
+ return nil, errors.New(fmt.Sprintf("no profile found for id %s", profile_id))
}
func (server *Server) getConfigWithProfile() (string, error) {