Skip to main content

token_init_script

Function token_init_script 

Source
pub fn token_init_script(
    app: &AppIdentity,
    origin: &str,
    token: &AccessToken,
) -> String
Expand description

The initialization script that hands the WebView its access token, as the window global the app’s SPA reads (AppIdentity::token_global).

Shared by the desktop GUI (wry’s initialization script, the gui module) and a mobile host (a WKUserScript at document start — see ledgeline-ffi).

This is how the token reaches the SPA WITHOUT appearing in any response body: the shell is served unauthenticated, so anything that can reach loopback can read it, and it used to carry the token for exactly that reason. wry runs this at document start, before the page’s own scripts, outside the page’s CSP — but on EVERY document the main frame loads, whatever it is. So the script checks two things before it hands anything over:

  • location.origin is ours exactly. The navigation handler already keeps the WebView on our origin; this means a document from anywhere else would still get nothing, should one ever load.
  • location.pathname is under the app’s SPA base (AppIdentity::spa_base, the same rule as in_spa), so a page that shares the origin but is not the SPA (the invoicer’s MBR pages) gets nothing either, even if it reached the main frame. An app whose SPA is the whole origin (/) gets the origin check alone, which keeps its script, the iOS host’s included, exactly as it was.

The values are serialized as JSON string literals rather than interpolated raw. AccessToken::parse already restricts the token to a safe character set, and the origin and base are ours, so this is belt and braces — but it costs nothing and does not depend on any of them staying true.