diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/api.go | 50 |
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{} |
