Guide Index Previous Part Download Markdown

#Part 16. Practical Recipes

#Part 16 Section 1. Protected Per-User Secret

On the Windows-hosted Runtime, protect small credential material for the current user:

library CB-Storage

fn main() {
    string path = CB-System.user_data("My ClosedBit App", "credential.cb")
    string protected_value = CB-Security.protect_text("short-lived-or-rotating-secret")
    CB-Storage.write_text(path, protected_value)

    string restored = CB-Security.unprotect_text(CB-Storage.read_text(path))
    println("Credential restored for this Windows account:", len(restored) > 0)
}

Do not use this for arbitrary large files, and never print the restored value.

#Part 16 Section 2. JSON Device Authorization Request

library CB-Networking.HTTP

fn main() {
    string body = "{\"client_id\":\"my-app\",\"scope\":\"profile\"}"
    string response = CB-Networking.HTTP.post_json_response(
        "https://closedbit.com/api/id/device/authorize",
        body
    )
    # response is status + newline + body, including OAuth 400 response bodies.
    println(response)
}

Production code must parse the status, keep the device code secret, show the user code clearly, obey the interval, and stop at expiry.

#Part 16 Section 3. Responsive Native Window

library CB-UI
library CB-Time

fn main() {
    init_window(1280, 800, "ClosedBit App")
    bool dirty = true
    while (true) {
        handle_events()
        if (dirty) {
            clear(0x101419)
            draw_text(40, 40, "ClosedBit", 0x77E6C4, 24)
            swap_buffers()
            dirty = false
        } else {
            time.sleep(8)
        }
        if (should_close()) { break }
    }
    cleanup()
}

For the OS shell, replace hard-coded dimensions with the current display mode and scale all coordinates from a shared layout system.

#Part 16 Section 4. Final Reality Check

Before calling any ClosedBit component “real,” answer four questions with evidence:

1. Which code path is executing, and can it run without the development host? 2. Which hardware, protocols, formats, and failure modes are genuinely supported? 3. Which automated and manual tests passed on the exact artifact being shared? 4. Which limitations remain visible to users?

That standard does not make ClosedBit less ambitious. It is how an ambitious system becomes dependable.