AccessTokenRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 8
dl 0
loc 83
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A persistNewAccessToken() 0 21 2
A getNewToken() 0 4 1
A revokeAccessToken() 0 7 2
A isAccessTokenRevoked() 0 7 2
A findToken() 0 4 1
A deleteExpiredTokens() 0 8 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Repositories;
4
5
use AdvancedLearning\Oauth2Server\Entities\AccessTokenEntity as AccessTokenEntity;
6
use AdvancedLearning\Oauth2Server\Models\AccessToken;
7
use Carbon\Carbon;
8
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
10
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
11
use SilverStripe\ORM\DB;
12
13
class AccessTokenRepository implements AccessTokenRepositoryInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
19
    {
20
        $newToken = AccessToken::create();
21
22
        $newToken->Identifier = $accessTokenEntity->getIdentifier();
23
        $newToken->Name = $accessTokenEntity->getClient()->getName();
24
        $newToken->User = $accessTokenEntity->getUserIdentifier();
25
        $newToken->ExpiryDateTime = $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i');
26
27
        // turn scopes into space separated string
28
        $newToken->Scopes = '';
29
        $separator = '';
30
        foreach ($accessTokenEntity->getScopes() as $scope) {
31
            $newToken->Scopes .= $separator . $scope->getIdentifier();
32
            $separator = ' ';
33
        }
34
35
        $newToken->write();
36
37
        return $newToken;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
44
    {
45
        return new AccessTokenEntity($userIdentifier, $scopes);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function revokeAccessToken($tokenId)
52
    {
53
        if ($token = $this->findToken($tokenId)) {
54
            $token->Revoked = true;
55
            $token->write();
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isAccessTokenRevoked($tokenId): bool
63
    {
64
        $token = $this->findToken($tokenId);
65
66
        // return true if there is no matching token
67
        return empty($token) || $token->Revoked;
68
    }
69
70
    /**
71
     * Find the Token for passed id.
72
     *
73
     * @param string $tokenId The id of the token.
74
     *
75
     * @return AccessToken|null
76
     */
77
    public function findToken(string $tokenId): ?AccessToken
78
    {
79
        return AccessToken::get()->filter(['Identifier' => $tokenId])->first();
80
    }
81
82
    /**
83
     * Delete tokens which have expired.
84
     *
85
     * @param integer $days
86
     */
87
    public function deleteExpiredTokens($days = 1)
88
    {
89
        $expiryDate = Carbon::now()->subDays($days);
90
        DB::query(sprintf(
91
            'DELETE FROM "OauthAccessToken" WHERE "ExpiryDateTime" < \'%s\'',
92
            $expiryDate->toDateTimeString()
93
        ));
94
    }
95
}
96