| 1 | <?php |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | // Simple server based on https://bshaffer.github.io/oauth2-server-php-docs/cookbook |
| 7 | |
| 8 | // $dbata needs to be set and be an associative array with the members 'dsn', 'username', and 'password'. |
| 9 | |
| 10 | // Autoloading (composer is preferred, but for this example let's just do this) |
| 11 | require_once('../oauth2-server-php/src/OAuth2/Autoloader.php'); |
| 12 | OAuth2\Autoloader::register(); |
| 13 | |
| 14 | // $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost" |
| 15 | $oauth2_storage = new OAuth2\Storage\Pdo($dbdata); |
| 16 | |
| 17 | // Set configuration |
| 18 | $oauth2_config = array( |
| 19 | 'require_exact_redirect_uri' => false, |
| 20 | 'always_issue_new_refresh_token' => true, // Needs to be handed below as well as there it's not constructed from within the server object. |
| 21 | 'refresh_token_lifetime' => 90*24*3600, |
| 22 | ); |
| 23 | |
| 24 | // Pass a storage object or array of storage objects to the OAuth2 server class |
| 25 | $server = new OAuth2\Server($oauth2_storage, $oauth2_config); |
| 26 | |
| 27 | // Add the "Client Credentials" grant type (it is the simplest of the grant types) |
| 28 | //$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage)); |
| 29 | |
| 30 | // Add the "Authorization Code" grant type (this is where the oauth magic happens) |
| 31 | $server->addGrantType(new OAuth2\GrantType\AuthorizationCode($oauth2_storage)); |
| 32 | |
| 33 | // Add the "Refresh Token" grant type (required to get longer-living resource access by generating new access tokens) |
| 34 | $server->addGrantType(new OAuth2\GrantType\RefreshToken($oauth2_storage, array('always_issue_new_refresh_token' => true))); |
| 35 | |
| 36 | ?> |