diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api.go | 22 | ||||
| -rw-r--r-- | internal/fsm.go | 16 | ||||
| -rw-r--r-- | internal/oauth.go | 6 |
3 files changed, 22 insertions, 22 deletions
diff --git a/internal/api.go b/internal/api.go index 718702b..2ed605e 100644 --- a/internal/api.go +++ b/internal/api.go @@ -8,8 +8,8 @@ import ( "net/url" ) -// Authenticated wrappers on top of HTTP -func (server *Server) apiAuthenticated(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) { +// Authorized wrappers on top of HTTP +func (server *Server) apiAuthorized(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{} @@ -33,17 +33,17 @@ func (server *Server) apiAuthenticated(method string, endpoint string, opts *HTT return HTTPMethodWithOpts(method, url, opts) } -func (server *Server) apiAuthenticatedRetry(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) { - header, body, bodyErr := server.apiAuthenticated(method, endpoint, opts) +func (server *Server) apiAuthorizedRetry(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) { + header, body, bodyErr := server.apiAuthorized(method, endpoint, opts) if bodyErr != nil { var error *HTTPStatusError - // Only retry authenticated if we get a HTTP 401 + // Only retry authroized if we get a HTTP 401 if errors.As(bodyErr, &error) && error.Status == 401 { - server.Logger.Log(LOG_INFO, fmt.Sprintf("API: Got HTTP error %v, retrying authenticated", error)) + server.Logger.Log(LOG_INFO, fmt.Sprintf("API: Got HTTP error %v, retrying authorized", error)) // Tell the method that the token is expired server.OAuth.Token.ExpiredTimestamp = GenerateTimeSeconds() - return server.apiAuthenticated(method, endpoint, opts) + return server.apiAuthorized(method, endpoint, opts) } return header, nil, bodyErr } @@ -51,7 +51,7 @@ func (server *Server) apiAuthenticatedRetry(method string, endpoint string, opts } func (server *Server) APIInfo() error { - _, body, bodyErr := server.apiAuthenticatedRetry(http.MethodGet, "/info", nil) + _, body, bodyErr := server.apiAuthorizedRetry(http.MethodGet, "/info", nil) if bodyErr != nil { return bodyErr } @@ -77,7 +77,7 @@ func (server *Server) APIConnectWireguard(profile_id string, pubkey string) (str "profile_id": {profile_id}, "public_key": {pubkey}, } - header, connectBody, connectErr := server.apiAuthenticatedRetry(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) + header, connectBody, connectErr := server.apiAuthorizedRetry(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { return "", "", connectErr } @@ -95,7 +95,7 @@ func (server *Server) APIConnectOpenVPN(profile_id string) (string, string, erro urlForm := url.Values{ "profile_id": {profile_id}, } - header, connectBody, connectErr := server.apiAuthenticatedRetry(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) + header, connectBody, connectErr := server.apiAuthorizedRetry(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { return "", "", connectErr } @@ -106,5 +106,5 @@ func (server *Server) APIConnectOpenVPN(profile_id string) (string, string, erro // This needs no further return value as it's best effort func (server *Server) APIDisconnect() { - server.apiAuthenticatedRetry(http.MethodPost, "/disconnect", nil) + server.apiAuthorizedRetry(http.MethodPost, "/disconnect", nil) } diff --git a/internal/fsm.go b/internal/fsm.go index e848cae..6997d92 100644 --- a/internal/fsm.go +++ b/internal/fsm.go @@ -38,8 +38,8 @@ const ( // OAuth Started means the OAuth process has started OAUTH_STARTED - // Authenticated means the OAuth process has finished and the user is now authenticated with the server - AUTHENTICATED + // Authorized means the OAuth process has finished and the user is now authorized with the server + AUTHORIZED // Requested config means the user has requested a config for connecting REQUEST_CONFIG @@ -70,8 +70,8 @@ func (s FSMStateID) String() string { return "Request_Config" case ASK_PROFILE: return "Ask_Profile" - case AUTHENTICATED: - return "Authenticated" + case AUTHORIZED: + return "Authorized" case CONNECTED: return "Connected" default: @@ -105,13 +105,13 @@ func (fsm *FSM) Init(name string, callback func(string, string, string), logger fsm.States = FSMStates{ DEREGISTERED: {{NO_SERVER, "Client registers"}}, NO_SERVER: {{CHOSEN_SERVER, "User chooses a server"}}, - CHOSEN_SERVER: {{AUTHENTICATED, "Found tokens in config"}, {OAUTH_STARTED, "No tokens found in config"}}, - OAUTH_STARTED: {{AUTHENTICATED, "User authorizes with browser"}, {CHOSEN_SERVER, "Cancel OAuth"}}, - AUTHENTICATED: {{OAUTH_STARTED, "Re-authenticate with OAuth"}, {REQUEST_CONFIG, "Client requests a config"}}, + 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"}}, 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"}}, - CONNECTED: {{AUTHENTICATED, "OS reports disconnected"}}, + CONNECTED: {{AUTHORIZED, "OS reports disconnected"}}, } fsm.Current = DEREGISTERED fsm.Name = name diff --git a/internal/oauth.go b/internal/oauth.go index a49b492..9d17777 100644 --- a/internal/oauth.go +++ b/internal/oauth.go @@ -84,7 +84,7 @@ type OAuthToken struct { ExpiredTimestamp int64 `json:"expires_in_timestamp"` } -// Gets an authenticated HTTP client by obtaining refresh and access tokens +// Gets an authorized HTTP client by obtaining refresh and access tokens func (oauth *OAuth) getTokensWithCallback() error { oauth.Session.Context = context.Background() mux := http.NewServeMux() @@ -267,7 +267,7 @@ func (oauth *OAuth) start(name string, authorizationURL string, tokenURL string) // Error definitions func (oauth *OAuth) Finish() error { - if !oauth.FSM.HasTransition(AUTHENTICATED) { + if !oauth.FSM.HasTransition(AUTHORIZED) { return errors.New("invalid state to finish oauth") } tokenErr := oauth.getTokensWithCallback() @@ -275,7 +275,7 @@ func (oauth *OAuth) Finish() error { if tokenErr != nil { return tokenErr } - oauth.FSM.GoTransition(AUTHENTICATED) + oauth.FSM.GoTransition(AUTHORIZED) return nil } |
