Completed
Push — master ( e30ecd...13caca )
by
unknown
13s queued 12s
created

AccessTokenRepository::findToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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