Skip to content

Commit

Permalink
adding ability to force refresh source credential.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed May 27, 2024
1 parent 4604153 commit bc9fc9a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
15 changes: 12 additions & 3 deletions clients/internal/base/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,17 @@ func (c *SourceClientBase) GetSourceCredential() models.SourceCredential {
return c.SourceCredential
}

func (c *SourceClientBase) RefreshAccessToken() error {
func (c *SourceClientBase) RefreshAccessToken(options ...func(*models.SourceClientRefreshOptions)) error {
if c.testMode {
//if test mode is enabled, we cannot refresh the access token
return nil
}

refreshOptions := &models.SourceClientRefreshOptions{}
for _, o := range options {
o(refreshOptions)
}

c.refreshMutex.Lock()
defer c.refreshMutex.Unlock()

Expand All @@ -144,8 +149,12 @@ func (c *SourceClientBase) RefreshAccessToken() error {
Expiry: time.Unix(c.SourceCredential.GetExpiresAt(), 0),
}

if token.Expiry.Before(time.Now().Add(5 * time.Second)) { // expired (or will expire in 5 seconds) so let's update it
c.Logger.Info("access token expired, must refresh")
if refreshOptions.Force || token.Expiry.Before(time.Now().Add(5*time.Second)) { // expired (or will expire in 5 seconds) so let's update it
if refreshOptions.Force {
c.Logger.Info("force refresh access token", refreshOptions.Force)
} else {
c.Logger.Info("access token expired, must refresh")
}

if c.SourceCredential.IsDynamicClient() {
c.Logger.Info("refreshing dynamic client...")
Expand Down
2 changes: 1 addition & 1 deletion clients/internal/manual/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (m ManualClient) ExtractPatientId(bundleFile *os.File) (string, pkg.FhirVer
}
}

func (m ManualClient) RefreshAccessToken() error {
func (m ManualClient) RefreshAccessToken(options ...func(*models.SourceClientRefreshOptions)) error {
//TODO implement me
panic("implement me")
}
Expand Down
12 changes: 8 additions & 4 deletions clients/models/mock/mock_source_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/models/source_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ type SourceClient interface {
ExtractPatientId(bundleFile *os.File) (string, pkg.FhirVersion, error)

GetSourceCredential() SourceCredential
RefreshAccessToken() error
RefreshAccessToken(options ...func(*SourceClientRefreshOptions)) error
}
11 changes: 11 additions & 0 deletions clients/models/source_client_refresh_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type SourceClientRefreshOptions struct {
Force bool //force refresh even if token is not expired
}

func WithForce(force bool) func(*SourceClientRefreshOptions) {
return func(s *SourceClientRefreshOptions) {
s.Force = force
}
}

0 comments on commit bc9fc9a

Please sign in to comment.