notesy::accounts
Signing the user in to a web service, the way desktop apps do: notesy
opens the service's sign-in page in the browser, the user says yes there,
and the browser comes back to notesy with a code that notesy trades for
tokens (OAuth 2.0 for native apps, RFC 8252, with PKCE). Takes the
accounts permission, and the service in the plugin's [[accounts]]
(plugin.toml).
tomlpermissions = ["accounts"]
[net]
hosts = ["github.com", "api.github.com"]
[[accounts]]
id = "github"
name = "GitHub"
authorize_url = "https://github.com/login/oauth/authorize"
token_url = "https://github.com/login/oauth/access_token"
client_id = "Iv1.0123456789abcdef"
scopes = ["read:user"]
hosts = ["api.github.com"]
rustuse notesy::{accounts, commands, log, net};
pub fn ready() {
commands::add("sign-in", "Sign in to GitHub", |command| {
accounts::sign_in("github", |result| {
let signed_in = result?;
log::info(`signed in, with ${signed_in.scope}`);
});
});
commands::add("whoami", "Who am I on GitHub", |command| {
net::fetch(#{ url: "https://api.github.com/user", account: "github" }, |result| {
log::info(result?.text);
});
});
}
| Function | What it does |
|---|---|
accounts::sign_in(id, then) |
Opens the sign-in page for account id in the browser, then calls then(result) once the user's back: Ok(#{ account, scope, expires_at }), or Err(why) (they said no, or didn't finish within five minutes). One sign-in at a time. |
accounts::signed_in(id) |
Whether there's a token for account id. |
accounts::sign_out(id) |
Forgets account id's tokens. |
expires_at is when the token runs out, in seconds since 1970, when the
service said.
#Using the account
A request that names the account (account: "github" in
net::fetch) goes with its token as Authorization, when it's
to one of the account's own sites: its sign-in page's host, its token
address's host, or one in its hosts (api.github.com above). Naming the
account in a request to any other site is an error. When the token has
run out and the service gave a refresh token, notesy refreshes it first.
The script never sees a token: they're kept in the plugin's
secrets, under names only notesy can read.
#What notesy checks
- The sign-in page and the token address have to be on hosts the plugin names; notesy checks when it reads the manifest, and again when the script signs in.
- The token goes only to the account's own sites, never to another site the plugin names, nor on to where a redirect sends it.
- The browser comes back to a port on this computer only, open just for
that sign-in, and counts only with the
statenotesy made for it. - The code is traded with the PKCE verifier notesy made, so a code taken on the way is no use to anyone else.
- Removing the plugin with its data deletes its tokens.
Every page
- Overview
- plugin.toml: Every field of plugin.toml
- Permissions: What a plugin can ask for, when notesy asks, and what changes it
- Scripts: How a script runs: its lifecycle, events, limits and errors
- Packing and installing: Making, checking, signing, packing and installing
- notesy::log: Lines for its log on the Plugins page
- notesy::events: Hearing what happens, and every event
- notesy::commands: Adding commands, and running notesy's
- notesy::store: Keeping its own data
- notesy::settings: Reading its settings
- notesy::secrets: Keys and tokens, in the system keychain
- notesy::notes: Reading and changing the vault's notes
- notesy::editor: The note in front
- notesy::files: A folder of its own
- notesy::links: Opening pages in the browser
- notesy::clipboard: Copying and pasting
- notesy::view: What panels, tabs and sections show
- notesy::panels and notesy::views: Adding side panels and tabs
- notesy::sections: Sidebar sections
- notesy::menus: Items in notesy's right-click menus
- notesy::status: Status bar items
- notesy::notices: Notices
- notesy::dialogs: Asking in a dialog, notesy's kinds or its own
- notesy::cards: Its part of the tree's hover cards
- notesy::boards: Boards' cards and arrows, and kinds of card of its own
- notesy::blocks: Drawing its fenced blocks
- notesy::icons: Drawing its own icons
- notesy::net: Requests to the sites it names
- notesy::accounts: Signing in to a service
- notesy::json: Reading and writing JSON
- notesy::toml: Reading and writing TOML
- notesy::yaml: Reading and writing YAML, and a note's front matter
- notesy::time: Now, written in the user's time zone, dates read, how long ago
- notesy::math: Trigonometry and the like, for drawing
- Names: Icon, Color, Tone, Side, Method, Command, Menu, Sidebar, Event: Notesy's names as enums
- notesy::preview: Its own screens for notesy preview
- notesy::perf: Timing its own work
- notesy::tex: Math macros for every note's formulas