API Bearer Auth
The API Bearer Auth plugin enables authentication for the REST API by using JWT access an refresh tokens. After the user logs in, the access and refresh tokens are returned and can be used for the next requests. Issued tokens can be revoked from within the users admin screen. See below for the endpoints. Note that after activating this plugin, all REST API endpoints will need to be authenticated, unless the endpoint is whitelisted in the api_bearer_auth_unauthenticated_urls filter (see FAQ for how to use this filter). JWT Access tokens can be formatted as JWT tokens. For this to work, you first have to create a secret and add it to the wp-config.php file. If you don’t do this, access tokens will work also, but are just random strings. To create a random secret key, you can do for example: base64_encode(openssl_random_pseudo_bytes(64)); And then add the result to wp-config: define('API_BEARER_JWT_SECRET', 'mysecretkey'); If you have problems, you can verify your JWT tokens at: https://jwt.io/ Revoke tokens This plugin adds a column to the users table in de admin where you can see when a token expires. You can also revoke tokens by selection the “Revoke API tokens” from the bulk actions select box. API endpoints Note that all endpoints expect JSON in the POST body. Login Endpoint: POST /api-bearer-auth/v1/login Request body: Note: client_name is optional. But if you use it, make sure to use it as well for the refresh call! {"username": "my_username", "password": "my_password", "client_name": "my_app"} Response: { "wp_user": { "data": { "ID": 1, "user_login": "your_user_login", // other default WordPress user fields } }, "access_token": "your_access_token", "expires_in": 86400, // number of seconds "refresh_token": "your_refresh_token" } Make sure to save the access and refresh token! Refresh access token Endpoint: POST /api-bearer-auth/v1/tokens/refresh Request body: Note: client_name is optional. But if you did use it for the login call, make sure to use it here as well! {"token": "your_refresh_token", "client_name": "my_app"} Response success: { "access_token": "your_new_access_token", "expires_in": 86400 } Response when sending a wrong refresh token is a 401: { "code": "api_api_bearer_auth_error_invalid_token", "message": "Invalid token.", "data": { "status": 401 } } Do a request After you have the access token, you can make requests to authenticated endpoints with an Authorization header like this: Authorization: Bearer Note that Apache sometimes strips out the Authorization header. If this is the case, make sure to add this to the .htaccess file: RewriteCond %{HTTP:Authorization} ^(.*) # Don't know why, but some need the line below instead of the RewriteRule line # SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0 RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1] If you are not logged in or you send an invalid access token, you get a 401 response: { "code": "api_bearer_auth_not_logged_in", "message": "You are not logged in.", "data": { "status": 401 } } Important update Update immediately if you’re using a version below 20200807. Before this version all access tokens were updated when calling the refresh callback. If you are affected by this the fastest solution is to execute this query: update wp_user_tokens set access_token_valid = NOW(); This will invalidate all access tokens. This means that all users need to refresh their access token and will get a new access token and a unique one this time. A big thank to @harchvertelol for reporting this and suggesting the fix as well!
Top keywords
- token19×3.23%
- access17×2.89%
- tokens14×2.38%
- refresh12×2.04%
- access token10×1.70%
- api9×1.53%
- authorization7×1.19%
- bearer6×1.02%
- jwt6×1.02%
- user6×1.02%
- endpoints5×0.85%
- login5×0.85%
Two Factor
The Two-Factor plugin adds an extra layer of security to your WordPress login by requiring users to provide a second form of authentication in addition to their password. This helps protect against unauthorized access even if passwords are compromised. Setup Instructions Important: Each user must individually configure their two-factor authentication settings. For Individual Users Navigate to your profile: Go to “Users” → “Your Profile” in the WordPress admin Find Two-Factor Options: Scroll down to the “Two-Factor Options” section Choose your methods: Enable one or more authentication providers (noting a site admin may have hidden one or more so what is available could vary): Authenticator App (TOTP) – Use apps like Google Authenticator, Authy, or 1Password Email Codes – Receive one-time codes via email Backup Codes – Generate one-time backup codes for emergencies Dummy Method – For testing purposes only (requires WP_DEBUG) Configure each method: Follow the setup instructions for each enabled provider Set primary method: Choose which method to use as your default authentication Save changes: Click “Update Profile” to save your settings For Site Administrators Plugin settings: The plugin provides a settings page under “Settings → Two-Factor” to configure which providers should be disabled site-wide. User management: Administrators can configure 2FA for other users by editing their profiles Security recommendations: Encourage users to enable backup methods to prevent account lockouts Available Authentication Methods Authenticator App (TOTP) – Recommended Security: High – Time-based one-time passwords Setup: Scan QR code with authenticator app Compatibility: Works with Google Authenticator, Authy, 1Password, and other TOTP apps Best for: Most users, provides excellent security with good usability Backup Codes – Recommended Security: Medium – One-time use codes Setup: Generate 10 backup codes for emergency access Compatibility: Works everywhere, no special hardware needed Best for: Emergency access when other methods are unavailable Email Codes Security: Medium – One-time codes sent via email Setup: Automatic – uses your WordPress email address Compatibility: Works with any email-capable device Best for: Users who prefer email-based authentication FIDO U2F Security Keys Deprecated and removed due to loss of browser support. Dummy Method Security: None – Always succeeds Setup: Only available when WP_DEBUG is enabled Purpose: Testing and development only Best for: Developers testing the plugin Important Notes HTTPS Requirement All methods work on both HTTP and HTTPS sites Browser Compatibility TOTP and email methods work on all devices and browsers Account Recovery Always enable backup codes to prevent being locked out of your account If you lose access to all authentication methods, contact your site administrator Security Best Practices Use multiple authentication methods when possible Keep backup codes in a secure location Regularly review and update your authentication settings For more information about two-factor authentication in WordPress, see the WordPress Advanced Administration Security Guide. For more history, see this post. Actions & Filters Here is a list of action and filter hooks provided by the plugin: two_factor_providers filter overrides the available two-factor providers such as email and time-based one-time passwords. Array values are PHP classnames of the two-factor providers. two_factor_providers_for_user filter overrides the available two-factor providers for a specific user. Array values are instances of provider classes and the user object WP_User is available as the second argument. two_factor_enabled_providers_for_user filter overrides the list of two-factor providers enabled for a user. First argument is an array of enabled provider classnames as values, the second argument is the user ID. two_factor_user_authenticated action which receives the logged in WP_User object as the first argument for determining the logged in user right after the authentication workflow. two_factor_user_api_login_enable filter restricts authentication for REST API and XML-RPC to application passwords only. Provides the user ID as the second argument. two_factor_email_token_ttl filter overrides the time interval in seconds that an email token is considered after generation. Accepts the time in seconds as the first argument and the ID of the WP_User object being authenticated. two_factor_email_token_length filter overrides the default 8 character count for email tokens. two_factor_backup_code_length filter overrides the default 8 character count for backup codes. Provides the WP_User of the associated user as the second argument. two_factor_rest_api_can_edit_user filter overrides whether a user’s Two-Factor settings can be edited via the REST API. First argument is the current $can_edit boolean, the second argument is the user ID. two_factor_before_authentication_prompt action which receives the provider object and fires prior to the prompt shown on the authentication input form. two_factor_after_authentication_prompt action which receives the provider object and fires after the prompt shown on the authentication input form. two_factor_after_authentication_input action which receives the provider object and fires after the input shown on the authentication input form (if form contains no input, action fires immediately after two_factor_after_authentication_prompt). two_factor_login_backup_links filters the backup links displayed on the two-factor login form. Redirect After the Two-Factor Challenge To redirect users to a specific URL after completing the two-factor challenge, use WordPress Core built-in login_redirect filter. The filter works the same way as in a standard WordPress login flow: add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user ) { return home_url( '/dashboard/' ); }, 10, 3 );