fix 'Refresh failed: Invalid token provided' caused by token-fresher request to wrong idc - #118
Conversation
…request to wrong idc if serviceRegion is not same with ARN region
|
Second data point for this, from a different region pair. My Identity Center directory is in sa-east-1 and the CodeWhisperer profile ARN is in us-east-1, and the failure matches yours: every refresh after the first hour returns 400 invalid_request / "Invalid token provided". Probing both endpoints with the same refreshToken, clientId and clientSecret: The 200 response returns the same refreshToken it was given, so kiro-cli and the plugin can both refresh one session without invalidating each other's copy. One addition worth considering on top of const oidcRegion = regionFromOidcClientId(p.clientId) ?? auth.oidcRegion ?? auth.regionimport { isValidRegion } from '../constants.js'
function regionFromOidcClientId(clientId?: string): string | undefined {
if (!clientId) return undefined
for (let i = 0; i < clientId.length; i++) {
const tail = clientId.slice(i).replace(/-/g, '+').replace(/_/g, '/')
const padded = tail + '='.repeat((4 - (tail.length % 4)) % 4)
let decoded: string
try {
decoded = Buffer.from(padded, 'base64').toString('utf8')
} catch {
continue
}
const match = /([a-z]{2}(?:-[a-z]+)+-\d)$/.exec(decoded)
if (match && isValidRegion(match[1])) return match[1]
}
return undefined
}The This buys two things. An account whose row holds a wrong I ran this on my account with |
User may experience frequently error if they use non US access point.
An IdC account has two regions and they are not always the same: the OIDC
region that issued the SSO session and refresh token, and the service
region where the CodeWhisperer profile lives. The sync layer set both to
the profile ARN's region, discarding the real OIDC region:
Since token.ts resolves
auth.oidcRegion || auth.regionto build therefresh URL, every refresh for such an account went to the wrong endpoint
and failed deterministically.
Confirmed by probing both endpoints with the account's real credentials:
POST oidc.ap-southeast-1.amazonaws.com/token -> 200, expiresIn 3600
POST oidc.us-east-1.amazonaws.com/token
-> 400 invalid_request / "Invalid token provided"
The observed case is an ap-southeast-1 session paired with a us-east-1
profile ARN. The DB schema already had separate region/oidc_region
columns and token.ts already preferred oidcRegion, so only the sync layer
needed fixing.