DefaultGenerator   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 10
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getServer() 0 39 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\AuthorizationServer;
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 League\OAuth2\Server\AuthorizationServer;
11
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
12
use League\OAuth2\Server\Grant\PasswordGrant;
13
use SilverStripe\Control\Director;
14
use SilverStripe\Core\Environment;
15
16
class DefaultGenerator implements Generator
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function getServer(): AuthorizationServer
22
    {
23
        // Init our repositories
24
        $clientRepository = new ClientRepository();
25
        $scopeRepository = new ScopeRepository();
26
        $accessTokenRepository = new AccessTokenRepository();
27
        $userRepository = new UserRepository();
28
        $refreshRepository = new RefreshTokenRepository();
29
30
        // Path to public and private keys
31
        $privateKey = Environment::getEnv('OAUTH_PRIVATE_KEY_PATH');
32
        // inject base bath if necessary
33
        $privateKey = str_replace('{BASE_DIR}', Director::baseFolder(), $privateKey);
34
35
        $encryptionKey = Environment::getEnv('OAUTH_ENCRYPTION_KEY');
36
37
        // Setup the authorization server
38
        $server = new AuthorizationServer(
39
            $clientRepository,
40
            $accessTokenRepository,
41
            $scopeRepository,
42
            $privateKey,
43
            $encryptionKey
44
        );
45
46
        // Enable the client credentials grant on the server
47
        $server->enableGrantType(
48
            new ClientCredentialsGrant(),
49
            new \DateInterval('PT1H') // access tokens will expire after 1 hour
50
        );
51
52
        // Enable password grant
53
        $server->enableGrantType(
54
            new PasswordGrant($userRepository, $refreshRepository),
55
            new \DateInterval('PT1H')
56
        );
57
58
        return $server;
59
    }
60
}