• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created about 6 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Enable JSON Web Token authentication for the WordPress REST API.

JWT Auth

Enable JSON Web Token authentication for the WordPress REST API.

Contributors: valendesigns
Tags: jwt, json-web-token, auth, authentication, rest, wp-rest, api, wp-api, json, wp-json
Requires at least: 5.2
Tested up to: 5.2
Stable tag: 0.1.0
License: GPLv2 or later
Requires PHP: 5.6.20

Build Status Coverage Status

Description

This plugin makes it possible to use a JSON Web Token (JWT) to securely authenticate a valid user requesting access to your WordPress REST API resources.

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.

Installation

This plugin is not currently listed in the WordPress Plugin Directory. You'll need to install it manually.

  1. Download the latest version of the jwt-auth plugin.
  2. Go to Plugins > Add New.
  3. Click Upload Plugin to display the WordPress Plugin upload field.
  4. Click Choose File to navigate your local file directory.
  5. Select the WordPress Plugin zip archive you wish to upload and install.
  6. Click Install Now to install the WordPress Plugin.
  7. The resulting installation screen will list the installation as successful or note any problems during the install.
  8. If successful, click Activate Plugin to activate it, or Return to Plugin Installer for further actions.

Generate Tokens

In order to generate an access and refresh token, you must be an authenticate user. There are a couple ways to authenticate a user, but only one works for tokens.

When generating a token we must authenticate with what is called an application password. This allows us to invalidate both the access token and refresh token by adding the API key to the tokens private claim. This ensures that when a token is used that has a valid API key it will authenticate the request, but if the key has been revoked the token becomes invalidated and cannot authenticate access to the request.

Application passwords protect us from the threat of long-lived tokens. Tokens are never stored on a server anywhere, 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 and look for our safe and revocable application password inside the private claim. And since an application password cannot be used to login to WordPress, it only exists to generate tokens, we now have a secure separation of access and authentication.

If you try to generate a token with you username and password:

curl -X POST https://example.org/wp-json/wp/v2/token \
	-F username=admin \
	-F password=password

You should see an error like this:

{
    "code": "rest_authentication_required_api_key_secret",
    "message": "An API key-pair is required to generate a token.",
    "data": {
        "status": 403
    }
}

Now with an application password:

curl -X POST https://example.org/wp-json/wp/v2/token \
	-F api_key=12345ascde \
	-F api_secret=54321edcba

You should see something like this:

{
    "access_token": "YOUR_ACCESS_TOKEN",
    "data": {
        "user": {
            "id": 1,
            "type": "wp_user",
            "user_login": "admin",
            "user_email": "[email protected]",
            "api_key": "12345ascde"
        }
    },
    "exp": 604800,
    "refresh_token": "YOUR_REFRESH_TOKEN"
}

The access_token field is what you'll use for subsequent requests. For example, to fetch the user data, you could perform a request like:

curl -X GET https://sample.org/wp-json/wp/v2/users/1 \
	-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'

Note that the header reads Bearer YOUR_ACCESS_TOKEN. Ensure you include the word "Bearer" (with a space after it) in order to be properly authenticated.

Now the refresh_token field is a special kind of token that can be used to obtain a renewed access token when it finally expires.

That request would be like this:

curl -X POST https://example.org/wp-json/wp/v2/token \
	-F refresh_token=YOUR_REFRESH_TOKEN

You can also check if the token is still valid and when it expires:

curl -X GET https://sample.org/wp-json/wp/v2/token/validate \
	-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
{
    "code": "rest_authentication_valid_access_token",
    "message": "Valid access token.",
    "data": {
        "status": 200,
        "exp": 604800
    }
}

Generate Key-pairs

In order to generate a token you first need to create an application password, or what we also refer to as a key-pair. To create a key-pair you have to first log into the WordPress administrative panel and go to your profile page. There you will see a section that gives you the ability to generate a named key-pair, download the key-pair, and generate and download new tokens, as well.

By ensuring only users that can login to WordPress can create a key-pair and only key-pairs can generate tokens we get all the benefits of implementing other security systems like 2factor authentication to secure users and don't have to worry about defending that side of the user authentication flow.

Contributing

Contributors Welcome! The best way to get involved is to reach out via the #core-restapi channel in Slack. Meetings are held weekly Thursdays @ 06:00 UTC.

License

jwt-auth is licensed under GNU General Public License v2

More Repositories

1

WP-API

The WP REST API has been merged into WordPress core. Please do not create issues or send pull requests. Submit support requests to the forums or patches to Trac (see README below for links).
PHP
3,961
star
2

node-wpapi

An isomorphic JavaScript client for the WordPress REST API
JavaScript
1,678
star
3

Basic-Auth

Basic Authentication handler for the JSON API, used for development and debugging purposes
PHP
757
star
4

OAuth1

OAuth server implementation for WP API
PHP
330
star
5

client-js

Backbone-based JavaScript client for WP API
JavaScript
268
star
6

rest-filter

Restores the `filter` param to post enpdpoints.
PHP
185
star
7

OAuth2

Connect applications to your WordPress site without ever giving away your password.
PHP
171
star
8

docs

The content for developer.wordpress.org/rest-api
PHP
111
star
9

example-client

PHP
79
star
10

new-list-tables

An experiment in re-writing the post list screen using the REST API.
JavaScript
71
star
11

api-console

API console for WP API
JavaScript
69
star
12

client-cli

PHP
64
star
13

authentication

The home for design & development of a core WordPress REST API authentication solution
62
star
14

client-php

Reference client library for the WP API
PHP
60
star
15

docs-v2

Documentation for version 2 of the API
CSS
58
star
16

menus-endpoints

Feature plugin for Menu Endpoints
PHP
43
star
17

discovery-php

Library for discovering the WordPress REST API
PHP
41
star
18

wp-api-meta-endpoints

Legacy Feature plugin for Meta Endpoints. Use register_post_meta & register_meta with `'show_in_rest' => true` instead.
PHP
38
star
19

wordpress-rest-api-oauth-1

WordPress REST API OAuth 1 JavaScript Client
JavaScript
35
star
20

wp-api-site-endpoints

Legacy experimental plugin for Site Endpoints. Not maintained.
PHP
28
star
21

live-settings

Update your settings, live.
JavaScript
27
star
22

rest-api-console

JavaScript
26
star
23

multisite

Multisite handler for WP API
PHP
26
star
24

WP-API.github.io

CSS
24
star
25

markdown-sync

Syncing plugin used for WordPress.org CLI/REST API handbooks
PHP
23
star
26

wp-api-customize-endpoints

Legacy experimental WP REST API endpoints for the Customizer
PHP
20
star
27

broker-plugin

A broker between client apps and WP-based servers for OAuth bootstrapping.
PHP
18
star
28

wp-api-plugins-themes-endpoints

Legacy Feature plugin for Plugin and Theme Endpoints (Superseded by core /themes endpoint)
PHP
17
star
29

api-core

PHP
14
star
30

docs-ja

Japanese documentation for the REST API
CSS
10
star
31

broker-theme

PHP
9
star
32

wp-api-sites-endpoints

Feature project for multisite sites endpoint
PHP
8
star
33

widgets-endpoints

Experimental WordPress REST API Widgets endpoints.
PHP
7
star
34

oauth-debugger

OAuth 1 Debugger App
JavaScript
6
star
35

broker-server

PHP
6
star
36

docs-development

CSS
4
star
37

broker-example

Example client for broker authentication
PHP
4
star
38

proposals

4
star
39

rest-filter-response-fields

Legacy plugin; use ?_fields= instead of installing this!
Shell
3
star