# Plugins: your own sign-in, permissions and rules on a site [中文](plugins.zh-CN.md) A **plugin** adds to a site what only your organisation knows: how people sign in, who may open what, what must be refused, and which other systems to tell when something happens. It is one JavaScript file (or an npm package) that the site loads when it starts. The same plugin runs on the Node site and on Cloudflare. Most people only **use** plugins: pick one, write its settings in a list, restart the site. That is the first half of this page. The second half is for people who **write** one. > **If you are an agent doing this for someone** > > - Ask, never guess: whether to install a plugin at all; which sign-in or permission system to trust; what the > permission service may see about people (each question sends the person's e-mail, groups and the artifact's > title); which department, group or address gets which role. > - A plugin runs with the site's full authority. Install only code the person chose, and say where it comes from. > - Never print a secret (shared secrets, keys, webhook URLs, the owner key). Put it in an environment variable or a > Worker secret and say where it is. > - Run `plugin check` before every restart. On a live site, restart only with the person's go-ahead. > - Check each step with its **Check:** before the next one. After two failures of the same step, stop and report > what you ran and what you saw. ## What a plugin can do, in one screen | You want | Hook | Example in `examples/host-plugins/` | | --- | --- | --- | | People sign in with the company login (a proxy header, your JWT cookie, your SSO) | `auth.authenticate`, `auth.signIn` | `header-auth.mjs`, `company-jwt.mjs` | | Add groups after any sign-in, or refuse some people | `auth.identity` | `company-jwt.mjs` (refuses unlisted departments) | | Permissions from your own service | `authz.role` | `http-authz.mjs` | | Permissions from a table in the settings ("design edits everything") | `authz.role` | `static-groups.mjs`, `company-jwt.mjs` | | Refuse an action by policy (no public links, no downloads, no uploads over 10 MB) | `guard` | `company-jwt.mjs` (no public links) | | Tell Slack or your log what happened | `on.` | `webhook-events.mjs` | | E-mail people when something is shared with them | `on.share.invited` | `invite-email.mjs` | | A link or the company's name on the site's pages | `ui` | `company-jwt.mjs` ("My department" in the account menu) | | A web address of its own (a webhook, a small page) | `routes` | `http-authz.mjs` (`/plugins/http-authz/invalidate`) | | Hand requests for the owner's agent to your own runner | `wake.deliver` | none yet | What a plugin cannot do yet is listed under [What a plugin cannot do](#what-a-plugin-cannot-do). ## Use a plugin (operators) ### The plugin list The site reads one list. Each entry names a plugin (`use`) and gives its settings (`config`): ```json [ { "use": "/srv/open-artifact/examples/host-plugins/header-auth.mjs", "config": { "secret": { "env": "PROXY_SECRET" }, "groupsHeader": "x-forwarded-groups" } }, { "use": "/srv/open-artifact/examples/host-plugins/static-groups.mjs", "config": { "groups": { "design": "bob@example.com, carol@example.com", "staff": "@example.com" }, "roles": { "staff": "viewer", "design": "editor" } }, "cacheSeconds": 300 }, { "use": "/srv/open-artifact/examples/host-plugins/webhook-events.mjs", "config": { "url": { "env": "SLACK_WEBHOOK_URL" }, "format": "slack" }, "optional": true, "timeoutMs": 2000 } ] ``` | Field | Meaning | | --- | --- | | `use` | A path to the plugin's file, or an npm package name. On Cloudflare: the name it is bundled under. | | `config` | The plugin's settings. Each plugin lists its own (`plugin list` shows the names). | | `enabled` | `false` keeps the entry but does not run it. | | `optional` | When it cannot load or its settings are wrong, the site starts without it (and logs why) instead of refusing to start. Use it for nice-to-have plugins, never for sign-in or permissions. | | `timeoutMs` | How long each call may take, 1 to 60000 ms. Default 5000. | | `cacheSeconds` | How long the site reuses a permission answer, 0 to 86400. Wins over the plugin's own default. | A bare string (`"./audit.mjs"`) is an entry with only `use`. Order matters for sign-in (the first plugin that knows the person wins) and for roles (each plugin sees the answer of the one before). ### Where the list lives | Site | The list | Which wins | | --- | --- | --- | | Node | the environment variable `OA_PLUGINS` (JSON), or the file `OA_PLUGINS_FILE` (default `/plugins.json`) | `OA_PLUGINS` when it is set; the file is then ignored | | Cloudflare | the Worker variable or secret `OA_PLUGINS` | there is only this one | Relative paths in the file start at the file's folder; in `OA_PLUGINS`, at the folder the site starts in. Use full paths and there is nothing to work out. A package name is looked for in `/plugins/node_modules` (install with `npm install --prefix /plugins `), then where the site itself finds packages. A plugin can import `@open-artifact/host-plugin` (the helpers; `header-auth.mjs`, `http-authz.mjs` and `company-jwt.mjs` do) from any folder, without installing it: the site hands every plugin its own copy of that package (on Node when the plugin loads, on Cloudflare when the Worker is built). So any plugin file, the examples included, can be copied anywhere, the data folder too, and listed by its path there. ### Settings and secrets Never write a secret into the list. Give any setting as `{"env": "NAME"}` and the site reads it from that environment variable (on Cloudflare: a Worker variable or secret of that name). The text is read as the setting's type: a `map` or `json` setting as JSON, a `string[]` as a comma-separated list or a JSON array. The site checks every setting when it starts. A misspelt name stops it, and says what was meant: ``` ! Plugin "header-auth": unknown setting "emailHeadr". Did you mean "emailHeader"? Its settings are: emailHeader, nameHeader, groupsHeader, secretHeader, secret, jwtHeader, jwksUrl, issuer, audience. ``` A secret written into the list is only a warning, so the site still starts; move it out: ``` ! Plugin "header-auth", setting "secret": the secret is written into the configuration; give it as {"env": "NAME"} to keep it out of the file. ``` Secrets never appear in `plugin list`, the admin API or the logs. ### Commands on the Node site Run from the Open Artifact folder, with the same `OA_DATA_DIR` the site uses. (The messages say `open-artifact-host`; from the source folder that command is `node packages/host/src/node/main.ts`.) ``` node packages/host/src/node/main.ts plugin list the plugins, their hooks and settings, and what is wrong node packages/host/src/node/main.ts plugin check the same; exits 1 when the site would not start node packages/host/src/node/main.ts plugin role --as [--json] that person's role and what decided it node packages/host/src/node/main.ts plugin add [--config ] [--optional] [--disabled] [--timeout-ms ] [--cache-seconds ] node packages/host/src/node/main.ts plugin remove|enable|disable node packages/host/src/node/main.ts plugin set [=…] [--secret =…] [--timeout-ms ] [--cache-seconds ] [--optional | --required] node packages/host/src/node/main.ts plugin unset … node packages/host/src/node/main.ts audit [--artifact ] [--actor ] [--action ] [--before