Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
7 / 8 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
AuthToken | |
87.50% |
7 / 8 |
|
80.00% |
4 / 5 |
5.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getHeaderValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isExpired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExpiresIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Class AuthToken |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Moravian Library 2021. |
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 VuFind\Auth |
25 | * @author Josef Moravec <moravec@mzk.cz> |
26 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | declare(strict_types=1); |
31 | |
32 | namespace VuFind\Auth; |
33 | |
34 | /** |
35 | * Class AuthToken |
36 | * |
37 | * @category VuFind |
38 | * @package VuFind\Auth |
39 | * @author Josef Moravec <moravec@mzk.cz> |
40 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org Main Page |
42 | */ |
43 | class AuthToken |
44 | { |
45 | /** |
46 | * Access token |
47 | * |
48 | * @var string |
49 | */ |
50 | protected $token; |
51 | |
52 | /** |
53 | * Token type (usually 'Bearer') |
54 | * |
55 | * @var string |
56 | */ |
57 | protected $tokenType; |
58 | |
59 | /** |
60 | * Number of seconds in token expires |
61 | * |
62 | * @var ?int |
63 | */ |
64 | protected $expiresIn; |
65 | |
66 | /** |
67 | * Timestamp of token creation |
68 | * |
69 | * @var int |
70 | */ |
71 | protected $timeCreated; |
72 | |
73 | /** |
74 | * AuthToken constructor. |
75 | * |
76 | * @param string $token Access token string |
77 | * @param int|null $expiresIn Expires in seconds? |
78 | * @param string $tokenType Type of token |
79 | */ |
80 | public function __construct( |
81 | string $token, |
82 | ?int $expiresIn, |
83 | string $tokenType = 'Bearer' |
84 | ) { |
85 | $this->token = $token; |
86 | $this->timeCreated = time(); |
87 | $this->expiresIn = $expiresIn ?? null; |
88 | $this->tokenType = $tokenType; |
89 | } |
90 | |
91 | /** |
92 | * String to be used as Authorization header value |
93 | * |
94 | * @return string |
95 | */ |
96 | public function getHeaderValue(): string |
97 | { |
98 | return $this->tokenType . ' ' . $this->token; |
99 | } |
100 | |
101 | /** |
102 | * To string casting method |
103 | * |
104 | * @return string |
105 | */ |
106 | public function __toString(): string |
107 | { |
108 | return $this->getHeaderValue(); |
109 | } |
110 | |
111 | /** |
112 | * Is token expired? |
113 | * |
114 | * @return bool |
115 | */ |
116 | public function isExpired(): bool |
117 | { |
118 | return ($this->timeCreated + $this->expiresIn) <= time(); |
119 | } |
120 | |
121 | /** |
122 | * Return expires in value in seconds |
123 | * |
124 | * @return ?int |
125 | */ |
126 | public function getExpiresIn(): ?int |
127 | { |
128 | return $this->expiresIn; |
129 | } |
130 | } |