diff --git a/playlet-app/src/manifest b/playlet-app/src/manifest
index 0fe076b2..c2a02ecc 100644
--- a/playlet-app/src/manifest
+++ b/playlet-app/src/manifest
@@ -40,7 +40,7 @@ requires_widevine_version=1.0
playlet_lib_squashfs_remote_url=https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.squashfs.pkg
playlet_lib_zip_remote_url=https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.zip
playlet_lib_zip_embedded_url=pkg:/lib/playlet-lib.zip
-playlet_lib_zip_debug_url=https://gitea.spaghet.io/spaghet/playlet-piped/raw/branch/main/piped/playlet-lib-1795489292.zip
+playlet_lib_zip_debug_url=https://gitea.spaghet.io/spaghet/playlet-piped/raw/branch/main/piped/playlet-lib-1795490592.zip
git_commit_sha=unknown
diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedBackendRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedBackendRouter.bs
index 9e0554d1..24e1cb55 100644
--- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedBackendRouter.bs
+++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedBackendRouter.bs
@@ -129,7 +129,7 @@ namespace Http
end function
function LibBuildTag() as string
- return "PIPED-LIB-17"
+ return "PIPED-LIB-18"
end function
function FetchJson(path as string, params = invalid as object) as dynamic
diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs
index 7deaa6ad..8223dce2 100644
--- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs
+++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs
@@ -70,19 +70,56 @@ namespace Http
' index assignment. The three most recent nonces stay valid, so a
' reloaded page, a rescanned QR, or a health probe can never race
' the real login out of existence; older ones fall out of the ring.
- function EnsureLoginNonce() as string
- if m.loginNonces = invalid
- m.loginNonces = ["", "", "", ""]
- m.loginNonceSlot = 0
+ ' The ring lives in the registry so an app restart (or a redeploy)
+ ' mid-login cannot 403 a callback whose nonce was issued before it:
+ ' the phone may sit on the SSO provider's page for minutes while the
+ ' TV side churns. Load once, save on every mutation.
+ function LoadNonceRing() as void
+ if m.loginNonces <> invalid
+ return
end if
+ m.loginNonces = ["", "", "", ""]
+ m.loginNonceSlot = 0
+ reg = CreateObject("roRegistrySection", "piped_login")
+ if reg.Exists("nonces")
+ stored = reg.Read("nonces").Split("|")
+ slot = Val(ValidString(reg.Read("slot")))
+ if slot >= 0 and slot < 4
+ m.loginNonceSlot = slot
+ end if
+ for i = 0 to 3
+ if i < stored.Count() and stored[i] <> invalid
+ m.loginNonces[i] = ValidString(stored[i])
+ end if
+ end for
+ end if
+ end function
+
+ function SaveNonceRing() as void
+ reg = CreateObject("roRegistrySection", "piped_login")
+ reg.Write("nonces", m.loginNonces[0] + "|" + m.loginNonces[1] + "|" + m.loginNonces[2] + "|" + m.loginNonces[3])
+ reg.Write("slot", Str(m.loginNonceSlot).Replace(" ", ""))
+ reg.Flush()
+ end function
+
+ function ClearNonceRing() as void
+ m.loginNonces = ["", "", "", ""]
+ m.loginNonceSlot = 0
+ m.SaveNonceRing()
+ end function
+
+ function EnsureLoginNonce() as string
+ m.LoadNonceRing()
nonce = CreateObject("roDeviceInfo").GetRandomUUID()
m.loginNonces[m.loginNonceSlot] = nonce
m.loginNonceSlot = (m.loginNonceSlot + 1) mod 4
+ m.SaveNonceRing()
return nonce
end function
function KnownNonceIdx(nonce as string) as integer
- if m.loginNonces = invalid or nonce = ""
+ m.LoadNonceRing()
+ if nonce = ""
return -1
end if
for i = 0 to 3
@@ -93,6 +130,21 @@ namespace Http
return -1
end function
+ ' Put a nonce into the ring (no-op if already present). The SSO
+ ' redirect re-registers the nonce its URL carries so a login that
+ ' started before an app restart - which wipes the in-memory ring -
+ ' can still complete its callback instead of 403ing.
+ function RegisterNonce(nonce as string) as void
+ m.LoadNonceRing()
+ for i = 0 to 3
+ if m.loginNonces[i] = nonce
+ return
+ end if
+ end for
+ m.loginNonces[m.loginNonceSlot] = nonce
+ m.loginNonceSlot = (m.loginNonceSlot + 1) mod 4
+ end function
+
function SetLoggedInProfile(username as string, sessionUuid as string) as void
instance = m.GetInstance()
port = Str(m.server.task.port).Replace(" ", "")
@@ -149,12 +201,15 @@ namespace Http
request = context.request
response = context.response
- nonce = m.EnsureLoginNonce()
reqNonce = ValidString(request.query.nonce)
- if reqNonce <> "" and reqNonce <> nonce
- ' The TV dialog re-rendered; the link the phone holds is stale
+ if reqNonce <> ""
+ ' The phone clicked the link the login page handed out; keep
+ ' that nonce valid for the callback this redirect starts,
+ ' even if the app restarted (ring wiped) in between.
nonce = reqNonce
- m.loginNonce = reqNonce
+ m.RegisterNonce(reqNonce)
+ else
+ nonce = m.EnsureLoginNonce()
end if
instance = m.GetInstance()
@@ -195,8 +250,8 @@ namespace Http
password = ValidString(fields.password)
nonce = ValidString(fields.nonce)
- if nonce <> "" and m.loginNonce <> invalid and nonce <> m.loginNonce
- response.Default(400, m.ErrorPage("Stale login page. Open the code on the TV again."))
+ if nonce <> "" and m.KnownNonceIdx(nonce) < 0
+ response.Default(400, m.ErrorPage(`Stale login page. Start again`))
return true
end if
@@ -269,11 +324,12 @@ namespace Http
' rolling window above). Consume it so each link is single-use.
idx = m.KnownNonceIdx(nonce)
if idx < 0
- response.Default(403, m.ErrorPage("Login link expired or already used. Start again on the TV."))
+ response.Default(403, m.ErrorPage(`Login link expired or already used. Start again`))
return true
end if
' consume: blank the slot (roArray splice ops vary by firmware)
m.loginNonces[idx] = ""
+ m.SaveNonceRing()
' Resolve the username for the session, best effort.
username = m.LookupUsername(token)