1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Bone\Mvc\Controller; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DateTime; |
8
|
|
|
use Del\Common\ContainerService; |
9
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
10
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
11
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
12
|
|
|
use OAuth\Repository\AccessTokenRepository; |
13
|
|
|
use OAuth\Repository\ClientRepository; |
14
|
|
|
use OAuth\Repository\RefreshTokenRepository; |
15
|
|
|
use OAuth\Repository\ScopeRepository; |
16
|
|
|
use OAuth\Repository\UserRepository; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
|
20
|
|
|
class OAuthController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** @var AuthorizationServer $oauth2Server */ |
23
|
|
|
private $oauth2Server; |
24
|
|
|
|
25
|
|
|
public function init() |
26
|
|
|
{ |
27
|
|
|
$container = ContainerService::getInstance()->getContainer(); |
28
|
|
|
$clientRepository = $container['repository.Client']; |
29
|
|
|
$accessTokenRepository = $container['repository.AccessToken']; |
30
|
|
|
$scopeRepository = $container['repository.Scope']; |
31
|
|
|
$userRepository = $container['repository.User']; |
32
|
|
|
$refreshTokenRepository = $container['repository.RefreshToken']; |
33
|
|
|
|
34
|
|
|
// Setup the authorization server |
35
|
|
|
$server = new AuthorizationServer($clientRepository, $accessTokenRepository, $scopeRepository, |
36
|
|
|
'file://'.__DIR__.'/../private.key', // path to private key |
37
|
|
|
'file://'.__DIR__.'/../public.key' // path to public key |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$grant = new PasswordGrant($userRepository, $refreshTokenRepository); |
41
|
|
|
|
42
|
|
|
$grant->setRefreshTokenTTL(new DateInterval('P1M')); // refresh tokens will expire after 1 month |
43
|
|
|
|
44
|
|
|
// Enable the password grant on the server with a token TTL of 1 hour |
45
|
|
|
$server->enableGrantType( |
46
|
|
|
$grant, |
47
|
|
|
new DateInterval('PT1H') // access tokens will expire after 1 month |
48
|
|
|
); |
49
|
|
|
$this->oauth2Server = $server; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sends a response with the time |
55
|
|
|
*/ |
56
|
|
|
public function pingAction() |
57
|
|
|
{ |
58
|
|
|
$date = new DateTime(); |
59
|
|
|
$this->sendJsonResponse(['pong' => $date->format('Y-m-d H:i:s')]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function accessTokenAction() |
64
|
|
|
{ |
65
|
|
|
$this->sendJsonResponse(['accessTokenRequested' => time()]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|