Skip to main content

secure_flow 0.4.0 copy "secure_flow: ^0.4.0" to clipboard
secure_flow: ^0.4.0 copied to clipboard

Pure-Dart browser round trips for desktop apps and command line tools: OAuth redirects, reCAPTCHA (v2 and Enterprise) and code entry answered over a hardened one-shot loopback server, reCAPTCHA in the [...]

0.4.0 #

  • PKCE (RFC 7636, S256) is built in: OAuthFlow.authorization and OAuthFlow.discovery take pkce: true (off by default). The flow makes a verifier of 32 secure random bytes (base64url, 43 characters), adds code_challenge and code_challenge_method=S256 to the authorization URL the caller builds (an ArgumentError if the caller set them), and returns the verifier on the new OAuthRedirect.codeVerifier; never in a URL, page, error or toString. With discovery, a document whose code_challenge_methods_supported lacks S256 fails the run before the browser opens; a document without the field is accepted. A provider app's redirect has no verifier. Pkce.generate() and Pkce.challengeOf(verifier) are public for apps that build their own request. secure_flow still has no dependencies: it carries its own SHA-256 (FIPS 180-4 vectors, VM and web).
  • Breaking: SecureFlowPurpose.deviceCode, the purpose of a device sign-in. A switch over SecureFlowPurpose without a default (or a wildcard) no longer compiles until it handles the new case.
  • FacebookDeviceFlow: Facebook Login for Devices, for desktop apps and command line tools. POST graph.facebook.com/<facebookGraphApiVersion>/device/login with access_token=<app id>|<client token> and scope; the user code and the verification URL go to a DeviceCodePresenter, and polling of device/login_status starts at once (it does not wait for the presenter to return; an error of the presenter ends the run). Polls come at Facebook's interval, never more often than every five seconds, plus five seconds for good after each slow_down. The result is a FacebookDeviceToken. A code that expires (Facebook's subcode or its expires_in) is a SecureFlowTimeout, the app's cancel or the run's timeout a SecureFlowCancelled or SecureFlowTimeout; on each, the wait and the expiry timer are cancelled, the request in flight is aborted, and nothing is sent afterwards (the flow overrides SecureFlow.presentDirectlyUntil). A granted sign-in leaves no timer behind. Facebook's documented errors are matched on the subcode, or the code without one (1349174 / 31 pending, 1349172 / 17 too fast, 1349152 / 463 expired). An interval above 60 seconds or a lifetime above one hour is refused as an unusable answer. Other failures are a FacebookDeviceFailure (network, status, response, deviceLoginDisabled for "Invalid API method" (error 3), refused) whose message holds the HTTP status and Facebook's numeric codes, never the body or a token. No API takes a secret parameter: the value must be <app id>|<client token>, and one without exactly one | is refused. An app access token (<app id>|<app secret>) has the same shape, so passing one is a misuse the library cannot detect. The Graph API version is pinned (facebookGraphApiVersion, v25.0). The HTTP layer is dart:io's HttpClient and can be replaced (post, a DeviceHttpPost); on the web there is none, so run fails with an UnsupportedError unless post is given.
  • AppleWebFlow and AppleRelay: Sign in with Apple on Windows, Linux and the command line, through a relay the app supplies (an https return URL of its Services ID; Apple never redirects to 127.0.0.1). There is no AppleWebFlow without a relay. The result is Apple's tokens (AppleWebResult: the ID token, the authorization code, the redirect URI it was issued for, and AppleWebUser with the first-time name and email); the app decides what to do with them. The flow makes its own listener path and secret (32 random bytes each, base64url without padding) and sends state=<port>.<path>.<secret>, response_type=code id_token, the hashed nonce (64 lowercase hex digits), and always response_mode=form_post, so that no token is ever in a URL. Its listener takes only a form of the relay's origin (or one without Origin) carrying the secret; the first one decides the run (no retry). It checks iss, aud (the Services ID), exp (60 seconds of skew) and the nonce (constant time), not the signature. Apple's cancel is a SecureFlowCancelled; other failures an AppleWebFailure (response, refused, claims, nonceMismatch) whose message holds no token, code or nonce. A flow runs once. Presenters: the system browser and the console. The README has the relay's rules, what the relay sees, and a paragraph on shared machines in "What it does not protect against".
  • SecureAnswer.crossOriginForm(path:, allowedOrigin:): a form answer that a page of one other https origin may post. The origin must be https with a host and no user info, path, query or fragment (ArgumentError otherwise), and is compared as Uri.origin; Origin: null is never taken. SecureAnswer.allowedOrigin is null for the other answers, whose behaviour does not change.
  • A request the loopback server refuses (400) names the check that refused it, at the end of a message page and in the x-secure-flow-refusal header: host, origin, content-type or state. No value of the request or of the run is in it.
  • DeviceCode, DeviceCodePresenter and DeviceCodeCallbackPresenter (kind other): a code for the person to type on another device; DeviceCode.verificationUriWithCode is the page with the code filled in, for a QR code. ConsolePresenter is a DeviceCodePresenter too: it writes one line with the page and the code.
  • Dev dependency: fake_async.
  • OAuthAppRequest(abandoned:) and OAuthAppRequest.abandoned: a future that completes when SecureFlow.run gives up on a provider-app sign-in because its timeout or cancel came first (never after a result or an error), so that the presenter can close its sheet and free itself for the next sign-in. It completes before run throws its SecureFlowTimeout or SecureFlowCancelled, so the presenter is free by the time the caller retries.
  • SecureFlow.presentDirectlyUntil(presenter, abandoned:), which run now calls instead of presentDirectly. Its default calls presentDirectly, so a flow that extends SecureFlow and was written for 0.3.0 works unchanged. Breaking for a class that implements SecureFlow (rather than extending it): it must add presentDirectlyUntil. OAuthFlow and map pass the signal on.
  • ProviderAppException: an abstract base class for the errors of provider apps, with a coarse kind, a ProviderAppExceptionKind (nonceMismatch, invalidRequest, other; other by default), for callers to map. A new kind comes only in a breaking version.
  • SecureFlowCancelled also covers a person's cancel in a provider app.
  • Documentation: the README and the RecaptchaFlow dartdoc record the real runs of 2026-11-28 (Google through OAuthFlow.discovery, the discovery cache, GitHub, a reCAPTCHA v2 token on localhost); the README warns that CallbackPresenter defaults to kind: systemBrowser, so an embedded web view must pass its true kind; and it notes that the Windows 413 drain fix (#131) has not been run on Windows since the fix.
  • Docs: the public doc comments no longer carry RFC and OpenID section numbers; the streams that callers pass in (ConsolePresenter.input, the body of OidcHttpResponse and DeviceHttpResponse) say they are listened to once and do not replay. Doc-only references use @docImport instead of imports.

0.3.0 #

  • Breaking: OAuthFlow's unnamed constructor is now OAuthFlow.authorization, with the same arguments. Migration: replace OAuthFlow( with OAuthFlow.authorization(. The authorizationUrl field is gone (the builder is private).

  • OAuthFlow.discovery(providerId:, issuer:, authorizationUrl:, state:, discovery:, ...): an OpenID Connect sign-in whose endpoints come from the issuer's discovery document. run fetches it before the loopback server starts, within the run's timeout and cancel (a provider app needs none, and none is fetched), and calls authorizationUrl(configuration, redirectUri); the caller still builds the query. OAuthRedirect.configuration carries the document, so that map can exchange the code at its tokenEndpoint.

  • OidcDiscovery: fetches and caches discovery documents. The issuer must be https (no user info, query or fragment), the document's issuer must equal it exactly (OpenID Connect Discovery 1.0, section 4.3), and its authorization_endpoint and token_endpoint must be https. One GET, no redirects followed, a 64 KiB body cap, a 30 second timeout (timeout), and cancel. Freshness: Cache-Control: max-age, or else Expires minus Date, minus Age; no-store is not stored, no-cache is revalidated every time. A stale document is revalidated with one conditional GET (If-None-Match, If-Modified-Since): a 304 renews it, a 200 replaces it after the same checks, and a failure throws OidcDiscoveryException (a stale copy is never returned). A cached document is checked again whenever it is used. OidcConfiguration has issuer, authorizationEndpoint, tokenEndpoint and the whole json.

  • Caches (OidcConfigurationCache, keyed by issuer): MemoryOidcConfigurationCache (the default) and FileOidcConfigurationCache(path), one JSON file at a path the app chooses (a corrupt file is a miss; writes go to a temporary file renamed over it). The HTTP layer is dart:io's HttpClient and can be replaced (httpGet, an OidcHttpGet). On the web there is no default HTTP layer (pass httpGet), and the file cache throws an UnsupportedError; the library still compiles there.

  • SecureFlow.prepare: the step before the loopback server starts (the fetch of OAuthFlow.discovery), bounded by the run's timeout and cancel. It returns the flow itself by default.

  • Changed behaviour: the timeout and the cancel of SecureFlow.run now bound the work of map (and of parse) on the loopback server too, as they already did with a direct presenter. When either comes while an answer is being parsed, run throws SecureFlowTimeout or SecureFlowCancelled at once, the browser that sent the answer gets the failed page, and the late result is dropped (the work itself is not stopped: give a code exchange its own, shorter timeout). Before, the timeout waited for the parser, so a hanging exchange hung run, while the cancel did not wait; the dartdoc said neither applied.

  • ConsolePresenter's timeout hint no longer tells the person to forward the port of the run that just ended and open its URL again (that server is closed, and the next run gets a new port): it says to run the flow again and forward the port of the new URL while it waits.

  • Docs: the security scope names other local programs and, on Linux, other local users (the URL is in the opener's and a freshly started browser's arguments, which /proc shows to every local account by default), with hidepid as a mitigation. reCAPTCHA keys, v2 and Enterprise, do not allow localhost by default: add it to the key's domain list.

  • The browser tests (test/browser) run in CI, in Chrome with dart2js and dart2wasm.

0.2.0 #

  • Breaking for exhaustive switch statements: SecurePresenterKind has the new value inPage, and RecaptchaAction has new values for the Enterprise actions.

  • RecaptchaFlow.enterprise(siteKey:, action:): a reCAPTCHA Enterprise token for an action. Its loopback page loads enterprise.js?render=<site key> and posts the token of grecaptcha.enterprise.execute as soon as it is ready. The action is checked (letters, digits, / and _).

  • RecaptchaFlow takes the size (RecaptchaSize: normal, compact, invisible) and theme (RecaptchaTheme) of the v2 widget; the loopback page starts an invisible widget at once. Its script keeps its names out of the page's global scope.

  • InPageRecaptchaPresenter (web builds, dart:js_interop only): renders the v2 widget into an element of the app's page (invisible without a container), or runs the Enterprise execute, and returns the token without the loopback server; render, clear, and onVerified/onExpired/onError callbacks. The Enterprise script loads once per site key (also next to App Check's), and again after a failed execute; a script that fails or times out loses its tag; without a container the widget's element is removed after each check; a newer check fails the waiting one (superseded); scriptAddress replaces Google's address. SecureEnvironment.current() offers one on the web, and RecaptchaFlow ranks it first, so a reCAPTCHA runs in the page there. Tested in Chrome against a fake grecaptcha.

  • New presenter kind SecurePresenterKind.inPage and interface RecaptchaPresenter (a presenter that returns a reCAPTCHA token itself; RecaptchaFlow takes one of any kind it ranks). CallbackPresenter refuses the inPage kind.

  • RecaptchaFailure (scriptLoad, loadTimeout, widget, noToken, superseded) for a presenter that could not make a token; RecaptchaFlow throws it for an empty token of a RecaptchaPresenter.

0.1.0 #

First release.

  • SecureFlow<T>: a self-contained browser round trip (what to open or serve, which request answers it, its host, how the answer becomes a T) that ranks the presenter kinds it accepts and runs itself: flow.run(presenter:, environment:, timeout:, cancel:) takes the best presenter of the environment, or checks the given one and throws SecureFlowRefused when it does not accept it. map runs work, such as a code exchange, before the browser is told the outcome. Third parties can write flows and presenters.
  • Flows: OAuthFlow (result OAuthResult: the redirect of a code request on http://127.0.0.1:<port>, or the tokens of a provider app; ranks provider app, system browser, in-app browser, console), RecaptchaFlow (a reCAPTCHA v2 token from a page on http://localhost:<port>; ranks embedded web view, system browser, console) and CodeEntryFlow (a code typed at the terminal or into a form; ranks console, then the system browser).
  • Presenters with a SecurePresenterKind (providerApp, systemBrowser, inAppBrowser, embeddedWebView, console, other) and capability interfaces (LoopbackPresenter, PromptPresenter, ProviderAppPresenter): SystemBrowserPresenter, ConsolePresenter (only the URL, on stderr by default; prompts read the injected input, stdin by default) and CallbackPresenter (the app shows the URL and declares the kind). SecureEnvironment lists what is on offer: registered presenters, the system browser with a display, the console with a terminal.
  • The loopback server: binds 127.0.0.1 (and ::1 for localhost flows), random path, the page only for a GET with the state, single-use state compared in constant time, one accepted answer, Host checked against the flow's host, Origin checked for form answers, 64 KiB body limit, a CSP nonce in every response, timeout, cancel, closed on every exit; pages follow the system's light or dark setting.
  • Typed errors: SecureFlowTimeout (with an ssh -L hint from ConsolePresenter), SecureFlowCancelled, SecureFlowRefused, SecureFlowNoPresenter.
0
likes
150
points
154
downloads

Documentation

API reference

Publisher

verified publisherupcode.dev

Weekly Downloads

Pure-Dart browser round trips for desktop apps and command line tools: OAuth redirects, reCAPTCHA (v2 and Enterprise) and code entry answered over a hardened one-shot loopback server, reCAPTCHA in the page of a web app, and Facebook Login for Devices.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#oauth #authentication #cli #desktop

License

Apache-2.0 (license)

More

Packages that depend on secure_flow