Skip to content

Do not clear the configured OAuth Client ID on a token mismatch (#712) - #713

Open
einanderson wants to merge 2 commits into
anxdpanic:masterfrom
einanderson:fix/client-id-cleared-on-mismatch
Open

Do not clear the configured OAuth Client ID on a token mismatch (#712)#713
einanderson wants to merge 2 commits into
anxdpanic:masterfrom
einanderson:fix/client-id-cleared-on-mismatch

Conversation

@einanderson

Copy link
Copy Markdown
Contributor

Fixes the loop reported in #712: the add-on clears the OAuth Client ID the user configured.

What happens

Twitch.valid_token() compares the Client ID the token was issued for with the configured one.
On a mismatch it called utils.clear_client_id() — deleting the user's setting and falling back
to the bundled Client ID, which Twitch treats as a confidential client and which therefore cannot
refresh. Every fresh login then died again after about an hour.

It is a stable loop, because the documented order makes users walk straight into it: the README
said log in first, enter your own Client ID afterwards — which produces exactly that mismatch, and
every repair attempt triggers the same wipe. The reporter's dialog quote "Client id mismatch /
Press OK to resolve" is the wipe being confirmed.

The change

Discard the stale token instead and ask for a new login; the configured Client ID is left
alone. clear_client_id() has no other caller and is removed. The README now puts the Client ID
step before the login.

A second commit closes the ordering trap itself: a Monitor in the service discards the stored
token as soon as oauth_clientid changes, so it no longer matters whether you log in first or
configure the Client ID first. Clearing the store mirrors empty values back into the settings and
fires onSettingsChanged again — the remembered Client ID is updated before clearing, which
makes that second call a no-op.

Verification

Reproduced and fixed on real hardware (Kodi 21.3, LibreELEC, Raspberry Pi 4), by writing a token
issued for the bundled Client ID and then configuring a different one:

before after
log OAuth Client-ID mismatch … mismatch, discarding the stored token
configured Client ID erased (<setting id="oauth_clientid" default="true" />) kept
token left in place, dies within the hour discarded, new login requested

Two details worth knowing for anyone re-testing this: the wipe only happens after the dialog is
confirmed, and only when the stale token belongs to the bundled Client ID — a token from another
own application does not reach that branch.

onSettingsChanged was verified to fire as well: the token is dropped 64 ms after the setting
changes, exactly once, with no re-entrancy from the settings mirror.

The reporter of #712 confirmed the workaround derived from this diagnosis (revoke token → enter
Client ID → log in) restores stable tokens for them.

einanderson and others added 2 commits August 12, 2026 04:29
When the stored token belonged to the bundled Client-ID while the user had
configured their own, valid_token() cleared the configured Client-ID and fell
back to the bundled one. The bundled Client-ID is confidential and cannot
refresh tokens, so the user was stranded: every login died ~1h later, and
re-entering the Client-ID triggered the very same wipe again.

Following the README this was the normal path, not an edge case: log in first,
register a public app and enter its Client-ID afterwards -> the next API call
sees the mismatch and wipes the setting that was just entered.

Discard the stale token instead and ask for a new login; the configured
Client-ID is the user's deliberate setting and stays untouched. The README now
puts the Client-ID before the login, so the mismatch does not happen in the
first place, and points existing users at a single re-login.

Fixes anxdpanic#712

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A token is only valid for the Client-ID it was issued for. Entering a Client-ID
after logging in therefore leaves a stale token behind, which only surfaces on
the next API call as a "Client id mismatch" dialog and a manual re-login. That
ordering trap is what made issue anxdpanic#712 so easy to walk into.

Discard the token as soon as the setting changes, so the order in which the user
logs in and configures their Client-ID no longer matters.

Clearing the store mirrors empty values back into the Kodi settings, which fires
onSettingsChanged again; remembering the new Client-ID before clearing makes the
second call a no-op.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant