Completed
Push — master ( 8763c1...14778d )
by Conrad
03:00
created

AuthoriseController   B

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 17
dl 0
loc 69
rs 7.8571

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 16 2
B getAuthorisationServer() 0 39 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Controllers;
4
5
use AdvancedLearning\Oauth2Server\Repositories\AccessTokenRepository;
6
use AdvancedLearning\Oauth2Server\Repositories\ClientRepository;
7
use AdvancedLearning\Oauth2Server\Repositories\RefreshTokenRepository;
8
use AdvancedLearning\Oauth2Server\Repositories\ScopeRepository;
9
use AdvancedLearning\Oauth2Server\Repositories\UserRepository;
10
use DateInterval;
11
use Exception;
12
use GuzzleHttp\Psr7\Response;
13
use League\OAuth2\Server\AuthorizationServer;
14
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
15
use League\OAuth2\Server\Grant\PasswordGrant;
16
use Robbie\Psr7\HttpRequestAdapter;
17
use Robbie\Psr7\HttpResponseAdapter;
18
use SilverStripe\Control\Controller;
19
use SilverStripe\Control\Director;
20
use SilverStripe\Control\HTTPResponse;
21
use SilverStripe\Core\Environment;
22
23
class AuthoriseController extends Controller
24
{
25
    /**
26
     * Handles authorisation.
27
     *
28
     * @return HTTPResponse
29
     */
30
    public function index(): HTTPResponse
31
    {
32
        // request needs parsed body
33
        $psrRequest = (new HttpRequestAdapter())->toPsr7($this->getRequest())
34
            ->withParsedBody(json_decode($this->getRequest()->getBody(), true));
35
        $psrResponse = new Response();
36
37
        $authServer = $this->getAuthorisationServer();
38
39
        try {
40
            return (new HttpResponseAdapter())
41
                ->fromPsr7($authServer->respondToAccessTokenRequest($psrRequest, $psrResponse));
42
        } catch (Exception $e) {
43
            return new HTTPResponse($e->getMessage(), 500);
44
        }
45
    }
46
47
    /**
48
     * Gets the OAuth2 AuthorizationServer.
49
     *
50
     * @return AuthorizationServer
51
     */
52
    protected function getAuthorisationServer(): AuthorizationServer
53
    {
54
        // Init our repositories
55
        $clientRepository = new ClientRepository();
56
        $scopeRepository = new ScopeRepository();
57
        $accessTokenRepository = new AccessTokenRepository();
58
        $userRepository = new UserRepository();
59
        $refreshRepository = new RefreshTokenRepository();
60
61
        // Path to public and private keys
62
        $privateKey = Environment::getEnv('OAUTH_PRIVATE_KEY_PATH');
63
        // inject base bath if necessary
64
        $privateKey = str_replace('{BASE_DIR}', Director::baseFolder(), $privateKey);
65
66
        $encryptionKey = Environment::getEnv('OAUTH_ENCRYPTION_KEY');
67
68
        // Setup the authorization server
69
        $server = new AuthorizationServer(
70
            $clientRepository,
71
            $accessTokenRepository,
72
            $scopeRepository,
73
            $privateKey,
74
            $encryptionKey
75
        );
76
77
        // Enable the client credentials grant on the server
78
        $server->enableGrantType(
79
            new ClientCredentialsGrant(),
80
            new DateInterval('PT1H') // access tokens will expire after 1 hour
81
        );
82
83
        // Enable password grant
84
        $server->enableGrantType(
85
            new PasswordGrant($userRepository, $refreshRepository),
86
            new DateInterval('PT1H')
87
        );
88
89
        return $server;
90
    }
91
}
92