Skip to content

Fix Redis Token Decoding for Zimfarm Authentication#1142

Closed
dkconnect wants to merge 1 commit intoopenzim:mainfrom
dkconnect:main
Closed

Fix Redis Token Decoding for Zimfarm Authentication#1142
dkconnect wants to merge 1 commit intoopenzim:mainfrom
dkconnect:main

Conversation

@dkconnect
Copy link
Copy Markdown

So i was going through this file and I felt this is a big missing part here. Please have a look

Problem

  1. Incorrect Token Lookup
  • Redis returns data as "bytes" in many clients
  • Code was accessing using "str" keys ("refresh_token")
  • so the Result: token was never found

2.. Repeated Unnecessary Authentication Calls

  • Because "refresh_token" was never detected, the system will continue requesting new tokens This will mean:
  • Extra API calls
  • Inefficient performance
  1. Runtime Errors
  • Direct ".decode()" could fail if Redis already returned strings

So i was going through this file and I felt this is a big missing part here. Please have a look

Problem
1. Incorrect Token Lookup
- Redis returns data as "bytes" in many clients
- Code was accessing using "str" keys ("refresh_token")
- so the Result: token was never found

2.. Repeated Unnecessary Authentication Calls
- Because "refresh_token" was never detected, the system will continue requesting new tokens
This will mean:
- Extra API calls
- Inefficient performance

3. Runtime Errors 
- Direct ".decode()" could fail if Redis already returned strings
@kelson42
Copy link
Copy Markdown
Collaborator

kelson42 commented Apr 1, 2026

@dkconnect Thank you for your PR. It will be reviewed by a maintainer within a few days.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Zimfarm authentication token retrieval by normalizing Redis hgetall() results so refresh tokens can be found even when Redis returns bytes, preventing unnecessary re-authentication.

Changes:

  • Default Redis hash read to {} when missing to avoid None handling branches.
  • Decode Redis hash keys/values from bytes to str before token lookup.
  • Switch refresh-token presence check to key membership ("refresh_token" in data).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wp1/zimfarm.py
Comment on lines +118 to 121
if "refresh_token" not in data:
logger.debug("No saved zimfarm refresh_token, requesting")
return request_zimfarm_token(redis)

Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that Redis bytes keys/values are normalized, the code will start hitting the refresh path, but refresh_zimfarm_token() does not persist the refreshed token/expiry back to Redis. This means expires_time in Redis remains expired and get_zimfarm_token() will call the refresh endpoint on every invocation after expiry. Consider storing the refresh response (and updating expires_time if needed) so subsequent calls can reuse the cached access token.

Copilot uses AI. Check for mistakes.
Comment thread wp1/zimfarm.py
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.78%. Comparing base (7d01e46) to head (356d5a4).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1142   +/-   ##
=======================================
  Coverage   92.78%   92.78%           
=======================================
  Files          74       74           
  Lines        4311     4312    +1     
=======================================
+ Hits         4000     4001    +1     
  Misses        311      311           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dkconnect
Copy link
Copy Markdown
Author

Hey @kelson42 @benoit74 I dont what am I missing here

do i need to do something different to pass the "Continuous Integration / black-format-check" test case

@audiodude
Copy link
Copy Markdown
Member

Just run black . to format all files with black in place.

I will do a proper review soon.

@audiodude
Copy link
Copy Markdown
Member

Preempted by #1146

@audiodude audiodude closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants