mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
Preserve unknown refresh recovery state without inventing an access-token rejection. Evaluate availability for the requested partition and block missing tokens before model-level selection in both auth guards. Cover partial refresh and later inference through real OAuth HTTP responses, CodexExecutor, and Manager execution. Auth, executor, integration, race, and build checks pass. The full suite encounters an existing WebSocket temporary-log cleanup race, reproduced on clean dev.
53 lines
2.4 KiB
Go
53 lines
2.4 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// refreshAccessError keeps an expired access token blocked without turning an
|
|
// uncertain refresh exchange into evidence of an upstream access-token rejection.
|
|
// wasTerminalUnauthorized describes the state before applying this refresh result.
|
|
func refreshAccessError(auth *Auth, message string, accessTokenRejected, wasTerminalUnauthorized bool) *Error {
|
|
if auth != nil && strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") &&
|
|
!accessTokenRejected && !wasTerminalUnauthorized &&
|
|
(auth.RejectedAccessToken == "" || auth.RejectedAccessToken != authAccessToken(auth)) &&
|
|
auth.RefreshError != nil && auth.RefreshError.Code == "refresh_outcome_unknown" {
|
|
outcome := cloneError(auth.RefreshError)
|
|
if outcome.HTTPStatus == http.StatusUnauthorized {
|
|
// A diagnostic 401 must not turn this recovery state into access rejection.
|
|
outcome.HTTPStatus = http.StatusServiceUnavailable
|
|
}
|
|
return outcome
|
|
}
|
|
return &Error{Code: "unauthorized", Message: message, HTTPStatus: http.StatusUnauthorized}
|
|
}
|
|
|
|
// codexRefreshMissingAccessForRequest keeps model-level availability from
|
|
// admitting a missing token while its grant still requires recovery.
|
|
func codexRefreshMissingAccessForRequest(ctx context.Context, auth *Auth, headers ...http.Header) bool {
|
|
if !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") || !auth.RequiresSignIn ||
|
|
auth.RefreshError == nil || auth.RefreshError.Code != "refresh_outcome_unknown" {
|
|
return false
|
|
}
|
|
return authAccessTokenForRequest(ctx, auth, headers...) == ""
|
|
}
|
|
|
|
// codexRefreshUnavailableForRequest lets a request use a healthy outer token
|
|
// while SIWC requires recovery. Ordinary cooldowns and model failures still apply.
|
|
func codexRefreshUnavailableForRequest(ctx context.Context, auth *Auth, now time.Time, headers ...http.Header) bool {
|
|
if !auth.Unavailable || !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") ||
|
|
auth.LastError == nil || auth.LastError.Code != "refresh_outcome_unknown" || !auth.RequiresSignIn ||
|
|
!IsCodexUseSIWC(auth) || IsCodexUseSIWCForRequest(ctx, auth, headers...) || auth.NextRetryAfter.After(now) ||
|
|
auth.RejectedSIWCRefreshToken == "" || auth.RejectedSIWCRefreshToken != authSIWCTokenField(auth, "refresh_token") {
|
|
return auth.Unavailable
|
|
}
|
|
if authOuterAccessToken(auth) == "" {
|
|
return true
|
|
}
|
|
expiry, hasExpiry := auth.OuterExpirationTime()
|
|
return hasExpiry && !expiry.After(now)
|
|
}
|