feat(console): read a QR code from a PNG with a pure decoder

This commit is contained in:
imSp4rky
2026-11-27 12:40:41 -07:00
parent cfe6685dfc
commit 8a93b4f49a
8 changed files with 519 additions and 2 deletions
+20 -1
View File
@@ -279,7 +279,7 @@ session shows it. A REST download job shows it in its `notice` field until the w
For a login that the user scans with a phone, give the text of the QR code as `qr`. Both
`wait_for_approval` and `request_input` take it. The terminal that shows the message draws
the code under it, on the client for a remote session. Give the text that the login API puts in
the code, such as the login URL, not an image. Keep the URL or code in the message too: a
the code, such as the login URL. Keep the URL or code in the message too: a
terminal that cannot draw the code, or a phone that cannot read it, still shows the text.
`EXAMPLE.scan_login` in `unshackle/services/EXAMPLE/` shows the whole flow.
@@ -292,6 +292,25 @@ token = self.wait_for_approval(
)
```
When the login API gives only a PNG of the QR code, read the text from the image with
`read_qr_image` from `unshackle.core.console`, and give that text as `qr`. It reads a clean,
axis-aligned code, dark on a light background, as a login API renders it. Decode a base64 data URI
to bytes first. It does no error correction, so a code with a logo or other mark over it does not
read. It returns `None` for such a code, for an interlaced PNG or another image format, and for a
photo of a code. The terminal then shows only the message.
```python
from unshackle.core.console import read_qr_image
png = self.session.get(login["qr_image_url"]).content
token = self.wait_for_approval(
f"Scan the code with the app, or open {login_url}",
check=lambda: self.poll_login(session_id),
timeout=expires_in,
qr=read_qr_image(png),
)
```
The wait sees a cancel only between two `check` calls, so `check` must return within a few
seconds. A `check` that reads a socket keeps the socket open across calls and returns `None`
after each short idle wait.
+2
View File
@@ -96,6 +96,8 @@ These are abstract. A service will not load unless it defines all three:
another device, such as a pair code. A remote-dl session shows the message as a log
line, and a REST download job shows it as the job's `notice`. Both methods take `qr`, the
text of a QR code to scan, and the terminal or UI that shows the user the message draws it.
When the login API gives only a PNG, `read_qr_image(data)` in `unshackle.core.console`
gives the text.
- **Event callbacks**: `on_segment_downloaded`, `on_track_downloaded`,
`on_track_decrypted`, `on_track_repacked`, `on_track_multiplex` let a service react to
pipeline events.