summaryrefslogtreecommitdiff
path: root/internal/api
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-28 16:48:26 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2024-02-28 16:48:26 +0100
commit617aa3a52eb5acfb98c1b9b54ed47d8563949cd8 (patch)
treea3ae559c2dbbf06542a93dbc8867c57b6a72a5ca /internal/api
parent35b8fb90a9cb836ee228fed20720b7ade494532f (diff)
API: Update to latest eduoauth-go for better fetching of well-known
Diffstat (limited to 'internal/api')
-rw-r--r--internal/api/api.go50
1 files changed, 17 insertions, 33 deletions
diff --git a/internal/api/api.go b/internal/api/api.go
index d611896..f7b8329 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -64,17 +64,20 @@ type API struct {
// NewAPI creates a new API object by creating an OAuth object
func NewAPI(ctx context.Context, clientID string, sd ServerData, cb Callbacks, tokens *eduoauth.Token) (*API, error) {
- ep, epauth, err := refreshEndpoints(ctx, sd)
- if err != nil {
- return nil, err
- }
-
cr := customRedirect(clientID)
// Construct OAuth
o := eduoauth.OAuth{
ClientID: clientID,
- BaseAuthorizationURL: epauth.Authorization,
- TokenURL: epauth.Token,
+ EndpointFunc: func(ctx context.Context) (*eduoauth.EndpointResponse, error) {
+ ep, err := getEndpoints(ctx, sd.BaseAuthWK)
+ if err != nil {
+ return nil, err
+ }
+ return &eduoauth.EndpointResponse{
+ AuthorizationURL: ep.API.V3.Authorization,
+ TokenURL: ep.API.V3.Token,
+ }, nil
+ },
CustomRedirect: cr,
RedirectPath: "/callback",
TokensUpdated: func(tok eduoauth.Token) {
@@ -89,10 +92,9 @@ func NewAPI(ctx context.Context, clientID string, sd ServerData, cb Callbacks, t
api := &API{
cb: cb,
oauth: &o,
- apiURL: ep.API,
Data: sd,
}
- err = api.authorize(ctx)
+ err := api.authorize(ctx)
if err != nil {
return nil, err
}
@@ -120,7 +122,7 @@ func (a *API) authorize(ctx context.Context) (err error) {
}()
scope := "config"
- url, err := a.oauth.AuthURL(scope)
+ url, err := a.oauth.AuthURL(ctx, scope)
if err != nil {
return err
}
@@ -141,7 +143,11 @@ func (a *API) authorize(ctx context.Context) (err error) {
}
func (a *API) authorized(ctx context.Context, method string, endpoint string, opts *httpw.OptionalParams) (http.Header, []byte, error) {
- u := a.apiURL + endpoint
+ ep, err := getEndpoints(ctx, a.Data.BaseWK)
+ if err != nil {
+ return nil, nil, err
+ }
+ u := ep.API.V3.API + endpoint
// TODO: Cache HTTP client?
httpC := httpw.NewClient(a.oauth.NewHTTPClient())
@@ -335,28 +341,6 @@ func getEndpoints(ctx context.Context, url string) (*endpoints.Endpoints, error)
return &ep, nil
}
-func refreshEndpoints(ctx context.Context, sd ServerData) (*endpoints.List, *endpoints.List, error) {
- // Get the endpoints
- ep, err := getEndpoints(ctx, sd.BaseWK)
- if err != nil {
- return nil, nil, err
- }
-
- // This is a mess but we essentially have to instantiate different endpoints if the authorization base URL is different from the base portal URL
- // This happens with secure internet when the location is not equal to the home location
- var epauth *endpoints.Endpoints
- if sd.BaseAuthWK != sd.BaseWK {
- oep, err := getEndpoints(ctx, sd.BaseAuthWK)
- if err != nil {
- return nil, nil, err
- }
- epauth = oep
- } else {
- epauth = ep
- }
- return &ep.API.V3, &epauth.API.V3, err
-}
-
// OAuthLogger is defined here to update the internal logger
// for the eduoauth library
type OAuthLogger struct{}