owner

tape
import owner from "owner";
import ui from "ui";
import io from "io";

var g_backups: bool = true;

fn backups_off_answered(r: owner.OwnerResult) {
    if (r == owner.OwnerResult.Verified) {
        g_backups = false;
        io.println("backups are off");
    } else if (r == owner.OwnerResult.Unavailable) {
        io.println("no way to ask here: ask for the account's password instead");
    }
    // Refused: the user cancelled; nothing changes
}

fn turn_backups_off() {
    owner.verify("Turn off backups of your messages", backups_off_answered);
}

Call turn_backups_off from a button’s handler. The system shows its own dialog, and the answer comes to backups_off_answered later.

Asking

verify(reason, on_done) asks the system to check that the person at the computer is its owner: Windows Hello (a face, a finger or the PIN) on Windows, Touch ID or the account password on macOS. reason is shown in the dialog: say what is about to happen (“Add a new device to your account”). It must not be empty.

verify returns at once and never waits for the user. The answer comes to on_done exactly once, as an OwnerResult:

Verifiedthe system checked the person: the owner. The only yes
Refusedthe user cancelled or failed the check; the program stays where it was and may ask again
Unavailablethere is no way to ask here (Windows Hello not set up, no password or biometrics, no screen to ask on, Linux). Never a yes: your program decides what to do instead

When the system answers, on_done runs in the ui loop, between events, like other handlers. Some answers need no asking, and those come before verify returns:

  • only one verify at a time: a second while one is pending is Refused at once (the first one still gets its own answer);
  • Unavailable at once where there is no way to ask (Linux, or the system could not show its dialog).

So don’t write code after verify that assumes on_done hasn’t run yet. The handler is a plain function: keep what the action needs (which device, which setting) in your module’s state until the answer comes. On Windows the dialog belongs to the program’s window, so it opens in front of it. A program with no ui loop calls owner.poll() itself.

available() says whether verify can ask here. It never shows a dialog, but on Windows it can take a moment: ask once when the screen opens, to show or hide a “Require Windows Hello” option, not on every frame. true is not a promise; verify may still answer Unavailable if the user turns Windows Hello off in between.

The answer is a gate in your program, not a key: nothing is unlocked by it. Keep it for what the phones guard the same way (adding a device to an account, turning backups off, showing a recovery key).

Testing your program

Your tests can’t answer a system dialog, and must not put one up. simulate(true) makes the module stand in for the system: available() is true and verify waits until your test calls answer(r), which calls on_done before it returns. simulate(true, false) acts as a computer with no way to ask (available() false, verify answering Unavailable at once). simulate(false) goes back to the system; a verify still waiting is answered Refused.

tape
test "backups go off only for the owner" {
    owner.simulate(true);
    turn_backups_off();
    @assert(g_backups);                                  // still asking
    owner.answer(owner.OwnerResult.Verified);
    @assert(!g_backups);
    owner.simulate(false);
}

Platforms

WindowsWindows Hello (UserConsentVerifier), the dialog belonging to the program’s window
macOSLocalAuthentication (LAContext, the owner policy: Touch ID, a paired watch or the password)
Linux, UusiUnavailable: available() is false

It uses the desktop shell library (shell100), like notify and dialog.

Last modified: