=== Faable Auth ===
Contributors: boyander
Tags: login, sso, openid connect, oidc, authentication
Requires at least: 6.4
Tested up to: 7.1
Requires PHP: 8.1
Stable tag: 1.1.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Sign in to WordPress with Faable Auth (OpenID Connect). Roles come from Faable Auth, and headless front-ends use machine-to-machine tokens.

== Description ==

Faable Auth makes your site an OpenID Connect client of your Faable Auth tenant:

* **Sign in with Faable only.** wp-login.php shows a "Log in with Faable" button instead of the username and password form. Password resets, registration, XML-RPC authentication and Application Passwords are turned off.
* **Accounts without local passwords.** Users are created on their first login, without a usable local password.
* **Roles from Faable Auth, re-synced on every login.** Users get the WordPress role their Faable Auth role maps to (administrator, editor, author, contributor, subscriber by default; the map is configurable). A user without a mapped role gets no account and sees why.
* **Headless front-ends.** The REST API accepts Faable Auth machine-to-machine (client_credentials) access tokens instead of Basic Auth with an Application Password.
* **Emergency access from day one** (see below).

Works on any host, with any Faable Auth plan (including Free).

= Emergency access =

If Faable Auth cannot be reached, nobody can log in. There are two ways in, and you should know them before you need them:

1. **Break glass.** Add this line to `wp-config.php`:

    define( 'FAABLE_AUTH_BREAK_GLASS', true );

   The username and password form, "Lost your password?" and password resets come back, next to the Faable button. Remove the line once Faable Auth is back. Users created by the plugin have no local password: they use "Lost your password?" to set one, or an administrator sets it with `wp user update <user> --user_pass=<password>`.

2. **One-time login link.** With WP-CLI:

    wp faable-auth login-link <user>

   prints a URL that logs in as that user once, within 10 minutes. On Faable hosting, run it as a Task. Opening the URL shows a confirmation button, so link previews in chat apps do not use it up.

`wp faable-auth status` shows the current mode and configuration (never the secret).

= Configuration =

**On Faable hosting** nothing needs to be set and there is nothing to connect: when the site is installed, Faable creates its OIDC client in the project's Faable Auth tenant, gives the site these environment variables and loads the plugin as a must-use plugin from its image. While the site is hosted on Faable the plugin cannot be deactivated or disconnected: its settings page says "Managed by Faable" and shows the connection read-only. The role map, the headless (REST) options and federated logout stay editable.

* `FAABLE_AUTH_DOMAIN`: the tenant URL, e.g. `https://acme.auth.faable.link` (or its custom domain). It is the issuer of the tokens, without a trailing slash.
* `FAABLE_AUTH_CLIENT_ID`, `FAABLE_AUTH_CLIENT_SECRET`: the site's OIDC client in that tenant.

Optional: `FAABLE_AUTH_ROLE_MAP` (JSON or `faable_role = wp_role` lines), `FAABLE_AUTH_AUDIENCE` (an API whose ID tokens include roles), `FAABLE_AUTH_REST_AUDIENCE`, `FAABLE_AUTH_REST_USER`, `FAABLE_AUTH_REST_CLIENTS`, `FAABLE_AUTH_REST_SCOPE` (machine-to-machine REST), `FAABLE_AUTH_FEDERATED_LOGOUT`. Each can also be a PHP constant in `wp-config.php`. A value set this way wins over the settings page and shows there read-only.

**Anywhere else**, go to Settings → Faable Auth and press **Connect with Faable**. You log in to Faable, pick the project and the Auth tenant, and the site comes back connected. If you cannot use that, create a web client in your tenant with this redirect URI (the settings page shows it) and paste the domain, client ID and secret in the manual section:

    https://<your-wordpress-address>/wp-login.php?faable-auth=callback

Until the site is connected, the normal WordPress login stays on, so the administrator who connects it can log in.

= Roles =

The roles of a user are read from the ID token's `roles` claim when the site requests an audience (`FAABLE_AUTH_AUDIENCE`) whose API includes roles in ID tokens. Otherwise the plugin reads them from your tenant's Management API (`GET /user/:id/roles`) with a client_credentials token limited to `read:rolemembers`, cached for its lifetime (about one machine-to-machine token a day). If roles cannot be read, the login is refused.

Roles are managed in the project's Faable Auth tenant: being a member of the Faable project gives no role on the site.

= Users added in WordPress =

A user added in Users → Add New (or with `wp user create`) is also created in the Faable Auth tenant, with the password typed in WordPress (its WordPress 6.8+ hash is imported, so the same password works on the Faable login) and the Faable Auth role that maps to its WordPress role. An email the tenant already has is linked to that user instead, and its password is left as it is. A role changed in WordPress is changed in Faable Auth too, and deleting a user removes its mapped roles there (the Faable Auth user stays: it may use other apps of the tenant).

This needs the site's client to hold the Management API scopes `create:users read:roles create:roles create:rolemembers delete:rolemembers`. Without them WordPress works as before: the user's profile and the users list say the sync failed, and "Sync to Faable Auth" on the profile retries. `wp faable-auth push-users <user>…` (or `--all`) does the same from the command line. Users listed in `FAABLE_AUTH_APP_PASSWORD_USERS` are never synced.

= Headless front-ends (REST API) =

Set the token audience (an API identifier of your tenant) and the WordPress user the requests act as. A request with `Authorization: Bearer <token>` is accepted when the token is a client_credentials token of your tenant for that audience, from an allowed client and, if set, with the required scope. It gets the capabilities of that user, so give it the smallest role that works.

Application Passwords are off: move each front-end from Basic Auth to a client_credentials token before connecting the site.

= Developers =

Filters: `faable_auth_wp_role`, `faable_auth_authorize_params`, `faable_auth_rest_user_id`. Actions: `faable_auth_user_created`, `faable_auth_user_synced`, `faable_auth_login_failed`, `faable_auth_login_link_used`.

Multisite is not supported in this version.

== External services ==

This plugin connects to Faable services. It does nothing until it is configured.

* **Your Faable Auth tenant** (the domain you configure, e.g. `https://<tenant>.auth.faable.link`). When someone logs in, their browser is sent to the tenant's login page, and the site calls the tenant to read its configuration and signing keys, exchange the login code for tokens and read the user's roles. The site receives the user's ID, name, email and roles. When a REST request carries a Bearer token, the site may fetch the tenant's public signing keys.
* **Faable dashboard and platform tenant** (`https://dashboard.faable.com`, `https://faable.auth.faable.link`), only when an administrator presses "Connect with Faable": the browser goes to the dashboard with the site URL, and the site then exchanges a one-time code for its client credentials.

Faable terms of service: https://faable.com/docs/platform/terms-of-service — privacy policy: https://faable.com/docs/platform/privacy-policy

== Installation ==

1. Install and activate the plugin.
2. Settings → Faable Auth → Connect with Faable (not needed on Faable hosting).
3. Read the emergency access section above.

== Uninstall ==

Removing the plugin brings back the normal WordPress login. Users created by the plugin have no local password: each of them sets one with "Lost your password?".

== Changelog ==

= 1.1.4 =
* The login callback and the logout return URL follow the WordPress login URL, so sites whose WordPress address differs from the site address (WordPress in a subdirectory) work.

= 1.1.3 =
* The `prompt` parameter of the login link is sanitized before it is read.
* The WordPress.org build only carries what runs on WordPress.org.

= 1.1.2 =
* Ready for the WordPress.org plugin directory: every file refuses direct access, the login button's emblem is escaped where it is printed, tested up to WordPress 7.1.

= 1.1.1 =
* The REST API keeps refusing anonymous user lists on sites with plugins that hook the REST dispatch without returning its result (acf-to-rest-api).

= 1.1.0 =
* Users added in WordPress are created in Faable Auth with their password and role; role changes and deletions follow.
* The REST API no longer lists the site's users to anonymous visitors (`/wp/v2/users`); signed-in users and Application Passwords are unaffected. Filter: `faable_auth_restrict_rest_users`.
* User sync handles a tenant with two roles of the same name.

= 1.0.0 =
* Managed mode for sites hosted on Faable: the connection comes from the environment.
* Roles synced from Faable Auth; the user profile says "Managed by Faable Auth" and links to the user in the Faable dashboard.
* Settings page with links to the tenant in the Faable dashboard and to the documentation.
* Faable emblem on the login button, on a white disc.
* Local service users can keep Application Passwords for the REST API.

= 0.1.0 =
* First version.
