Simple OAuth is an implementation of the OAuth 2.0 Authorization Framework RFC. Using OAuth 2.0 Bearer Token is very easy. See how you can get the basics working in less than 5 minutes! This project is focused in simplicity of use and flexibility. When deciding which project to use, also consider other projects like OAuth, an OAuth 1 implementation that doesn't rely on you having https in your production server.
Based on League\OAuth2
This module uses the fantastic PHP library OAuth 2.0 Server from The League of Extraordinary Packages. This library has become the de-facto standard for modern PHP applications and is thoroughly tested.
Quick demo (Client Credentials Grant)
- Install the module using Composer:
composer require drupal/simple_oauth:6.0.x'. You can use any other installation method, as long as you install the OAuth2 Server composer package. - Generate a pair of keys to encrypt the tokens. And store them outside of your document root for security reasons.
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout > public.key
- Save the path to your keys in:
/admin/config/people/simple_oauth. - Go to
/admin/modulesand enable theJSON:APImodule. - Go to
/admin/people/permissionsand allow the permissionView published contentonly for authenticated user. - Create a scope by going to:
/admin/config/people/simple_oauth/oauth2_scope/dynamic/add, enable theClient Credentialsgrant type and set permission toaccess content. - Create a Client Application by going to:
/admin/config/services/consumer/add, enable theClient Credentialsgrant type, set User underClient Credentials settingsand setIs Confidential?to true. - Create a token with your credentials by making a
POSTrequest to/oauth/token. See the documentation about what fields your request should contain. - Request a node via JSON:API without authentication and watch it fail, e.g:
/jsonapi/node/{bundle}?page[limit]=1. - Request a node via JSON:API with the header
Authorization: Bearer {YOUR_TOKEN}and watch it succeed.
My token has expired!
First, that is a good thing. Tokens are like cash, if you have it you can use it. You don't need to prove that token belongs to you, so don't let anyone steal your token. In order to lower the risk tokens should expire fairly quickly. If your token expires in 120s then it will be only usable during that window.
What do I do if my token was expired?
Along with your access token, an authentication token is created. It's called the refresh token . It's a longer lived token, that it's associated to an access token and can be used to create a replica of your expired access token. You can then use that new access token normally. To use your refresh token you will need to make use of the Refresh Token Grant. That will return a JSON document with the new token and a new refresh token. That URL can only be accessed with your refresh token, even if your access token is still valid.
What do I do if my refresh token was also expired, or I don't have a refresh token?
Then you will need to generate a new token from scratch. You can avoid this by refreshing your access token before your refresh token expires. This way you avoid the need to require the user to prove their identity to Drupal to create a new token. Another way to mitigate this is to use longer expiration times in your tokens. This will work, but the the recommendation is to refresh your token in time.
I'm seeing warnings about my private key file permissions. What should I do?
The upstream OAuth library checks the private key's file permissions by default. This is suitable in certain server configurations, however in some modern environments (e.g., containerized workloads) where secrets are injected into the environment and owned by a user different from the web daemon's run-as user, this is a false-positive. In these scenarios, you can use the Settings API to set the value passed to CryptKey::__construct() for checking the file permission:
In settings.php:
$settings['simple_oauth.key_permissions_check'] = FALSE;
Recommendation
Check the official documentation on the Bearer Token Usage. And turn on SSL!.
Issues and contributions
Issues and development happens in the Drupal.org issue queue.