Make player token reset non-breaking (deferred rotation)

The maintenance-page reset used to rotate auth_code immediately. Because the
token also guards the playlist endpoints, that stopped a player from syncing
content the instant the button was pressed -- before the player ever had a
chance to react -- and clients that do not understand the heartbeat's
401 auth_required would have stayed broken until redeployed.

Split the model behaviour:

* request_reauth()  -- sets the flag only (used by the admin reset endpoints
  and the automatic interval). The current token stays valid, so there is no
  outage while the player is being asked to re-authenticate.
* issue_new_token() -- replaces the token now (used by the existing
  "regenerate auth code" action, where the admin is shown the new value).
* mark_authenticated() -- if a reset was pending, this is where the old token
  is retired: a new token is issued and returned to the player in the
  /api/auth/player response.

Net effect: the reset is a request the player acts on, never a forced logout.
A player that ignores it keeps working and shows as "awaiting re-auth" in the
UI; one that handles it gets a fresh token and the old one stops working.

Maintenance page wording updated to match (request vs reset; "awaiting
re-auth" badge), plus a themed info note explaining it is not a forced logout.

Adds docs/player-auth-reset.md describing the protocol and exactly what the
player client (Kiwy-Signage) still needs to implement: detect 401 /
auth_required on the heartbeat (and on the token-guarded endpoints), re-run
POST /api/auth/player with the stored quick-connect code, save the new
auth_code and retry. No password or SSH redeploy required.

Verified end-to-end: token unchanged and still accepted after the request
(200), heartbeat answers 401 auth_required=token_reset, re-auth issues a
different token, the old token is then rejected (403) and the new one accepted
(200), flag cleared; the interval path only flags without rotating.
This commit is contained in:
2026-10-14 23:14:11 +03:00
parent 4dc8506c9b
commit 6b0e51898c
6 changed files with 287 additions and 62 deletions
+20 -15
View File
@@ -1585,22 +1585,27 @@ def maintenance_set_interval():
@login_required
@admin_required
def maintenance_reset_token(player_id: int):
"""Reset a single player's authentication token."""
"""Request that a single player re-authenticate.
The token is not replaced here (see ``Player.request_reauth``): the player
is flagged, told to authenticate again on its next heartbeat, and receives
a fresh token at that point.
"""
try:
player = Player.query.get_or_404(player_id)
player.rotate_auth_token(force_reauth=True)
player.request_reauth()
db.session.commit()
log_action('info',
f'Auth token reset for player "{player.name}" '
f'Re-authentication requested for player "{player.name}" '
f'(ID: {player.id}) by {current_user.username}')
flash(f'Token reset for "{player.name}". It will re-authenticate on its '
f'next heartbeat.', 'success')
flash(f'"{player.name}" will be asked to re-authenticate on its next '
f'heartbeat and then receive a new token.', 'success')
except Exception as exc:
db.session.rollback()
log_action('error', f'Error resetting auth token: {exc}')
flash('Error resetting the authentication token.', 'danger')
log_action('error', f'Error requesting token reset: {exc}')
flash('Error requesting the re-authentication.', 'danger')
return redirect(url_for('admin.maintenance'))
@@ -1609,23 +1614,23 @@ def maintenance_reset_token(player_id: int):
@login_required
@admin_required
def maintenance_reset_all_tokens():
"""Reset the authentication token of every player."""
"""Request that every player re-authenticate (see ``request_reauth``)."""
try:
players = Player.query.all()
for player in players:
player.rotate_auth_token(force_reauth=True)
player.request_reauth()
db.session.commit()
log_action('info',
f'Auth tokens reset for all {len(players)} player(s) by '
f'{current_user.username}')
flash(f'Token reset for {len(players)} player(s). They will '
f're-authenticate on their next heartbeat.', 'success')
f'Re-authentication requested for all {len(players)} '
f'player(s) by {current_user.username}')
flash(f'{len(players)} player(s) will be asked to re-authenticate on '
f'their next heartbeat and then receive new tokens.', 'success')
except Exception as exc:
db.session.rollback()
log_action('error', f'Error resetting all auth tokens: {exc}')
flash('Error resetting the authentication tokens.', 'danger')
log_action('error', f'Error requesting all token resets: {exc}')
flash('Error requesting the re-authentications.', 'danger')
return redirect(url_for('admin.maintenance'))