|
| 1 | +<!-- DO NOT EDIT THIS FILE; it is auto-generated from readme.txt --> |
| 2 | +# JWT Auth |
| 3 | + |
| 4 | +Enable JSON Web Token authentication for the WordPress REST API. |
| 5 | + |
| 6 | +**Contributors:** [valendesigns](https://profiles.wordpress.org/valendesigns) |
| 7 | +**Tags:** [jwt](https://wordpress.org/plugins/tags/jwt), [json-web-token](https://wordpress.org/plugins/tags/json-web-token), [auth](https://wordpress.org/plugins/tags/auth), [authentication](https://wordpress.org/plugins/tags/authentication), [rest](https://wordpress.org/plugins/tags/rest), [wp-rest](https://wordpress.org/plugins/tags/wp-rest), [api](https://wordpress.org/plugins/tags/api), [wp-api](https://wordpress.org/plugins/tags/wp-api), [json](https://wordpress.org/plugins/tags/json), [wp-json](https://wordpress.org/plugins/tags/wp-json) |
| 8 | +**Requires at least:** 5.2 |
| 9 | +**Tested up to:** 5.2 |
| 10 | +**Stable tag:** 0.1.0 |
| 11 | +**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html) |
| 12 | +**Requires PHP:** 5.6.20 |
| 13 | + |
| 14 | +[](https://travis-ci.org/WP-API/jwt-auth) [](https://coveralls.io/github/WP-API/jwt-auth) |
| 15 | + |
| 16 | +## Description ## |
| 17 | + |
| 18 | +This plugin makes it possible to use a JSON Web Token (JWT) to securely authenticate a valid user requesting access to |
| 19 | +your WordPress REST API resources. |
| 20 | + |
| 21 | +JSON Web Tokens are an open, industry standard [RFC 7519](https://tools.ietf.org/html/rfc7519) method for representing |
| 22 | +claims securely between two parties. |
| 23 | + |
| 24 | +## Installation ## |
| 25 | + |
| 26 | +This plugin is not currently listed in the WordPress Plugin Directory. You'll need to install it manually. |
| 27 | + |
| 28 | +1. [Download](https://github.com/WP-API/jwt-auth/archive/develop.zip) the latest version of the `jwt-auth` plugin. |
| 29 | +1. Go to Plugins > Add New. |
| 30 | +1. Click Upload Plugin to display the WordPress Plugin upload field. |
| 31 | +1. Click Choose File to navigate your local file directory. |
| 32 | +1. Select the WordPress Plugin zip archive you wish to upload and install. |
| 33 | +1. Click Install Now to install the WordPress Plugin. |
| 34 | +1. The resulting installation screen will list the installation as successful or note any problems during the install. |
| 35 | +1. If successful, click Activate Plugin to activate it, or Return to Plugin Installer for further actions. |
| 36 | + |
| 37 | +## Generate Tokens ## |
| 38 | + |
| 39 | +In order to generate an access and refresh token, you must be an authenticate user. There are a couple ways to |
| 40 | +authenticate a user, but only one works for tokens. |
| 41 | + |
| 42 | +When generating a token we must authenticate with what is called an application password. This allows us to invalidate |
| 43 | +both the access token and refresh token by adding the API key to the tokens private claim. This ensures that when a |
| 44 | +token is used that has a valid API key it will authenticate the request, but if the key has been revoked the token |
| 45 | +becomes invalidated and cannot authenticate access to the request. |
| 46 | + |
| 47 | +Application passwords protect us from the threat of long-lived tokens. Tokens are never stored on a server anywhere, |
| 48 | +and they work until they expire, which could be filtered to be a long time from now. So what we do is decoded the token |
| 49 | +and look for our safe and revocable application password inside the private claim. And since an application password |
| 50 | +cannot be used to login to WordPress, it only exists to generate tokens, we now have a secure separation of access and |
| 51 | +authentication. |
| 52 | + |
| 53 | +If you try to generate a token with you username and password: |
| 54 | + |
| 55 | +```bash |
| 56 | +curl -X POST https://example.org/wp-json/wp/v2/token \ |
| 57 | + -F username=admin \ |
| 58 | + -F password=password |
| 59 | +``` |
| 60 | + |
| 61 | +You should see an error like this: |
| 62 | + |
| 63 | +```javascript |
| 64 | +{ |
| 65 | + "code": "rest_authentication_required_api_key_secret", |
| 66 | + "message": "An API key-pair is required to generate a token.", |
| 67 | + "data": { |
| 68 | + "status": 403 |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Now with an application password: |
| 74 | + |
| 75 | +```bash |
| 76 | +curl -X POST https://example.org/wp-json/wp/v2/token \ |
| 77 | + -F api_key=12345ascde \ |
| 78 | + -F api_secret=54321edcba |
| 79 | +``` |
| 80 | + |
| 81 | +You should see something like this: |
| 82 | + |
| 83 | +```javascript |
| 84 | +{ |
| 85 | + "access_token": "YOUR_ACCESS_TOKEN", |
| 86 | + "data": { |
| 87 | + "user": { |
| 88 | + "id": 1, |
| 89 | + "type": "wp_user", |
| 90 | + "user_login": "admin", |
| 91 | + "user_email": "[email protected]", |
| 92 | + "api_key": "12345ascde" |
| 93 | + } |
| 94 | + }, |
| 95 | + "exp": 604800, |
| 96 | + "refresh_token": "YOUR_REFRESH_TOKEN" |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +The `access_token` field is what you'll use for subsequent requests. For example, to fetch the user data, you could |
| 101 | +perform a request like: |
| 102 | + |
| 103 | +```bash |
| 104 | +curl -X GET https://sample.org/wp-json/wp/v2/users/1 \ |
| 105 | + -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' |
| 106 | +``` |
| 107 | + |
| 108 | +> Note that the header reads `Bearer YOUR_ACCESS_TOKEN`. Ensure you include the word "Bearer" (with a space after it) |
| 109 | +in order to be properly authenticated. |
| 110 | + |
| 111 | +Now the `refresh_token` field is a special kind of token that can be used to obtain a renewed access token when it |
| 112 | +finally expires. |
| 113 | + |
| 114 | +That request would be like this: |
| 115 | + |
| 116 | +```bash |
| 117 | +curl -X POST https://example.org/wp-json/wp/v2/token \ |
| 118 | + -F refresh_token=YOUR_REFRESH_TOKEN |
| 119 | +``` |
| 120 | + |
| 121 | +## Generate Application Passwords ## |
| 122 | + |
| 123 | +In order to generate a token you first need to create an application password, or what we also refer to as a key-pair. |
| 124 | +To create a key-pair you have to first log into the WordPress administrative panel and go to your profile page. There |
| 125 | +you will see a section that gives you the ability to generate a named key-pair, download the key-pair, and generate |
| 126 | +and download new tokens, as well. |
| 127 | + |
| 128 | +By ensuring only users that can login to WordPress can create a key-pair and only key-pairs can generate tokens we get |
| 129 | +all the benefits of implementing other security systems like 2factor authentication to secure users and don't have to |
| 130 | +worry about defending that side of the user authentication flow. |
| 131 | + |
| 132 | +## Contributing ## |
| 133 | + |
| 134 | +Contributors Welcome! The best way to get involved is to reach out via the [#core-restapi](https://wordpress.slack.com/messages/core-restapi/) channel in [Slack](https://make.wordpress.org/chat/). Meetings are held weekly [Thursdays @ 06:00 UTC](https://www.timeanddate.com/worldclock/timezone/utc). |
| 135 | + |
| 136 | +## License ## |
| 137 | + |
| 138 | +`jwt-auth` is licensed under [GNU General Public License v2](/LICENSE) |
| 139 | + |
0 commit comments