From 9c1de5e1cf122c7b94f4cc804e5bb905ee62bc41 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 8 Oct 2026 17:35:22 -0400 Subject: [PATCH] on screen code refactor, playlet lib requires code --- CHANGELOG.md | 4 +- docs/canary-changelog.md | 2 +- .../src/components/Dialog/DialogUtils.bs | 30 ++++++++ .../components/Dialog/OnScreenCodeDialog.bs | 44 +++++++++++ .../components/Dialog/OnScreenCodeDialog.xml | 6 ++ .../Middleware/PlayletLibUrlsRouter.bs | 21 +++++ .../Middleware/RegistryRouter.bs | 76 ++----------------- .../Web/PlayletWebServer/PlayletWebServer.bs | 2 +- .../tests/Dialog/OnScreenCodeDialog.spec.bs | 48 ++++++++++++ playlet-web/src/lib/Api/PlayletApi.ts | 61 +++++++++------ .../PlayletLibVersionControl.svelte | 41 ++++++++-- 11 files changed, 233 insertions(+), 102 deletions(-) create mode 100644 playlet-lib/src/components/Dialog/OnScreenCodeDialog.bs create mode 100644 playlet-lib/src/components/Dialog/OnScreenCodeDialog.xml create mode 100644 playlet-lib/src/source/tests/Dialog/OnScreenCodeDialog.spec.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f720057..5c7076e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing worth mentioning yet. +### Changed + +- Changing Playlet library url (e.g. to test a Canary version) now requires entering a verification code ## [0.48.8] - 2026-10-07 diff --git a/docs/canary-changelog.md b/docs/canary-changelog.md index 5d581b5d..b302fe84 100644 --- a/docs/canary-changelog.md +++ b/docs/canary-changelog.md @@ -6,5 +6,5 @@ To test this release: - Open Playlet web app settings in a browser (`http://ROKU_IP_ADDRESS:8888/index.html#settings`) - Scroll down to `Developer settings` and set `Playlet Library version` to `canary` and click `Apply` -- Restart Playlet +- Enter the code shown on your TV - You can verify that you're testing the `canary` release in the `Info` tab in Playlet or the web app diff --git a/playlet-lib/src/components/Dialog/DialogUtils.bs b/playlet-lib/src/components/Dialog/DialogUtils.bs index a7f5373b..06607f4d 100644 --- a/playlet-lib/src/components/Dialog/DialogUtils.bs +++ b/playlet-lib/src/components/Dialog/DialogUtils.bs @@ -8,6 +8,13 @@ namespace DialogUtils ' Very large messages can lead to execution timeout const MESSAGE_LENGTH_LIMIT = 1000 + const ON_SCREEN_CODE_MIN_INTERVAL_MS = 2000 + + enum OnScreenCodePurpose + ExportRegistry = "ExportRegistry" + SetPlayletLibUrls = "SetPlayletLibUrls" + end enum + function ShowDialogEx(options as object) as object currentDialog = m.top.getScene().dialog alwaysOnTop = false @@ -118,6 +125,29 @@ namespace DialogUtils return dialog end function + function ShowOnScreenCodeDialog(purpose as DialogUtils.OnScreenCodePurpose, title as string) as object + if m._onScreenCodeShownTimer = invalid + m._onScreenCodeShownTimer = CreateObject("roTimespan") + else if m._onScreenCodeShownTimer.TotalMilliseconds() < ON_SCREEN_CODE_MIN_INTERVAL_MS + return invalid + end if + m._onScreenCodeShownTimer.Mark() + + dialog = CreateObject("roSGNode", "OnScreenCodeDialog") + dialog.purpose = purpose + dialog.title = title + m.top.getScene().dialog = dialog + return dialog + end function + + function ConsumeOnScreenCode(purpose as DialogUtils.OnScreenCodePurpose, code as dynamic) as boolean + dialog = m.top.getScene().dialog + if dialog = invalid or dialog.subtype() <> "OnScreenCodeDialog" + return false + end if + return dialog@.Consume(purpose, code) + end function + function ShowDialogOnce(options as object) as object messageId = ValidString(options.messageId) hasMessageId = messageId <> "" diff --git a/playlet-lib/src/components/Dialog/OnScreenCodeDialog.bs b/playlet-lib/src/components/Dialog/OnScreenCodeDialog.bs new file mode 100644 index 00000000..e3f39e2e --- /dev/null +++ b/playlet-lib/src/components/Dialog/OnScreenCodeDialog.bs @@ -0,0 +1,44 @@ +import "pkg:/source/utils/Locale.bs" +import "pkg:/source/utils/Types.bs" + +' Proves the caller can see the TV: a LAN peer can press remote keys over +' ECP, but cannot read the screen. + +const CODE_LIFETIME_MINUTES = 2 + +function Init() + code = Rnd(999999).ToStr() + if code.Len() < 6 + code = string(6 - code.Len(), "0") + code + end if + m.code = code + + ' TODO:P2 localization + m.top.message = [ + `The code is: ${code.Left(3)} ${code.Right(3)}.` + `This code will expire in ${CODE_LIFETIME_MINUTES} minutes.` + "If you did not request this, please close this dialog." + ] + m.top.buttons = [Tr(Locale.Buttons.OK)] + m.top.alwaysOnTop = true + + m.expiryTimer = CreateObject("roSGNode", "Timer") + m.expiryTimer.duration = CODE_LIFETIME_MINUTES * 60 + m.expiryTimer.observeFieldScoped("fire", FuncName(CloseDialog)) + m.expiryTimer.control = "start" + + m.top.observeField("wasClosed", FuncName(OnDialogClosed)) +end function + +function OnDialogClosed() + m.expiryTimer.control = "stop" + m.expiryTimer.unobserveFieldScoped("fire") +end function + +function Consume(purpose as string, code as dynamic) as boolean + expectedCode = m.code + m.code = invalid + CloseDialog() + + return expectedCode <> invalid and purpose = m.top.purpose and IsString(code) and code.Replace(" ", "") = expectedCode +end function diff --git a/playlet-lib/src/components/Dialog/OnScreenCodeDialog.xml b/playlet-lib/src/components/Dialog/OnScreenCodeDialog.xml new file mode 100644 index 00000000..9fa997cc --- /dev/null +++ b/playlet-lib/src/components/Dialog/OnScreenCodeDialog.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs index 565ca0ec..b1ae13aa 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs @@ -1,3 +1,4 @@ +import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Web/WebServer/Http/HttpRouter.bs" import "pkg:/source/utils/RegistryUtils.bs" import "pkg:/source/utils/StringUtils.bs" @@ -29,11 +30,31 @@ namespace Http return true end function + @post("/api/playlet-lib-urls/code") + function ShowSetPlayletLibUrlsCode(context as object) as boolean + response = context.response + + ' TODO:P2 localization + dialog = DialogUtils.ShowOnScreenCodeDialog(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, "Change Playlet library") + if dialog = invalid + response.Default(429, "Too Many Requests") + return true + end if + + response.Default(204, "OK") + return true + end function + @post("/api/playlet-lib-urls") function SetPlayletLibUrls(context as object) as boolean request = context.request response = context.response + if not DialogUtils.ConsumeOnScreenCode(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, request.query.code) + response.Default(400, "Invalid code") + return true + end if + payload = request.BodyJson() if payload = invalid or not IsArray(payload) response.Default(400, "Invalid payload") diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs index 60913812..bb6a69ed 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs @@ -1,100 +1,38 @@ import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Web/WebServer/Http/HttpRouter.bs" import "pkg:/source/utils/RegistryUtils.bs" -import "pkg:/source/utils/StringUtils.bs" -import "pkg:/source/utils/TimeUtils.bs" const REGISTRY_EXPORT_VERSION = 1 namespace Http - ' TODO:P2 localization class RegistryRouter extends HttpRouter - function new(server as object) + function new() super() - - m.task = server.task - m.lastRequestCodeTimeSeconds = 0 - m.requestCode = invalid end function - @get("/api/registry/export/code") + @post("/api/registry/export/code") function ShowExportRegistryCode(context as object) as boolean response = context.response - requestCodeTimeSeconds = TimeUtils.Now().AsSeconds() - - m.requestCode = invalid - scene = m.task.getScene() - dialog = scene.dialog - if dialog <> invalid and dialog.hasField("isExportRegistryCodeDialog") - dialog.close = true - end if - - if requestCodeTimeSeconds - m.lastRequestCodeTimeSeconds < 2 + ' TODO:P2 localization + dialog = DialogUtils.ShowOnScreenCodeDialog(DialogUtils.OnScreenCodePurpose.ExportRegistry, "Export registry") + if dialog = invalid response.Default(429, "Too Many Requests") return true end if - requestCode = rnd(999999).ToStr() - if requestCode.Len() < 6 - requestCode = string(6 - requestCode.Len(), "0") + requestCode - end if - - m.requestCode = requestCode - m.lastRequestCodeTimeSeconds = requestCodeTimeSeconds - requestCode = requestCode.Left(3) + " " + requestCode.Right(3) - - message = `The code to export the registry data is: ${requestCode}. -This code will expire in 2 minutes. -If you did not request to export the registry, please close this dialog.` - dialog = DialogUtils.ShowDialogEx({ - message: message - title: "Export registry" - alwaysOnTop: true - }) - if dialog <> invalid - dialog.addField("isExportRegistryCodeDialog", "boolean", false) - end if - response.Default(204, "OK") return true end function - @get("/api/registry/export") + @post("/api/registry/export") function ExportRegistry(context as object) as boolean request = context.request response = context.response - requestCode = m.requestCode - m.requestCode = invalid - dialog = m.task.getScene().dialog - if dialog = invalid or not dialog.hasField("isExportRegistryCodeDialog") - response.Default(400, "Invalid code") - return true - end if - dialog.close = true - - requestTimeSeconds = TimeUtils.Now().AsSeconds() - if requestTimeSeconds - m.lastRequestCodeTimeSeconds > 120 - response.Default(400, "Invalid code") - return true - end if - - if requestCode = invalid - response.Default(400, "Invalid code") - return true - end if - - code = request.query.code - if StringUtils.IsNullOrEmpty(code) - response.Default(400, "Invalid code") - return true - end if - - code = code.Replace(" ", "") - if code <> requestCode + if not DialogUtils.ConsumeOnScreenCode(DialogUtils.OnScreenCodePurpose.ExportRegistry, request.query.code) response.Default(400, "Invalid code") return true end if diff --git a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs index a1f1c201..70985662 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs @@ -84,7 +84,7 @@ function SetupRoutes(server as object) server.UseRouter(new Http.PlayletLibUrlsRouter(server)) server.UseRouter(new Http.DialRouter(server)) server.UseRouter(new Http.CacheRouter(server)) - server.UseRouter(new Http.RegistryRouter(server)) + server.UseRouter(new Http.RegistryRouter()) server.UseRouter(new Http.PlayletInvidiousBackendRouter(server)) server.UseRouter(new Http.InnertubeRouter(server)) diff --git a/playlet-lib/src/source/tests/Dialog/OnScreenCodeDialog.spec.bs b/playlet-lib/src/source/tests/Dialog/OnScreenCodeDialog.spec.bs new file mode 100644 index 00000000..7e2b0130 --- /dev/null +++ b/playlet-lib/src/source/tests/Dialog/OnScreenCodeDialog.spec.bs @@ -0,0 +1,48 @@ +import "pkg:/components/Dialog/DialogUtils.bs" + +namespace Tests + + @suite + class OnScreenCodeDialogTests extends Rooibos.BaseTestSuite + + protected override function beforeEach() + m.dialog = CreateObject("roSGNode", "OnScreenCodeDialog") + m.dialog.purpose = DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls + match = CreateObject("roRegex", "(\d{3}) (\d{3})", "").Match(m.dialog.message[0]) + m.code = match[1] + match[2] + end function + + @describe("OnScreenCodeDialog.Consume") + + @it("accepts the code shown, and closes") + function _() + displayedCode = `${m.code.Left(3)} ${m.code.Right(3)}` + m.assertTrue(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, displayedCode)) + m.assertTrue(m.dialog.close) + end function + + @it("is single use") + function _() + m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code) + m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code)) + end function + + @it("burns the code on a wrong guess") + function _() + m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, "wrong") + m.assertTrue(m.dialog.close) + m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code)) + end function + + @it("rejects the code for another purpose") + function _() + m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.ExportRegistry, m.code)) + end function + + @it("rejects a code that is not a string") + function _() + m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code.ToInt())) + end function + end class + +end namespace diff --git a/playlet-web/src/lib/Api/PlayletApi.ts b/playlet-web/src/lib/Api/PlayletApi.ts index bf541ee7..02ac5b42 100644 --- a/playlet-web/src/lib/Api/PlayletApi.ts +++ b/playlet-web/src/lib/Api/PlayletApi.ts @@ -193,11 +193,11 @@ export class PlayletApi { } static async showExportRegistryCode() { - await fetch(`${PlayletApi.host()}/api/registry/export/code`); + await PlayletApi.send("POST", `${PlayletApi.host()}/api/registry/export/code`); } static async exportRegistry(code: string) { - const response = await fetch(`${PlayletApi.host()}/api/registry/export?code=${code}`); + const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/registry/export?code=${encodeURIComponent(code)}`); if (!response.ok) { const error = `Error from /api/registry/export: ${response.statusText}`; console.error(error); @@ -218,29 +218,46 @@ export class PlayletApi { return { filename, content }; } - static async setPlayletLibVersion(tag) { - if (tag !== "") { - const urls = [{ - link: `https://github.com/iBicha/playlet/releases/download/${tag}/playlet-lib.squashfs.pkg`, + static playletLibUrlsForTag(tag: string) { + const urls = [{ + link: `https://github.com/iBicha/playlet/releases/download/${tag}/playlet-lib.squashfs.pkg`, + type: 'custom' + }, { + link: `https://github.com/iBicha/playlet/releases/download/${tag}/playlet-lib.zip`, + type: 'custom' + }] + // When an official release is out, it replaces the current canary release. + // To avoid the "not found" error, we fallback to the default "latest" release. + if (tag === "canary") { + urls.push({ + link: `https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.squashfs.pkg`, type: 'custom' }, { - link: `https://github.com/iBicha/playlet/releases/download/${tag}/playlet-lib.zip`, + link: `https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.zip`, type: 'custom' - }] - // When an official release is out, it replaces the current canary release. - // To avoid the "not found" error, we fallback to the default "latest" release. - if (tag === "canary") { - urls.push({ - link: `https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.squashfs.pkg`, - type: 'custom' - }, { - link: `https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.zip`, - type: 'custom' - }) - } - await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls`, urls); - } else { - return await PlayletApi.send("DELETE", `${PlayletApi.host()}/api/playlet-lib-urls`); + }) + } + return urls; + } + + static async showSetPlayletLibUrlsCode() { + const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls/code`); + if (!response.ok) { + throw new Error(`Error from /api/playlet-lib-urls/code: ${response.status} ${await response.text()}`); + } + } + + static async setPlayletLibUrls(urls: { link: string, type: string }[], code: string) { + const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls?code=${encodeURIComponent(code)}`, urls); + if (!response.ok) { + throw new Error(`Error from /api/playlet-lib-urls: ${response.status} ${await response.text()}`); + } + } + + static async resetPlayletLibUrls() { + const response = await PlayletApi.send("DELETE", `${PlayletApi.host()}/api/playlet-lib-urls`); + if (!response.ok) { + throw new Error(`Error from /api/playlet-lib-urls: ${response.status} ${await response.text()}`); } } diff --git a/playlet-web/src/lib/Screens/Settings/SettingControls/PlayletLibVersionControl.svelte b/playlet-web/src/lib/Screens/Settings/SettingControls/PlayletLibVersionControl.svelte index 6cf84f8c..58d20090 100644 --- a/playlet-web/src/lib/Screens/Settings/SettingControls/PlayletLibVersionControl.svelte +++ b/playlet-web/src/lib/Screens/Settings/SettingControls/PlayletLibVersionControl.svelte @@ -97,18 +97,43 @@ const trFn = get(translate); if ( - confirm( + !confirm( trFn( "Are you sure you want to change the Playlet Library version to %1?" - ).replace("%1", version) - ) + - `\n` + - trFn("This will restart Playlet.") + ).replace("%1", version) + + `\n` + + trFn("This will restart Playlet.") + ) ) { - await PlayletApi.setPlayletLibVersion(selectedRelease); - alert(trFn("Playlet Library version changed. Playlet will now restart.")); - await ExternalControlProtocol.restartApp(appId); + return; } + + try { + if (selectedRelease === "") { + await PlayletApi.resetPlayletLibUrls(); + } else { + await PlayletApi.showSetPlayletLibUrlsCode(); + const code = prompt(trFn("Enter the code you see on your Roku device")); + if (!code) { + return; + } + await PlayletApi.setPlayletLibUrls( + PlayletApi.playletLibUrlsForTag(selectedRelease), + code + ); + } + } catch (error) { + console.error(error); + alert( + trFn( + "Failed to change the Playlet Library version. Please try again. See console for details." + ) + ); + return; + } + + alert(trFn("Playlet Library version changed. Playlet will now restart.")); + await ExternalControlProtocol.restartApp(appId); }