1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Bone\Mvc\Controller; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DateTime; |
8
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
9
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
10
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
11
|
|
|
use OAuth2ServerExamples\Repositories\AccessTokenRepository; |
12
|
|
|
use OAuth\Repository\ClientRepository; |
13
|
|
|
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; |
14
|
|
|
use OAuth2ServerExamples\Repositories\ScopeRepository; |
15
|
|
|
use OAuth2ServerExamples\Repositories\UserRepository; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
|
19
|
|
|
class OAuthController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** @var AuthorizationServer $oauth2Server */ |
22
|
|
|
private $oauth2Server; |
23
|
|
|
|
24
|
|
|
public function init() |
25
|
|
|
{ |
26
|
|
|
$this->oauth2Server = function () { |
|
|
|
|
27
|
|
|
// Setup the authorization server |
28
|
|
|
$server = new AuthorizationServer( |
29
|
|
|
new ClientRepository(), // instance of ClientRepositoryInterface |
|
|
|
|
30
|
|
|
new AccessTokenRepository(), // instance of AccessTokenRepositoryInterface |
31
|
|
|
new ScopeRepository(), // instance of ScopeRepositoryInterface |
32
|
|
|
'file://'.__DIR__.'/../private.key', // path to private key |
33
|
|
|
'file://'.__DIR__.'/../public.key' // path to public key |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$grant = new PasswordGrant( |
37
|
|
|
new UserRepository(), // instance of UserRepositoryInterface |
38
|
|
|
new RefreshTokenRepository() // instance of RefreshTokenRepositoryInterface |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month |
42
|
|
|
|
43
|
|
|
// Enable the password grant on the server with a token TTL of 1 hour |
44
|
|
|
$server->enableGrantType( |
45
|
|
|
$grant, |
46
|
|
|
new DateInterval('PT1H') // access tokens will expire after 1 month |
47
|
|
|
); |
48
|
|
|
return $server; |
49
|
|
|
}; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function pingAction() |
53
|
|
|
{ |
54
|
|
|
$date = new DateTime(); |
55
|
|
|
$this->sendJsonResponse(['pong' => $date->format('Y-m-d H:i:s')]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..