Fix Redis Token Decoding for Zimfarm Authentication#1142
Fix Redis Token Decoding for Zimfarm Authentication#1142dkconnect wants to merge 1 commit intoopenzim:mainfrom
Conversation
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
|
@dkconnect Thank you for your PR. It will be reviewed by a maintainer within a few days. |
There was a problem hiding this comment.
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 avoidNonehandling branches. - Decode Redis hash keys/values from
bytestostrbefore 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.
| if "refresh_token" not in data: | ||
| logger.debug("No saved zimfarm refresh_token, requesting") | ||
| return request_zimfarm_token(redis) | ||
|
|
There was a problem hiding this comment.
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Just run I will do a proper review soon. |
|
Preempted by #1146 |
So i was going through this file and I felt this is a big missing part here. Please have a look
Problem
2.. Repeated Unnecessary Authentication Calls