Completed
Push — master ( 0f5e6b...d874fd )
by Conrad
01:54
created

DefaultGenerator::getServer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
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
}