BaseTokenService::cleanExpired()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine OAuth2
5
 *
6
 * Platine OAuth2 is a library that implements the OAuth2 specification
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine OAuth2
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
declare(strict_types=1);
32
33
namespace Platine\OAuth2\Service;
34
35
use Platine\OAuth2\Configuration;
36
use Platine\OAuth2\Entity\BaseToken;
37
use Platine\OAuth2\Entity\Client;
38
use Platine\OAuth2\Entity\Scope;
39
use Platine\OAuth2\Exception\OAuth2Exception;
40
use Platine\OAuth2\Repository\TokenRepositoryInterface;
41
42
/**
43
 * @class BaseTokenService
44
 * @package Platine\OAuth2\Service
45
 */
46
class BaseTokenService
47
{
48
    /**
49
     * The TokenRepository instance
50
     * @var TokenRepositoryInterface
51
     */
52
    protected $tokenRepository;
53
54
    /**
55
     * The ScopeService instance
56
     * @var ScopeService
57
     */
58
    protected ScopeService $scopeService;
59
60
    /**
61
     * The Configuration instance
62
     * @var Configuration
63
     */
64
    protected Configuration $configuration;
65
66
    /**
67
     * Create new instance
68
     * @param TokenRepositoryInterface $tokenRepository
69
     * @param ScopeService $scopeService
70
     * @param Configuration $configuration
71
     */
72
    public function __construct(
73
        TokenRepositoryInterface $tokenRepository,
74
        ScopeService $scopeService,
75
        Configuration $configuration
76
    ) {
77
        $this->tokenRepository = $tokenRepository;
78
        $this->scopeService = $scopeService;
79
        $this->configuration = $configuration;
80
    }
81
82
    /**
83
     * Return the token entity of given token value
84
     * @param string $tokenValue
85
     * @return BaseToken|null
86
     */
87
    public function getToken(string $tokenValue): ?BaseToken
88
    {
89
        $token = $this->tokenRepository->getByToken($tokenValue);
90
        // Because the collation is most often case insensitive, we need to add a
91
        // check here to ensure that the token matches case
92
        if ($token === null || hash_equals($token->getToken(), $tokenValue) === false) {
93
            return null;
94
        }
95
96
97
        return $token;
98
    }
99
100
    /**
101
     * Delete the given token
102
     * @param BaseToken $token
103
     * @return void
104
     */
105
    public function delete(BaseToken $token): void
106
    {
107
        $this->tokenRepository->deleteToken($token);
108
    }
109
110
    /**
111
     * Clean the expired tokens
112
     * @return void
113
     */
114
    public function cleanExpired(): void
115
    {
116
        $this->tokenRepository->cleanExpiredTokens();
117
    }
118
119
    /**
120
     *
121
     * @param array<string>|Scope[] $scopes
122
     * @param Client|null $client
123
     * @return void
124
     */
125
    public function validateTokenScopes(array $scopes, ?Client $client = null): void
126
    {
127
        $scopeList = array_map(fn($scope) => (string) $scope, $scopes);
128
129
        $persistentScopes = $this->scopeService->all();
130
        $persistentList = array_map(fn($scope) => (string) $scope, $persistentScopes);
131
132
        $diff = array_diff($scopeList, $persistentList);
133
        if (count($diff) > 0) {
134
            throw OAuth2Exception::invalidScope(sprintf(
135
                'Some scope(s) do not exist: [%s]',
136
                implode(', ', $diff)
137
            ));
138
        }
139
140
        if ($client === null) {
141
            return;
142
        }
143
144
        $clientScopes = $client->getScopes();
145
        $diffClient = array_diff($scopeList, $clientScopes);
146
        if (count($diffClient) > 0) {
147
            throw OAuth2Exception::invalidScope(sprintf(
148
                'Some scope(s) are not assigned to client: %s',
149
                implode(', ', $diff)
150
            ));
151
        }
152
    }
153
}
154