2022-08-28 22:32:13 -06:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
|
2022-09-15 23:26:37 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
2022-08-28 22:32:13 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/oauth2"
|
2022-09-15 23:26:37 -06:00
|
|
|
"io"
|
2022-08-28 22:32:13 -06:00
|
|
|
"net/http"
|
2022-09-15 23:26:37 -06:00
|
|
|
"os"
|
2022-08-28 22:32:13 -06:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BaseClient struct {
|
2022-09-13 19:02:26 -06:00
|
|
|
Context context.Context
|
2022-08-28 22:32:13 -06:00
|
|
|
AppConfig config.Interface
|
|
|
|
Logger logrus.FieldLogger
|
|
|
|
|
|
|
|
OauthClient *http.Client
|
2022-08-30 20:03:24 -06:00
|
|
|
Source models.Source
|
2022-08-28 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:26:37 -06:00
|
|
|
func (c *BaseClient) SyncAllBundle(db database.DatabaseRepository, bundleFile *os.File) error {
|
|
|
|
panic("SyncAllBundle functionality is not available on this client")
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:02:26 -06:00
|
|
|
func NewBaseClient(ctx context.Context, appConfig config.Interface, globalLogger logrus.FieldLogger, source models.Source, testHttpClient ...*http.Client) (*BaseClient, *models.Source, error) {
|
2022-08-28 22:32:13 -06:00
|
|
|
var httpClient *http.Client
|
2022-09-09 00:51:46 -06:00
|
|
|
var updatedSource *models.Source
|
2022-08-28 22:32:13 -06:00
|
|
|
if len(testHttpClient) == 0 {
|
2022-09-09 00:51:46 -06:00
|
|
|
//check if we need to refresh the access token
|
|
|
|
//https://github.com/golang/oauth2/issues/84#issuecomment-520099526
|
|
|
|
// https://chromium.googlesource.com/external/github.com/golang/oauth2/+/8f816d62a2652f705144857bbbcc26f2c166af9e/oauth2.go#239
|
|
|
|
conf := &oauth2.Config{
|
|
|
|
ClientID: source.ClientId,
|
|
|
|
ClientSecret: "",
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2022-09-19 21:21:22 -06:00
|
|
|
AuthURL: source.OauthAuthorizationEndpoint,
|
|
|
|
TokenURL: source.OauthTokenEndpointAuthMethods,
|
2022-09-09 00:51:46 -06:00
|
|
|
},
|
|
|
|
//RedirectURL: "",
|
|
|
|
//Scopes: nil,
|
|
|
|
}
|
|
|
|
token := &oauth2.Token{
|
|
|
|
TokenType: "Bearer",
|
|
|
|
RefreshToken: source.RefreshToken,
|
|
|
|
AccessToken: source.AccessToken,
|
|
|
|
Expiry: time.Unix(source.ExpiresAt, 0),
|
|
|
|
}
|
|
|
|
if token.Expiry.Before(time.Now()) { // expired so let's update it
|
|
|
|
src := conf.TokenSource(ctx, token)
|
|
|
|
newToken, err := src.Token() // this actually goes and renews the tokens
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if newToken.AccessToken != token.AccessToken {
|
|
|
|
token = newToken
|
|
|
|
|
|
|
|
// update the "source" credential with new data (which will need to be sent
|
|
|
|
updatedSource = &source
|
|
|
|
updatedSource.AccessToken = newToken.AccessToken
|
|
|
|
updatedSource.ExpiresAt = newToken.Expiry.Unix()
|
|
|
|
// Don't overwrite `RefreshToken` with an empty value
|
|
|
|
// if this was a token refreshing request.
|
|
|
|
if newToken.RefreshToken != "" {
|
|
|
|
updatedSource.RefreshToken = newToken.RefreshToken
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OLD CODE
|
|
|
|
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(token))
|
|
|
|
|
2022-08-28 22:32:13 -06:00
|
|
|
} else {
|
|
|
|
//Testing mode.
|
|
|
|
httpClient = testHttpClient[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient.Timeout = 10 * time.Second
|
2022-09-15 23:26:37 -06:00
|
|
|
|
2022-08-30 20:03:24 -06:00
|
|
|
return &BaseClient{
|
2022-09-13 19:02:26 -06:00
|
|
|
Context: ctx,
|
2022-08-28 22:32:13 -06:00
|
|
|
AppConfig: appConfig,
|
|
|
|
Logger: globalLogger,
|
|
|
|
OauthClient: httpClient,
|
2022-08-30 20:03:24 -06:00
|
|
|
Source: source,
|
2022-09-09 00:51:46 -06:00
|
|
|
}, updatedSource, nil
|
2022-08-28 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// HttpClient
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
func (c *BaseClient) GetRequest(resourceSubpath string, decodeModelPtr interface{}) error {
|
2022-08-30 20:03:24 -06:00
|
|
|
url := fmt.Sprintf("%s/%s", strings.TrimRight(c.Source.ApiEndpointBaseUrl, "/"), strings.TrimLeft(resourceSubpath, "/"))
|
2022-08-28 22:32:13 -06:00
|
|
|
resp, err := c.OauthClient.Get(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
|
2022-09-10 21:12:29 -06:00
|
|
|
return fmt.Errorf("An error occurred during request %s - %d - %s", url, resp.StatusCode, resp.Status)
|
2022-08-28 22:32:13 -06:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:26:37 -06:00
|
|
|
err = ParseBundle(resp.Body, decodeModelPtr)
|
2022-08-28 22:32:13 -06:00
|
|
|
return err
|
|
|
|
}
|
2022-08-30 20:03:24 -06:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper Functions
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-09-15 23:26:37 -06:00
|
|
|
func ParseBundle(r io.Reader, decodeModelPtr interface{}) error {
|
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
//decoder.DisallowUnknownFields() //make sure we throw an error if unknown fields are present.
|
|
|
|
err := decoder.Decode(decodeModelPtr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|