diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-04-12 22:52:49 +0200 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2023-09-25 09:43:37 +0200 |
| commit | a23c3e61c5d89ef67973891b5b3a176c06e1b174 (patch) | |
| tree | f1eed03b047f8affd3d5123fa5c9e868ac7d8bec /internal/server/institute | |
| parent | ee95eb45708e1fa766a63866d26d05d13f23e8c9 (diff) | |
Refactor: Split internal server into multiple packages
- Pass contexts
- Have separate packages for e.g. custom, institute and secure
- internet servers, profiles....
- Return types from the public ./types package with a Public() method
Diffstat (limited to 'internal/server/institute')
| -rw-r--r-- | internal/server/institute/institute.go | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/internal/server/institute/institute.go b/internal/server/institute/institute.go new file mode 100644 index 0000000..ada1977 --- /dev/null +++ b/internal/server/institute/institute.go @@ -0,0 +1,106 @@ +package institute + +import ( + "context" + + "github.com/eduvpn/eduvpn-common/internal/oauth" + "github.com/eduvpn/eduvpn-common/internal/server/api" + "github.com/eduvpn/eduvpn-common/internal/server/base" + "github.com/eduvpn/eduvpn-common/types/server" + "github.com/go-errors/errors" +) + +type Server struct { + // An instute access server has its own OAuth + Auth oauth.OAuth `json:"oauth"` + + // Embed the server base + Basic base.Base `json:"base"` +} + +type Servers struct { + Map map[string]*Server `json:"map"` + CurrentURL string `json:"current_url"` +} + +func New( + ctx context.Context, + url string, + name map[string]string, + supportContact []string, +) (*Server, error) { + b := base.Base{ + URL: url, + DisplayName: name, + SupportContact: supportContact, + Type: server.TypeInstituteAccess, + } + if err := api.Endpoints(ctx, &b); err != nil { + return nil, err + } + API := b.Endpoints.API.V3 + + s := &Server{Basic: b} + s.Auth.Init(url, API.Authorization, API.Token) + return s, nil +} + +func (s *Servers) Current() (*Server, error) { + if s.Map == nil { + return nil, errors.Errorf("No map is found when getting the current server") + } + + srv, ok := s.Map[s.CurrentURL] + if !ok || srv == nil { + return nil, errors.Errorf("server not found") + } + return srv, nil +} + +func (s *Servers) Remove(url string) error { + // check if it is in the map to begin with + if _, ok := s.Map[url]; ok { + delete(s.Map, url) + } else { + return errors.Errorf("cannot remove URL: %v, not found in list", url) + } + + // Reset the current url + if s.CurrentURL == url { + s.CurrentURL = "" + } + return nil +} + +func (s *Servers) Add(srv *Server) { + if s.Map == nil { + s.Map = make(map[string]*Server) + } + s.Map[srv.Basic.URL] = srv +} + +func (s *Server) TemplateAuth() func(string) string { + return func(authURL string) string { + return authURL + } +} + +func (s *Server) Base() (*base.Base, error) { + return &s.Basic, nil +} + +func (s *Server) OAuth() *oauth.OAuth { + return &s.Auth +} + +func (s *Server) NeedsLocation() bool { + return false +} + +func (s *Server) Public() (interface{}, error) { + return &server.Server{ + DisplayName: s.Basic.DisplayName, + Identifier: s.Basic.URL, + Profiles: s.Basic.Profiles.Public(), + }, nil +} |
