-
Notifications
You must be signed in to change notification settings - Fork 279
/
Server.php
101 lines (83 loc) · 3.13 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace OAuth2Demo\Server;
use Silex\Application;
use Silex\ControllerProviderInterface;
use OAuth2\HttpFoundationBridge\Response as BridgeResponse;
use OAuth2\Server as OAuth2Server;
use OAuth2\Storage\Pdo;
use OAuth2\Storage\Memory;
use OAuth2\OpenID\GrantType\AuthorizationCode;
use OAuth2\GrantType\UserCredentials;
use OAuth2\GrantType\RefreshToken;
class Server implements ControllerProviderInterface
{
/**
* function to create the OAuth2 Server Object
*/
public function setup(Application $app)
{
// ensure our Sqlite database exists
if (!file_exists($sqliteFile = __DIR__.'/../../../data/oauth.sqlite')) {
$this->generateSqliteDb();
}
// create PDO-based sqlite storage
$storage = new Pdo(array('dsn' => 'sqlite:'.$sqliteFile));
// create array of supported grant types
$grantTypes = array(
'authorization_code' => new AuthorizationCode($storage),
'user_credentials' => new UserCredentials($storage),
'refresh_token' => new RefreshToken($storage, array(
'always_issue_new_refresh_token' => true,
)),
);
// instantiate the oauth server
$server = new OAuth2Server($storage, array(
'enforce_state' => true,
'allow_implicit' => true,
'use_openid_connect' => true,
'issuer' => $_SERVER['HTTP_HOST'],
),$grantTypes);
$server->addStorage($this->getKeyStorage(), 'public_key');
// add the server to the silex "container" so we can use it in our controllers (see src/OAuth2Demo/Server/Controllers/.*)
$app['oauth_server'] = $server;
/**
* add HttpFoundataionBridge Response to the container, which returns a silex-compatible response object
* @see (https://github.com/bshaffer/oauth2-server-httpfoundation-bridge)
*/
$app['oauth_response'] = new BridgeResponse();
}
/**
* Connect the controller classes to the routes
*/
public function connect(Application $app)
{
// create the oauth2 server object
$this->setup($app);
// creates a new controller based on the default route
$routing = $app['controllers_factory'];
/* Set corresponding endpoints on the controller classes */
Controllers\Authorize::addRoutes($routing);
Controllers\Token::addRoutes($routing);
Controllers\Resource::addRoutes($routing);
return $routing;
}
private function generateSqliteDb()
{
include_once($this->getProjectRoot().'/data/rebuild_db.php');
}
private function getKeyStorage()
{
$publicKey = file_get_contents($this->getProjectRoot().'/data/pubkey.pem');
$privateKey = file_get_contents($this->getProjectRoot().'/data/privkey.pem');
// create storage
$keyStorage = new Memory(array('keys' => array(
'public_key' => $publicKey,
'private_key' => $privateKey,
)));
return $keyStorage;
}
private function getProjectRoot()
{
return dirname(dirname(dirname(__DIR__)));
}
}