Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
44.44% |
8 / 18 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
AccessToken | |
44.44% |
8 / 18 |
|
20.00% |
1 / 5 |
18.97 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getByIdAndType | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
storeNonce | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getNonce | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
expirationCallback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Table Definition for access_token |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * Copyright (C) The National Library of Finland 2022. |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2, |
13 | * as published by the Free Software Foundation. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software |
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | * |
24 | * @category VuFind |
25 | * @package Db_Table |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
28 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
29 | * @link https://vufind.org Main Page |
30 | */ |
31 | |
32 | namespace VuFind\Db\Table; |
33 | |
34 | use Laminas\Db\Adapter\Adapter; |
35 | use VuFind\Db\Row\AccessToken as AccessTokenRow; |
36 | use VuFind\Db\Row\RowGateway; |
37 | |
38 | /** |
39 | * Table Definition for access_token |
40 | * |
41 | * @category VuFind |
42 | * @package Db_Table |
43 | * @author Demian Katz <demian.katz@villanova.edu> |
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 AccessToken extends Gateway |
49 | { |
50 | use ExpirationTrait; |
51 | |
52 | /** |
53 | * Constructor |
54 | * |
55 | * @param Adapter $adapter Database adapter |
56 | * @param PluginManager $tm Table manager |
57 | * @param array $cfg Laminas configuration |
58 | * @param RowGateway $rowObj Row prototype object (null for default) |
59 | * @param string $table Name of database table to interface with |
60 | */ |
61 | public function __construct( |
62 | Adapter $adapter, |
63 | PluginManager $tm, |
64 | $cfg, |
65 | ?RowGateway $rowObj = null, |
66 | $table = 'access_token' |
67 | ) { |
68 | parent::__construct($adapter, $tm, $cfg, $rowObj, $table); |
69 | } |
70 | |
71 | /** |
72 | * Retrieve an object from the database based on id and type; create a new |
73 | * row if no existing match is found. |
74 | * |
75 | * @param string $id Token ID |
76 | * @param string $type Token type |
77 | * @param bool $create Should we create rows that don't already exist? |
78 | * |
79 | * @return ?AccessTokenRow |
80 | */ |
81 | public function getByIdAndType( |
82 | string $id, |
83 | string $type, |
84 | bool $create = true |
85 | ): ?AccessTokenRow { |
86 | $row = $this->select(['id' => $id, 'type' => $type])->current(); |
87 | if ($create && empty($row)) { |
88 | $row = $this->createRow(); |
89 | $row->id = $id; |
90 | $row->type = $type; |
91 | $row->created = date('Y-m-d H:i:s'); |
92 | } |
93 | return $row; |
94 | } |
95 | |
96 | /** |
97 | * Add or replace an OpenID nonce for a user |
98 | * |
99 | * @param int $userId User ID |
100 | * @param ?string $nonce Nonce |
101 | * |
102 | * @return void |
103 | */ |
104 | public function storeNonce(int $userId, ?string $nonce) |
105 | { |
106 | $row = $this->getByIdAndType($userId, 'openid_nonce'); |
107 | $row->created = date('Y-m-d H:i:s'); |
108 | $row->user_id = $userId; |
109 | $row->data = json_encode(compact('nonce')); |
110 | $row->save(); |
111 | } |
112 | |
113 | /** |
114 | * Retrieve an OpenID nonce for a user |
115 | * |
116 | * @param int $userId User ID |
117 | * |
118 | * @return ?string |
119 | */ |
120 | public function getNonce(int $userId): ?string |
121 | { |
122 | if ($row = $this->getByIdAndType($userId, 'openid_nonce', false)) { |
123 | $data = json_decode($row->data, true); |
124 | return $data['nonce'] ?? null; |
125 | } |
126 | return null; |
127 | } |
128 | |
129 | /** |
130 | * Update the select statement to find records to delete. |
131 | * |
132 | * @param Select $select Select clause |
133 | * @param string $dateLimit Date threshold of an "expired" record in format |
134 | * 'Y-m-d H:i:s'. |
135 | * |
136 | * @return void |
137 | */ |
138 | protected function expirationCallback($select, $dateLimit) |
139 | { |
140 | $select->where->lessThan('created', $dateLimit); |
141 | } |
142 | } |