Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
AccessTokenRepository
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getNewToken
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 persistNewAccessToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 revokeAccessToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAccessTokenRevoked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * OAuth2 access token repository implementation.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2022-2024.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  OAuth2
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\OAuth2\Repository;
31
32use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
33use League\OAuth2\Server\Entities\ClientEntityInterface;
34use League\OAuth2\Server\Entities\ScopeEntityInterface;
35use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
36use VuFind\Auth\InvalidArgumentException;
37use VuFind\Db\Service\AccessTokenServiceInterface;
38use VuFind\Db\Service\UserServiceInterface;
39use VuFind\OAuth2\Entity\AccessTokenEntity;
40
41/**
42 * OAuth2 access token repository implementation.
43 *
44 * @category VuFind
45 * @package  OAuth2
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org Main Site
49 */
50class AccessTokenRepository extends AbstractTokenRepository implements AccessTokenRepositoryInterface
51{
52    /**
53     * Constructor
54     *
55     * @param array                       $oauth2Config       OAuth2 configuration
56     * @param AccessTokenServiceInterface $accessTokenService Access token service
57     * @param UserServiceInterface        $userService        User service
58     */
59    public function __construct(
60        array $oauth2Config,
61        AccessTokenServiceInterface $accessTokenService,
62        UserServiceInterface $userService
63    ) {
64        parent::__construct(
65            'oauth2_access_token',
66            AccessTokenEntity::class,
67            $oauth2Config,
68            $accessTokenService,
69            $userService
70        );
71    }
72
73    /**
74     * Create a new access token
75     *
76     * @param ClientEntityInterface  $clientEntity   Client entity
77     * @param ScopeEntityInterface[] $scopes         Scopes
78     * @param mixed                  $userIdentifier User identifier
79     *
80     * @return AccessTokenEntityInterface
81     */
82    public function getNewToken(
83        ClientEntityInterface $clientEntity,
84        array $scopes,
85        $userIdentifier = null
86    ) {
87        $accessToken = $this->getNew();
88        $accessToken->setClient($clientEntity);
89        foreach ($scopes as $scope) {
90            $accessToken->addScope($scope);
91        }
92        $accessToken->setUserIdentifier($userIdentifier);
93        return $accessToken;
94    }
95
96    /**
97     * Persists a new access token to permanent storage.
98     *
99     * @param AccessTokenEntityInterface $entity Access token entity
100     *
101     * @return void
102     *
103     * @throws InvalidArgumentException
104     */
105    public function persistNewAccessToken(AccessTokenEntityInterface $entity)
106    {
107        $this->persistNew($entity);
108    }
109
110    /**
111     * Revoke an access token.
112     *
113     * @param string $tokenId Token ID
114     *
115     * @return void
116     */
117    public function revokeAccessToken($tokenId)
118    {
119        $this->revoke($tokenId);
120    }
121
122    /**
123     * Check if the access token has been revoked.
124     *
125     * @param string $tokenId Token ID
126     *
127     * @return bool Return true if this token has been revoked
128     */
129    public function isAccessTokenRevoked($tokenId)
130    {
131        return $this->isRevoked($tokenId);
132    }
133}