Skip to content

Commit

Permalink
Add auth token refresh to RichClient
Browse files Browse the repository at this point in the history
There was no logic to refresh auth token needed for longer-time tasks
(e.g. tests). Adding this method to RichCLient.

To be able use Token Refresh method, it was needed keep
whole api.Login struct as part of client to have refresh token.

Related to konveyor/go-konveyor-tests#71

Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Jan 12, 2024
1 parent 951cb48 commit 9c6cfd0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
8 changes: 4 additions & 4 deletions binding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s Path) Inject(p Params) (out string) {

//
// NewClient Constructs a new client
func NewClient(url, token string) (client *Client) {
func NewClient(url string, token api.Login) (client *Client) {
client = &Client{
baseURL: url,
token: token,
Expand All @@ -90,7 +90,7 @@ type Client struct {
// baseURL for the nub.
baseURL string
// addon API token
token string
token api.Login
// transport
transport http.RoundTripper
// Retry limit.
Expand All @@ -101,7 +101,7 @@ type Client struct {

//
// SetToken sets hub token on client
func (r *Client) SetToken(token string) {
func (r *Client) SetToken(token api.Login) {
r.token = token
}

Expand Down Expand Up @@ -662,7 +662,7 @@ func (r *Client) send(rb func() (*http.Request, error)) (response *http.Response
if err != nil {
return
}
request.Header.Set(api.Authorization, r.token)
request.Header.Set(api.Authorization, r.token.Token)
client := http.Client{Transport: r.transport}
response, err = client.Do(request)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type RichClient struct {
func New(baseUrl string) (r *RichClient) {
//
// Build REST client.
client := NewClient(baseUrl, "")
client := NewClient(baseUrl, api.Login{})

//
// Build RichClient.
Expand Down Expand Up @@ -143,6 +143,18 @@ func (r *RichClient) Login(user, password string) (err error) {
if err != nil {
return
}
r.Client.SetToken(login.Token)
r.Client.SetToken(login)
return
}

//
// Refresh client token.
func (r *RichClient) RefreshToken() (err error) {
login := api.Login{Refresh: r.Client.token.Refresh}
err = r.Client.Post(api.AuthRefreshRoot, &login)
if err != nil {
return
}
r.Client.SetToken(login)
return
}
7 changes: 7 additions & 0 deletions test/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"fmt"
"os"
"time"

"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/settings"
Expand All @@ -22,6 +23,12 @@ func PrepareRichClient() (richClient *binding.RichClient) {
// Prepare RichClient and login to Hub API
richClient = binding.New(settings.Settings.Addon.Hub.URL)
err := richClient.Login(os.Getenv(Username), os.Getenv(Password))

// Start goroutine with token refresh.
go func () {
time.Sleep(1 * time.Minute) // TODO: base sleep time on token expiration
richClient.RefreshToken()
}()

if err != nil {
panic(fmt.Sprintf("Cannot login to API: %v.", err.Error()))
Expand Down

0 comments on commit 9c6cfd0

Please sign in to comment.