Skip to content

Fix OctoPrint login loop in the Device tab when an API key is set (#1088) - #1218

Open
alandebortolo wants to merge 1 commit into
Snapmaker:mainfrom
DesignMaster-Solutions:fix/octoprint-login-apikey
Open

alandebortolo wants to merge 1 commit into
Snapmaker:mainfrom
DesignMaster-Solutions:fix/octoprint-login-apikey

Conversation

@alandebortolo

Copy link
Copy Markdown

Fixes #1088.

What happens

For every non-SimplyPrint host that has an API key set, Plater.cpp passes the key to the Device tab, and PrinterWebView::SendAPIKey() wraps fetch and XMLHttpRequest so that every request of the printer page carries X-API-Key. That includes OctoPrint's own POST /api/login.

OctoPrint treats a request that arrives with a valid API key as already authenticated by key, and it does not keep a browser session for it. The login call returns 200, the page navigates to /?, that navigation has no header and no session, and OctoPrint redirects back to /login/?redirect=/?&permissions=STATUS,SETTINGS_READ. This is the loop in the issue. Without an API key in the printer settings the login works, which is why not everybody sees it.

Change

The hook leaves X-API-Key off requests to /api/login (fetch and XHR). Everything else still gets the header. 5 lines added, 2 changed, one file.

Tested

I did not build the app. I took the hook script out of PrinterWebView.cpp as it is on main and as it is in this branch, and ran each one in a standalone WKWebView that does what the macOS path does (evaluate the script after each finished load, no user script), against fresh local OctoPrint installs with a real user and a real user API key. The harness types the credentials into the login form and clicks Log in.

OctoPrint hook on main hook in this PR
1.10.3 back on the login page OctoPrint UI loads
1.11.7 back on the login page OctoPrint UI loads
2.0.1 back on the login page OctoPrint UI loads

In all six runs a request to /api/version made from the login page before logging in (no session yet) returned 200 through both XHR and fetch, so the header is still added to the other requests. With no API key configured, login worked on all three versions before the change.

Raw output
== OctoPrint 1.10.3 | user API key generated (len 43), GET /api/version with it = 200
   -- hook.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT login-loop
   -- hook_fixed.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT logged-in: OctoPrint | login form: false | final http://127.0.0.1:5056/?#temp
== OctoPrint 1.11.7 | user API key generated (len 43), GET /api/version with it = 200
   -- hook.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT login-loop
   -- hook_fixed.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT logged-in: OctoPrint | login form: false | final http://127.0.0.1:5057/?#temp
== OctoPrint 2.0.1 | user API key generated (len 43), GET /api/version with it = 200
   -- hook.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT login-loop
   -- hook_fixed.js
      PROBE /api/version without session: xhr=200 fetch=200
      RESULT logged-in: OctoPrint | login form: false | final http://127.0.0.1:5055/?#temp

Not tested

  • The built app, on any platform.
  • Windows and Linux, where the same script is also installed with AddUserScript. The script is identical, but I only ran the macOS-style injection.
  • A real printer. The installs were local, with no printer connected.
  • Hosts other than OctoPrint. The path /api/login is OctoPrint's; I did not check whether another supported host uses the same path and expects the key there.

The API key hook adds X-API-Key to every fetch/XHR of the printer page,
including POST /api/login. OctoPrint does not store a session for a request
authenticated by API key, so the login succeeds but no session is kept and
the next navigation lands on the login page again (Snapmaker#1088).
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.

Device tab: OctoPrint login never completes

2 participants