Skip to content

Commit 2217da5

Browse files
committed
feat: re-authorize during retries
1 parent b437c78 commit 2217da5

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

sdk/v2_requestExecutor.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,46 @@ func (re *RequestExecutor) doWithRetries(ctx context.Context, req *http.Request)
760760
if bodyReader != nil {
761761
req.Body = bodyReader()
762762
}
763+
764+
// Re-authorize the request to create a new DPoP JWT and access token
765+
if re.config.Okta.Client.AuthorizationMode == "PrivateKey" || re.config.Okta.Client.AuthorizationMode == "JWT" {
766+
// Clear the token cache to force fresh authorization
767+
// This will get a new access token and potentially a new nonce
768+
re.tokenCache.Delete(AccessTokenCacheKey)
769+
re.tokenCache.Delete(DpopAccessTokenNonce)
770+
re.tokenCache.Delete(DpopAccessTokenPrivateKey)
771+
772+
urlPath := req.URL.Path
773+
if req.URL.RawQuery != "" {
774+
urlPath += "?" + req.URL.RawQuery
775+
}
776+
777+
auth, err := re.NewRequest(req.Method, urlPath, nil)
778+
if err != nil {
779+
return err
780+
}
781+
782+
req.Header = req.Header.Clone() // Start with original headers
783+
784+
// Update only the authentication headers from the fresh auth request
785+
req.Header.Set("Authorization", auth.Header.Get("Authorization"))
786+
if dpopHeader := auth.Header.Get("Dpop"); dpopHeader != "" {
787+
req.Header.Set("Dpop", dpopHeader)
788+
}
789+
if userAgentExt := auth.Header.Get("x-okta-user-agent-extended"); userAgentExt != "" {
790+
req.Header.Set("x-okta-user-agent-extended", userAgentExt)
791+
}
792+
793+
if bodyReader != nil {
794+
req.Body = bodyReader()
795+
}
796+
} else {
797+
// Reuse the existing request headers and body
798+
req.Header = req.Header.Clone()
799+
if bodyReader != nil {
800+
req.Body = bodyReader()
801+
}
802+
}
763803
resp, err = re.httpClient.Do(req.WithContext(ctx))
764804
if errors.Is(err, io.EOF) {
765805
// retry on EOF errors, which might be caused by network connectivity issues

0 commit comments

Comments
 (0)