53e55b467835180bce98c78033d5a5177fe81d5e
[authserver.git] / app / server.inc.php
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 // $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"
11 $oauth2_storage = new OAuth2\Storage\Pdo($dbdata);
12
13 // Set configuration
14 $oauth2_config = array(
15   'require_exact_redirect_uri' => false,
16   'always_issue_new_refresh_token' => true, // Needs to be handed below as well as there it's not constructed from within the server object.
17   'refresh_token_lifetime' => 90*24*3600,
18 );
19
20 // Pass a storage object or array of storage objects to the OAuth2 server class
21 $server = new OAuth2\Server($oauth2_storage, $oauth2_config);
22
23 // Add the "Client Credentials" grant type (it is the simplest of the grant types)
24 //$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
25
26 // Add the "Authorization Code" grant type (this is where the oauth magic happens)
27 $server->addGrantType(new OAuth2\GrantType\AuthorizationCode($oauth2_storage));
28
29 // Add the "Refresh Token" grant type (required to get longer-living resource access by generating new access tokens)
30 $server->addGrantType(new OAuth2\GrantType\RefreshToken($oauth2_storage, array('always_issue_new_refresh_token' => true)));
31
32 ?>