diff --git a/playlet-app/src/manifest b/playlet-app/src/manifest
index c2a02ecc..7d47ea6e 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-1795490592.zip
+playlet_lib_zip_debug_url=https://gitea.spaghet.io/spaghet/playlet-piped/raw/branch/main/piped/playlet-lib-1795491297.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 24e1cb55..d9402080 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-18"
+ return "PIPED-LIB-19"
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 8223dce2..5358e62b 100644
--- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs
+++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PipedLoginRouter.bs
@@ -145,6 +145,34 @@ namespace Http
m.loginNonceSlot = (m.loginNonceSlot + 1) mod 4
end function
+ ' The gate script appends token+nonce to the cb URL while the cb
+ ' template already carries a nonce, so the callback can arrive with
+ ' the key twice; Playlet's query parser turns duplicate keys into an
+ ' ARRAY and ValidString(array) is "" -> the nonce looked unknown.
+ function FirstStr(v as dynamic) as string
+ if GetInterface(v, "ifArray") <> invalid
+ if v.Count() > 0
+ return ValidString(v[0])
+ end if
+ return ""
+ end if
+ return ValidString(v)
+ end function
+
+ function ReadLastSession() as string
+ reg = CreateObject("roRegistrySection", "piped_login")
+ if reg.Exists("current")
+ return reg.Read("current")
+ end if
+ return ""
+ end function
+
+ function WriteLastSession(sessionUuid as string) as void
+ reg = CreateObject("roRegistrySection", "piped_login")
+ reg.Write("current", sessionUuid)
+ reg.Flush()
+ end function
+
function SetLoggedInProfile(username as string, sessionUuid as string) as void
instance = m.GetInstance()
port = Str(m.server.task.port).Replace(" ", "")
@@ -201,7 +229,7 @@ namespace Http
request = context.request
response = context.response
- reqNonce = ValidString(request.query.nonce)
+ reqNonce = m.FirstStr(request.query.nonce)
if reqNonce <> ""
' The phone clicked the link the login page handed out; keep
' that nonce valid for the callback this redirect starts,
@@ -248,7 +276,7 @@ namespace Http
fields = m.ParseFormBody(ValidString(request.BodyText()))
username = ValidString(fields.username)
password = ValidString(fields.password)
- nonce = ValidString(fields.nonce)
+ nonce = m.FirstStr(fields.nonce)
if nonce <> "" and m.KnownNonceIdx(nonce) < 0
response.Default(400, m.ErrorPage(`Stale login page. Start again`))
@@ -313,8 +341,8 @@ namespace Http
request = context.request
response = context.response
- token = ValidString(request.query.token)
- nonce = ValidString(request.query.nonce)
+ token = m.FirstStr(request.query.token)
+ nonce = m.FirstStr(request.query.nonce)
if StringUtils.IsNullOrEmpty(token)
response.Default(400, m.ErrorPage("Missing token."))
@@ -324,6 +352,17 @@ namespace Http
' rolling window above). Consume it so each link is single-use.
idx = m.KnownNonceIdx(nonce)
if idx < 0
+ ' A consumed nonce arriving with the SAME session we just
+ ' installed is a retry/replay of a login that already
+ ' succeeded (the gate script can bounce the phone twice) -
+ ' show success, not an error the user cannot act on.
+ if nonce <> "" and token = m.ReadLastSession()
+ response.http_code = 200
+ response.headers["Cache-Control"] = "no-store, max-age=0"
+ response.ContentType("text/html")
+ response.SetBodyDataString(m.PageShell("Signed in", m.SuccessBody(m.LookupUsername(token))))
+ return true
+ end if
response.Default(403, m.ErrorPage(`Login link expired or already used. Start again`))
return true
end if
@@ -335,6 +374,7 @@ namespace Http
username = m.LookupUsername(token)
m.SetLoggedInProfile(username, token)
+ m.WriteLastSession(token)
response.http_code = 200
response.headers["Cache-Control"] = "no-store, max-age=0"