Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
IdentityRepository | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserEntityByIdentifier | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * OpenID Connect identity 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 | |
30 | namespace VuFind\OAuth2\Repository; |
31 | |
32 | use OpenIDConnectServer\Repositories\IdentityProviderInterface; |
33 | use VuFind\Auth\ILSAuthenticator; |
34 | use VuFind\Db\Service\AccessTokenServiceInterface; |
35 | use VuFind\Db\Service\UserServiceInterface; |
36 | use VuFind\ILS\Connection; |
37 | use VuFind\OAuth2\Entity\UserEntity; |
38 | |
39 | /** |
40 | * OpenID Connect repository implementation. |
41 | * |
42 | * @category VuFind |
43 | * @package OAuth2 |
44 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org Main Site |
47 | */ |
48 | class IdentityRepository implements IdentityProviderInterface |
49 | { |
50 | /** |
51 | * Constructor |
52 | * |
53 | * @param UserServiceInterface $userService User service |
54 | * @param AccessTokenServiceInterface $accessTokenService Access token service |
55 | * @param ?Connection $ils ILS connection |
56 | * @param array $oauth2Config OAuth2 configuration |
57 | * @param ILSAuthenticator $ilsAuthenticator ILS authenticator |
58 | */ |
59 | public function __construct( |
60 | protected UserServiceInterface $userService, |
61 | protected AccessTokenServiceInterface $accessTokenService, |
62 | protected ?Connection $ils, |
63 | protected array $oauth2Config, |
64 | protected ILSAuthenticator $ilsAuthenticator |
65 | ) { |
66 | } |
67 | |
68 | /** |
69 | * Get a user entity by identifier. |
70 | * |
71 | * @param int|string $identifier User Identifier |
72 | * |
73 | * @return ?UserEntity |
74 | */ |
75 | public function getUserEntityByIdentifier($identifier) |
76 | { |
77 | $userIdentifierField = $this->oauth2Config['Server']['userIdentifierField'] ?? 'id'; |
78 | if ($user = $this->userService->getUserByField($userIdentifierField, $identifier)) { |
79 | return new UserEntity( |
80 | $user, |
81 | $this->ils, |
82 | $this->oauth2Config, |
83 | $this->accessTokenService, |
84 | $this->ilsAuthenticator |
85 | ); |
86 | } |
87 | return null; |
88 | } |
89 | } |