A lightweight Lua patch for KOReader that enables authentication against Cloudflare Access (Zero Trust) using Service Tokens.
This patch allows you to access OPDS catalogs (like Calibre-Web Automated, Audiobookshelf, or Kavita) that are protected behind Cloudflare Access without needing a browser login, VPN client, or complex proxy setups on your e-reader.
KOReader natively supports HTTP/HTTPS but does not support the interactive OAuth login flows (Google/GitHub) required by Cloudflare Access.
This script uses "Monkey Patching" to hook into the core Lua network libraries (socket.http and ssl.https) inside KOReader. It intercepts every network request made by the device and automatically injects the CF-Access-Client-Id and CF-Access-Client-Secret headers before the request leaves the device.
- A device running KOReader (Kindle, Kobo, Android, etc.).
- A Cloudflare Zero Trust account protecting your OPDS server.
Before installing the patch, you must generate a Service Token in Cloudflare. This acts as a machine-to-machine username/password for your device.
- Open your Cloudflare Zero Trust Dashboard.
- Navigate to Access > Service Auth.
- Click Create Service Token.
- Name:
KOReader Device(or similar). - Duration: Set to
Non-expiring(recommended) or a custom duration.
- Name:
- Copy the "Client ID" and "Client Secret" immediately. You will not be able to see the secret again.
- Navigate to Access > Applications and select your OPDS application.
- Add a new Policy (or edit your existing one):
- Action:
Service Auth(Recommended) orAllow. - Rule: Select
Service Tokenand choose the token you just created.
- Action:
- Download the
2-cloudflare-auth.luafile from this repository. - Open the file in a text editor (Notepad++, VS Code, etc.).
- Replace the placeholder credentials with your tokens from Cloudflare:
local CF_ID = "put-your-client-id-here" local CF_SECRET = "put-your-client-secret-here"
- Connect your KOReader device to your computer via USB.
- Navigate to the KOReader directory on the device:
- Kindle:
.adds/koreader/patches/ - Kobo:
.adds/koreader/patches/ - Android:
/koreader/patches/ - (Note: If the
patchesfolder does not exist, create it).
- Kindle:
- Copy your modified
2-cloudflare-auth.luainto that folder. - Restart KOReader (Exit and re-open, or full reboot).
This patch integrates with KOReader's internal logging system. If you are having issues:
- Open the
crash.logfile in your KOReader directory. - Search for
CF-Auth. - You should see success messages like:
CF-Auth: Initializing... CF-Auth: βββ Hooks installed successfully βββ CF-Auth: β Injected headers for URL: [https://your-opds-url.com/opds](https://your-opds-url.com/opds)
- "Unable to Connect": Check your
CF_IDandCF_SECRETfor typos. Ensure your Cloudflare Policy is set to "Service Auth" and includes the token. - Boot Loop: If KOReader crashes on boot, delete the file from the
patchesfolder via USB.
Your Client Secret is stored in plain text on the device.
- If you lose your device, anyone with USB access could potentially copy the token.
- Mitigation: If your device is lost or stolen, simply revoke the Service Token in the Cloudflare Dashboard. This will immediately cut off access without needing to change your server passwords.
MIT License. Feel free to use, modify, and distribute.