Skip to content

Commit cc15b7f

Browse files
authored
fix: re-authorize during retries (#562)
1 parent 847a8f5 commit cc15b7f

2 files changed

Lines changed: 390 additions & 3 deletions

File tree

okta/client.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,9 +1470,43 @@ func (c *APIClient) doWithRetries(ctx context.Context, req *http.Request) (*http
14701470
maxRetries: c.cfg.Okta.Client.RateLimit.MaxRetries,
14711471
}
14721472
operation := func() (*http.Response, error) {
1473-
// Always rewind the request body when non-nil.
1474-
if bodyReader != nil {
1475-
req.Body = bodyReader()
1473+
if bOff.retryCount > 0 && (c.cfg.Okta.Client.AuthorizationMode == "PrivateKey" || c.cfg.Okta.Client.AuthorizationMode == "JWT") {
1474+
// Clear the token cache to force fresh authorization
1475+
// This will get a new access token and potentially a new nonce
1476+
c.tokenCache.Delete(AccessTokenCacheKey)
1477+
c.tokenCache.Delete(DpopAccessTokenNonce)
1478+
c.tokenCache.Delete(DpopAccessTokenPrivateKey)
1479+
1480+
headerParams := make(map[string]string)
1481+
queryParams := req.URL.Query()
1482+
req.URL.RawQuery = ""
1483+
auth, err := c.prepareRequest(ctx, req.URL.String(), req.Method, nil, headerParams, queryParams, url.Values{}, []formFile{})
1484+
if err != nil {
1485+
return nil, err
1486+
}
1487+
1488+
req.Header = req.Header.Clone() // Start with original headers
1489+
1490+
// Update only the authentication headers from the fresh auth request
1491+
req.Header.Set("Authorization", auth.Header.Get("Authorization"))
1492+
if dpopHeader := auth.Header.Get("Dpop"); dpopHeader != "" {
1493+
req.Header.Set("Dpop", dpopHeader)
1494+
}
1495+
if userAgentExt := auth.Header.Get("x-okta-user-agent-extended"); userAgentExt != "" {
1496+
req.Header.Set("x-okta-user-agent-extended", userAgentExt)
1497+
}
1498+
1499+
// Always rewind the request body when non-nil.
1500+
if bodyReader != nil {
1501+
req.Body = bodyReader()
1502+
}
1503+
} else {
1504+
// Reuse the existing request headers and body
1505+
req.Header = req.Header.Clone()
1506+
// Always rewind the request body when non-nil.
1507+
if bodyReader != nil {
1508+
req.Body = bodyReader()
1509+
}
14761510
}
14771511
resp, err := c.callAPI(req)
14781512
if errors.Is(err, io.EOF) {

0 commit comments

Comments
 (0)