diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-06 16:27:45 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | a84050a5e93f5fb9f5bbb79ca21b37e8359cf289 (patch) | |
| tree | ecdf0cea81b0bd6a3cf669f2b31c45a222d1c5f5 /internal/server/institute.go | |
| parent | 3152078aec8334357a61171838f664eb03299211 (diff) | |
Server: Refactor internal server package to use new state file
This completely rewrites the internal server package. Some advantages:
- Caches less
- Uses a callback interface so that the client package does not get so
convoluted
- Introduce a new API package that only deals with the server API and
uses github.com/jwijenbergh/eduoauth-go
Diffstat (limited to 'internal/server/institute.go')
| -rw-r--r-- | internal/server/institute.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/internal/server/institute.go b/internal/server/institute.go new file mode 100644 index 0000000..881f96d --- /dev/null +++ b/internal/server/institute.go @@ -0,0 +1,73 @@ +package server + +import ( + "context" + "time" + + "github.com/eduvpn/eduvpn-common/internal/api" + "github.com/eduvpn/eduvpn-common/internal/config/v2" + "github.com/eduvpn/eduvpn-common/internal/discovery" + "github.com/eduvpn/eduvpn-common/types/server" + "github.com/jwijenbergh/eduoauth-go" +) + +func (s *Servers) AddInstitute(ctx context.Context, disco *discovery.Discovery, id string, na bool) (*Server, error) { + // This is basically done to double check if the server is part of the institute access section of disco + dsrv, err := disco.ServerByURL(id, "institute_access") + if err != nil { + return nil, err + } + + sd := api.ServerData{ + ID: dsrv.BaseURL, + Type: server.TypeInstituteAccess, + BaseWK: dsrv.BaseURL, + BaseAuthWK: dsrv.BaseURL, + } + + var a *api.API + 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(dsrv.BaseURL, server.TypeInstituteAccess, v2.Server{LastAuthorizeTime: time.Now()}) + if err != nil { + return nil, err + } + + inst := s.NewServer(dsrv.BaseURL, server.TypeInstituteAccess, a) + return &inst, nil +} + +func (s *Servers) GetInstitute(ctx context.Context, id string, disco *discovery.Discovery, tok *eduoauth.Token, disableAuth bool) (*Server, error) { + // This is basically done to double check if the server is part of the institute access section of disco + dsrv, err := disco.ServerByURL(id, "institute_access") + if err != nil { + return nil, err + } + + // Get the server from the config + _, err = s.config.GetServer(dsrv.BaseURL, server.TypeInstituteAccess) + if err != nil { + return nil, err + } + sd := api.ServerData{ + ID: dsrv.BaseURL, + Type: server.TypeInstituteAccess, + BaseWK: dsrv.BaseURL, + BaseAuthWK: dsrv.BaseURL, + DisableAuthorize: disableAuth, + } + // Authorize by creating the API object + a, err := api.NewAPI(ctx, s.clientID, sd, s.cb, tok) + if err != nil { + return nil, err + } + + inst := s.NewServer(dsrv.BaseURL, server.TypeInstituteAccess, a) + return &inst, nil +} |
