Completed
Push — master ( 6d4bc5...5bd20c )
by Derek Stephen
05:29
created

OAuthController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 26 1
A pingAction() 0 5 1
A accessTokenAction() 0 4 1
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