Skip to content

Commit

Permalink
Retry on specific authentication failures
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Nov 28, 2023
1 parent c85fcfc commit 76a4a7d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.4 (unreleased)

- Retry on specific authentication failures

## 0.1.3

- Enhance authentication error messages
Expand Down
73 changes: 40 additions & 33 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,40 +263,47 @@ func (client *Client) Login() error {
data := url.Values{}
data.Set("j_username", client.Usr)
data.Set("j_password", client.Pwd)
req := client.NewReq("POST", "/j_security_check", strings.NewReader(data.Encode()), NoLogPayload)
req.HttpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
httpRes, err := client.HttpClient.Do(req.HttpReq)
if err != nil {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Authentication failed: StatusCode %v", httpRes.StatusCode)
return fmt.Errorf("authentication failed, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
bodyBytes, _ := io.ReadAll(httpRes.Body)
if len(bodyBytes) > 0 {
log.Printf("[ERROR] Authentication failed: Invalid credentials")
return fmt.Errorf("authentication failed, invalid credentials")
}
req = client.NewReq("GET", "/dataservice/client/token", nil)
httpRes, err = client.HttpClient.Do(req.HttpReq)
if err != nil {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Token retrieval failed: StatusCode %v", httpRes.StatusCode)
return fmt.Errorf("authentication failed, token retrieval, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
token, _ := io.ReadAll(httpRes.Body)
if string(token) == "" {
log.Printf("[ERROR] Token retrieval failed: no token in payload")
return fmt.Errorf("authentication failed, no token in payload")
for attempts := 0; ; attempts++ {
req := client.NewReq("POST", "/j_security_check", strings.NewReader(data.Encode()), NoLogPayload)
req.HttpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
httpRes, err := client.HttpClient.Do(req.HttpReq)
if err != nil {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Authentication failed: StatusCode %v", httpRes.StatusCode)
return fmt.Errorf("authentication failed, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
bodyBytes, _ := io.ReadAll(httpRes.Body)
if len(bodyBytes) > 0 {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] Authentication failed: Invalid credentials")
return fmt.Errorf("authentication failed, invalid credentials")
} else {
log.Printf("[ERROR] Authentication failed: %s, retries: %v", err, attempts)
continue
}
}
req = client.NewReq("GET", "/dataservice/client/token", nil)
httpRes, err = client.HttpClient.Do(req.HttpReq)
if err != nil {
return err
}
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Token retrieval failed: StatusCode %v", httpRes.StatusCode)
return fmt.Errorf("authentication failed, token retrieval, status code: %v", httpRes.StatusCode)
}
defer httpRes.Body.Close()
token, _ := io.ReadAll(httpRes.Body)
if string(token) == "" {
log.Printf("[ERROR] Token retrieval failed: no token in payload")
return fmt.Errorf("authentication failed, no token in payload")
}
client.Token = string(token)
log.Printf("[DEBUG] Authentication successful")
return nil
}
client.Token = string(token)
log.Printf("[DEBUG] Authentication successful")
return nil
}

// Login if no token available.
Expand Down

0 comments on commit 76a4a7d

Please sign in to comment.