diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-03-13 16:22:32 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-03-13 16:27:24 +0100 |
| commit | d182c9d52e7e27bb3d5a698d4788a3f0919012bc (patch) | |
| tree | 3e9bd451bb4c6e3b880cc8c18198915a6d3d5d8c /internal/server/custom.go | |
| parent | e604b1e1424edff72f4f0e7f447e66ffee850110 (diff) | |
Client + Server: Refactor adding a server by first adding then auth
Otherwise we go to the main state after auth is done and the server is
not yet added
Diffstat (limited to 'internal/server/custom.go')
| -rw-r--r-- | internal/server/custom.go | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/internal/server/custom.go b/internal/server/custom.go index 10e9a28..d2679cf 100644 --- a/internal/server/custom.go +++ b/internal/server/custom.go @@ -2,7 +2,6 @@ package server import ( "context" - "time" "github.com/eduvpn/eduvpn-common/internal/api" "github.com/eduvpn/eduvpn-common/internal/config/v2" @@ -14,7 +13,7 @@ import ( // `ctx` is the context used for cancellation // `id` is the identifier of the server, the base URL // `na` specifies whether or not we want to add the server without doing authorization now -func (s *Servers) AddCustom(ctx context.Context, id string, na bool) (*Server, error) { +func (s *Servers) AddCustom(ctx context.Context, id string, na bool) error { sd := api.ServerData{ ID: id, Type: server.TypeCustom, @@ -22,25 +21,24 @@ func (s *Servers) AddCustom(ctx context.Context, id string, na bool) (*Server, e BaseAuthWK: id, } - var a *api.API - var err error - if !na { - // Authorize by creating the API object - a, err = api.NewAPI(ctx, s.clientID, sd, s.cb, nil) - if err != nil { - return nil, err - } - } - - err = s.config.AddServer(id, server.TypeCustom, v2.Server{LastAuthorizeTime: time.Now()}) + err := s.config.AddServer(id, server.TypeCustom, v2.Server{}) if err != nil { - return nil, err + return err } - cust := s.NewServer(id, server.TypeCustom, a) - // Return the server with the API + // no authorization should be triggered, return + if na { + return nil + } - return &cust, nil + // Authorize by creating the API object + _, err = api.NewAPI(ctx, s.clientID, sd, s.cb, nil) + if err != nil { + // authorization has failed, remove the server again + s.config.RemoveServer(id, server.TypeCustom) + return err + } + return nil } // GetCustom gets a custom server |
