@@ -4,32 +4,32 @@ discard block |
||
4 | 4 | |
5 | 5 | class OATH_OCRAParser { |
6 | 6 | |
7 | - private $OCRASuite = NULL; |
|
7 | + private $OCRASuite = NULL; |
|
8 | 8 | |
9 | - private $OCRAVersion = NULL; |
|
9 | + private $OCRAVersion = NULL; |
|
10 | 10 | |
11 | - private $CryptoFunctionType = NULL; |
|
12 | - private $CryptoFunctionHash = NULL; |
|
13 | - private $CryptoFunctionHashLength = NULL; |
|
14 | - private $CryptoFunctionTruncation = NULL; |
|
11 | + private $CryptoFunctionType = NULL; |
|
12 | + private $CryptoFunctionHash = NULL; |
|
13 | + private $CryptoFunctionHashLength = NULL; |
|
14 | + private $CryptoFunctionTruncation = NULL; |
|
15 | 15 | |
16 | - private $C = FALSE; |
|
17 | - private $Q = FALSE; |
|
18 | - private $QType = 'N'; |
|
19 | - private $QLength = 8; |
|
16 | + private $C = FALSE; |
|
17 | + private $Q = FALSE; |
|
18 | + private $QType = 'N'; |
|
19 | + private $QLength = 8; |
|
20 | 20 | |
21 | - private $P = FALSE; |
|
22 | - private $PType = 'SHA1'; |
|
23 | - private $PLength = 20; |
|
21 | + private $P = FALSE; |
|
22 | + private $PType = 'SHA1'; |
|
23 | + private $PLength = 20; |
|
24 | 24 | |
25 | - private $S = FALSE; |
|
26 | - private $SLength = 64; |
|
25 | + private $S = FALSE; |
|
26 | + private $SLength = 64; |
|
27 | 27 | |
28 | - private $T = FALSE; |
|
29 | - private $TLength = 60; // 1M |
|
30 | - private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
28 | + private $T = FALSE; |
|
29 | + private $TLength = 60; // 1M |
|
30 | + private $TPeriods = array('H' => 3600, 'M' => 60, 'S' => 1); |
|
31 | 31 | |
32 | - private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
32 | + private $supportedHashFunctions = array('SHA1' => 20, 'SHA256' => 32, 'SHA512' => 64); |
|
33 | 33 | |
34 | 34 | |
35 | 35 | /** |
@@ -37,149 +37,149 @@ discard block |
||
37 | 37 | * @throws Exception |
38 | 38 | */ |
39 | 39 | public function __construct(String $ocraSuite) { |
40 | - $this->parseOCRASuite($ocraSuite); |
|
41 | - } |
|
40 | + $this->parseOCRASuite($ocraSuite); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Inspired by https://github.com/bdauvergne/python-oath |
|
43 | + /** |
|
44 | + * Inspired by https://github.com/bdauvergne/python-oath |
|
45 | 45 | * |
46 | 46 | * @throws Exception |
47 | - */ |
|
48 | - private function parseOCRASuite($ocraSuite) { |
|
49 | - if (!is_string($ocraSuite)) { |
|
50 | - throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
51 | - } |
|
52 | - |
|
53 | - $ocraSuite = strtoupper($ocraSuite); |
|
54 | - $this->OCRASuite = $ocraSuite; |
|
55 | - |
|
56 | - $s = explode(':', $ocraSuite); |
|
57 | - if (count($s) != 3) { |
|
58 | - throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
59 | - } |
|
60 | - |
|
61 | - $algo = explode('-', $s[0]); |
|
62 | - if (count($algo) != 2) { |
|
63 | - throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
64 | - } |
|
65 | - |
|
66 | - if ($algo[0] !== 'OCRA') { |
|
67 | - throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
68 | - } |
|
69 | - |
|
70 | - if ($algo[1] !== '1') { |
|
71 | - throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
72 | - } |
|
73 | - $this->OCRAVersion = $algo[1]; |
|
74 | - |
|
75 | - $cf = explode('-', $s[1]); |
|
76 | - if (count($cf) != 3) { |
|
77 | - throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
78 | - } |
|
79 | - |
|
80 | - if ($cf[0] !== 'HOTP') { |
|
81 | - throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
82 | - } |
|
83 | - $this->CryptoFunctionType = $cf[0]; |
|
84 | - |
|
85 | - if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
86 | - throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
87 | - } |
|
88 | - $this->CryptoFunctionHash = $cf[1]; |
|
89 | - $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
90 | - |
|
91 | - if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
92 | - throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
93 | - } |
|
94 | - $this->CryptoFunctionTruncation = intval($cf[2]); |
|
95 | - |
|
96 | - $di = explode('-', $s[2]); |
|
97 | - if (count($cf) == 0) { |
|
98 | - throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
99 | - } |
|
100 | - |
|
101 | - $data_input = array(); |
|
102 | - foreach($di as $elem) { |
|
103 | - $letter = $elem[0]; |
|
104 | - if (array_key_exists($letter, $data_input)) { |
|
105 | - throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
106 | - } |
|
107 | - $data_input[$letter] = 1; |
|
108 | - |
|
109 | - if ($letter === 'C' && strlen($elem) == 1) { |
|
110 | - $this->C = TRUE; |
|
111 | - } elseif ($letter === 'Q') { |
|
112 | - if (strlen($elem) == 1) { |
|
113 | - $this->Q = TRUE; |
|
114 | - } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
115 | - $q_len = intval($match[2]); |
|
116 | - if ($q_len < 4 || $q_len > 64) { |
|
117 | - throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
118 | - } |
|
119 | - $this->Q = TRUE; |
|
120 | - $this->QType = $match[1]; |
|
121 | - $this->QLength = $q_len; |
|
122 | - } else { |
|
123 | - throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
124 | - } |
|
125 | - } elseif ($letter === 'P') { |
|
126 | - if (strlen($elem) == 1) { |
|
127 | - $this->P = TRUE; |
|
128 | - } else { |
|
129 | - $p_algo = substr($elem, 1); |
|
130 | - if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
131 | - throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
132 | - } |
|
133 | - $this->P = TRUE; |
|
134 | - $this->PType = $p_algo; |
|
135 | - $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
136 | - } |
|
137 | - } elseif ($letter === 'S') { |
|
138 | - if (strlen($elem) == 1) { |
|
139 | - $this->S = TRUE; |
|
140 | - } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
141 | - $s_len = intval($match[1]); |
|
142 | - if ($s_len <= 0 || $s_len > 512) { |
|
143 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
144 | - } |
|
145 | - |
|
146 | - $this->S = TRUE; |
|
147 | - $this->SLength = $s_len; |
|
148 | - } else { |
|
149 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
150 | - } |
|
151 | - } elseif ($letter === 'T') { |
|
152 | - if (strlen($elem) == 1) { |
|
153 | - $this->T = TRUE; |
|
154 | - } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
155 | - preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
156 | - |
|
157 | - if (count($match[1]) !== count(array_unique($match[2]))) { |
|
158 | - throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
159 | - } |
|
160 | - |
|
161 | - $length = 0; |
|
162 | - for ($i = 0; $i < count($match[1]); $i++) { |
|
163 | - $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
164 | - } |
|
165 | - if ($length <= 0) { |
|
166 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
167 | - } |
|
168 | - |
|
169 | - $this->T = TRUE; |
|
170 | - $this->TLength = $length; |
|
171 | - } else { |
|
172 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
173 | - } |
|
174 | - } else { |
|
175 | - throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - if (!$this->Q) { |
|
180 | - throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
181 | - } |
|
182 | - } |
|
47 | + */ |
|
48 | + private function parseOCRASuite($ocraSuite) { |
|
49 | + if (!is_string($ocraSuite)) { |
|
50 | + throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
51 | + } |
|
52 | + |
|
53 | + $ocraSuite = strtoupper($ocraSuite); |
|
54 | + $this->OCRASuite = $ocraSuite; |
|
55 | + |
|
56 | + $s = explode(':', $ocraSuite); |
|
57 | + if (count($s) != 3) { |
|
58 | + throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
59 | + } |
|
60 | + |
|
61 | + $algo = explode('-', $s[0]); |
|
62 | + if (count($algo) != 2) { |
|
63 | + throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
64 | + } |
|
65 | + |
|
66 | + if ($algo[0] !== 'OCRA') { |
|
67 | + throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
68 | + } |
|
69 | + |
|
70 | + if ($algo[1] !== '1') { |
|
71 | + throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
72 | + } |
|
73 | + $this->OCRAVersion = $algo[1]; |
|
74 | + |
|
75 | + $cf = explode('-', $s[1]); |
|
76 | + if (count($cf) != 3) { |
|
77 | + throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
78 | + } |
|
79 | + |
|
80 | + if ($cf[0] !== 'HOTP') { |
|
81 | + throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
82 | + } |
|
83 | + $this->CryptoFunctionType = $cf[0]; |
|
84 | + |
|
85 | + if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
|
86 | + throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
87 | + } |
|
88 | + $this->CryptoFunctionHash = $cf[1]; |
|
89 | + $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
|
90 | + |
|
91 | + if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
|
92 | + throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
93 | + } |
|
94 | + $this->CryptoFunctionTruncation = intval($cf[2]); |
|
95 | + |
|
96 | + $di = explode('-', $s[2]); |
|
97 | + if (count($cf) == 0) { |
|
98 | + throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
99 | + } |
|
100 | + |
|
101 | + $data_input = array(); |
|
102 | + foreach($di as $elem) { |
|
103 | + $letter = $elem[0]; |
|
104 | + if (array_key_exists($letter, $data_input)) { |
|
105 | + throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
106 | + } |
|
107 | + $data_input[$letter] = 1; |
|
108 | + |
|
109 | + if ($letter === 'C' && strlen($elem) == 1) { |
|
110 | + $this->C = TRUE; |
|
111 | + } elseif ($letter === 'Q') { |
|
112 | + if (strlen($elem) == 1) { |
|
113 | + $this->Q = TRUE; |
|
114 | + } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
|
115 | + $q_len = intval($match[2]); |
|
116 | + if ($q_len < 4 || $q_len > 64) { |
|
117 | + throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
118 | + } |
|
119 | + $this->Q = TRUE; |
|
120 | + $this->QType = $match[1]; |
|
121 | + $this->QLength = $q_len; |
|
122 | + } else { |
|
123 | + throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
124 | + } |
|
125 | + } elseif ($letter === 'P') { |
|
126 | + if (strlen($elem) == 1) { |
|
127 | + $this->P = TRUE; |
|
128 | + } else { |
|
129 | + $p_algo = substr($elem, 1); |
|
130 | + if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
|
131 | + throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
132 | + } |
|
133 | + $this->P = TRUE; |
|
134 | + $this->PType = $p_algo; |
|
135 | + $this->PLength = $this->supportedHashFunctions[$p_algo]; |
|
136 | + } |
|
137 | + } elseif ($letter === 'S') { |
|
138 | + if (strlen($elem) == 1) { |
|
139 | + $this->S = TRUE; |
|
140 | + } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
|
141 | + $s_len = intval($match[1]); |
|
142 | + if ($s_len <= 0 || $s_len > 512) { |
|
143 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
144 | + } |
|
145 | + |
|
146 | + $this->S = TRUE; |
|
147 | + $this->SLength = $s_len; |
|
148 | + } else { |
|
149 | + throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
150 | + } |
|
151 | + } elseif ($letter === 'T') { |
|
152 | + if (strlen($elem) == 1) { |
|
153 | + $this->T = TRUE; |
|
154 | + } elseif (preg_match('/^T(\d+[HMS])+$/', $elem)) { |
|
155 | + preg_match_all('/(\d+)([HMS])/', $elem, $match); |
|
156 | + |
|
157 | + if (count($match[1]) !== count(array_unique($match[2]))) { |
|
158 | + throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
159 | + } |
|
160 | + |
|
161 | + $length = 0; |
|
162 | + for ($i = 0; $i < count($match[1]); $i++) { |
|
163 | + $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
|
164 | + } |
|
165 | + if ($length <= 0) { |
|
166 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
167 | + } |
|
168 | + |
|
169 | + $this->T = TRUE; |
|
170 | + $this->TLength = $length; |
|
171 | + } else { |
|
172 | + throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
173 | + } |
|
174 | + } else { |
|
175 | + throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + if (!$this->Q) { |
|
180 | + throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
181 | + } |
|
182 | + } |
|
183 | 183 | |
184 | 184 | /** Generate an OCRA challenge question according to the ocra suite specified in the constructor |
185 | 185 | * @return The randomly generated OCRA question |
@@ -192,50 +192,50 @@ discard block |
||
192 | 192 | * Note that the question string is the exact question string a specified in the OCRA strandard (RFC 6287) |
193 | 193 | * The challenge is not yet hex encoded as expected by OCRA::generateOCRA() |
194 | 194 | */ |
195 | - public function generateChallenge() : String { |
|
196 | - $q_length = $this->QLength; |
|
197 | - $q_type = $this->QType; |
|
195 | + public function generateChallenge() : String { |
|
196 | + $q_length = $this->QLength; |
|
197 | + $q_type = $this->QType; |
|
198 | 198 | |
199 | 199 | $bytes = Tiqr_Random::randomBytes($q_length); |
200 | 200 | |
201 | - switch($q_type) { |
|
202 | - case 'A': |
|
203 | - $challenge = base64_encode($bytes); |
|
204 | - $tr = implode("", unpack('H*', $bytes)); |
|
205 | - $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
206 | - break; |
|
207 | - case 'H': |
|
208 | - $challenge = implode("", unpack('H*', $bytes)); |
|
209 | - break; |
|
210 | - case 'N': |
|
211 | - $challenge = implode("", unpack('N*', $bytes)); |
|
212 | - break; |
|
213 | - default: |
|
214 | - throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
215 | - break; |
|
216 | - } |
|
217 | - |
|
218 | - $challenge = substr($challenge, 0, $q_length); |
|
219 | - |
|
220 | - return $challenge; |
|
221 | - } |
|
222 | - |
|
223 | - |
|
224 | - /** |
|
225 | - * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
226 | - */ |
|
227 | - public static function constEqual(string $s1, string $s2): bool { |
|
228 | - if (strlen($s1) != strlen($s2)) { |
|
229 | - return FALSE; |
|
230 | - } |
|
231 | - |
|
232 | - $result = TRUE; |
|
233 | - $length = strlen($s1); |
|
234 | - for ($i = 0; $i < $length; $i++) { |
|
235 | - $result &= ($s1[$i] == $s2[$i]); |
|
236 | - } |
|
237 | - |
|
238 | - return (boolean)$result; |
|
239 | - } |
|
201 | + switch($q_type) { |
|
202 | + case 'A': |
|
203 | + $challenge = base64_encode($bytes); |
|
204 | + $tr = implode("", unpack('H*', $bytes)); |
|
205 | + $challenge = rtrim(strtr($challenge, '+/', $tr), '='); |
|
206 | + break; |
|
207 | + case 'H': |
|
208 | + $challenge = implode("", unpack('H*', $bytes)); |
|
209 | + break; |
|
210 | + case 'N': |
|
211 | + $challenge = implode("", unpack('N*', $bytes)); |
|
212 | + break; |
|
213 | + default: |
|
214 | + throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
215 | + break; |
|
216 | + } |
|
217 | + |
|
218 | + $challenge = substr($challenge, 0, $q_length); |
|
219 | + |
|
220 | + return $challenge; |
|
221 | + } |
|
222 | + |
|
223 | + |
|
224 | + /** |
|
225 | + * Constant time string comparison, see http://codahale.com/a-lesson-in-timing-attacks/ |
|
226 | + */ |
|
227 | + public static function constEqual(string $s1, string $s2): bool { |
|
228 | + if (strlen($s1) != strlen($s2)) { |
|
229 | + return FALSE; |
|
230 | + } |
|
231 | + |
|
232 | + $result = TRUE; |
|
233 | + $length = strlen($s1); |
|
234 | + for ($i = 0; $i < $length; $i++) { |
|
235 | + $result &= ($s1[$i] == $s2[$i]); |
|
236 | + } |
|
237 | + |
|
238 | + return (boolean)$result; |
|
239 | + } |
|
240 | 240 | |
241 | 241 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | private function parseOCRASuite($ocraSuite) { |
47 | 47 | if (!is_string($ocraSuite)) { |
48 | - throw new Exception('OCRASuite not in string format: ' . var_export($ocraSuite, TRUE)); |
|
48 | + throw new Exception('OCRASuite not in string format: '.var_export($ocraSuite, TRUE)); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | $ocraSuite = strtoupper($ocraSuite); |
@@ -53,54 +53,54 @@ discard block |
||
53 | 53 | |
54 | 54 | $s = explode(':', $ocraSuite); |
55 | 55 | if (count($s) != 3) { |
56 | - throw new Exception('Invalid OCRASuite format: ' . var_export($ocraSuite, TRUE)); |
|
56 | + throw new Exception('Invalid OCRASuite format: '.var_export($ocraSuite, TRUE)); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | $algo = explode('-', $s[0]); |
60 | 60 | if (count($algo) != 2) { |
61 | - throw new Exception('Invalid OCRA version: ' . var_export($s[0], TRUE)); |
|
61 | + throw new Exception('Invalid OCRA version: '.var_export($s[0], TRUE)); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | if ($algo[0] !== 'OCRA') { |
65 | - throw new Exception('Unsupported OCRA algorithm: ' . var_export($algo[0], TRUE)); |
|
65 | + throw new Exception('Unsupported OCRA algorithm: '.var_export($algo[0], TRUE)); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | if ($algo[1] !== '1') { |
69 | - throw new Exception('Unsupported OCRA version: ' . var_export($algo[1], TRUE)); |
|
69 | + throw new Exception('Unsupported OCRA version: '.var_export($algo[1], TRUE)); |
|
70 | 70 | } |
71 | 71 | $this->OCRAVersion = $algo[1]; |
72 | 72 | |
73 | 73 | $cf = explode('-', $s[1]); |
74 | 74 | if (count($cf) != 3) { |
75 | - throw new Exception('Invalid OCRA suite crypto function: ' . var_export($s[1], TRUE)); |
|
75 | + throw new Exception('Invalid OCRA suite crypto function: '.var_export($s[1], TRUE)); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | if ($cf[0] !== 'HOTP') { |
79 | - throw new Exception('Unsupported OCRA suite crypto function: ' . var_export($cf[0], TRUE)); |
|
79 | + throw new Exception('Unsupported OCRA suite crypto function: '.var_export($cf[0], TRUE)); |
|
80 | 80 | } |
81 | 81 | $this->CryptoFunctionType = $cf[0]; |
82 | 82 | |
83 | 83 | if (!array_key_exists($cf[1], $this->supportedHashFunctions)) { |
84 | - throw new Exception('Unsupported hash function in OCRA suite crypto function: ' . var_export($cf[1], TRUE)); |
|
84 | + throw new Exception('Unsupported hash function in OCRA suite crypto function: '.var_export($cf[1], TRUE)); |
|
85 | 85 | } |
86 | 86 | $this->CryptoFunctionHash = $cf[1]; |
87 | 87 | $this->CryptoFunctionHashLength = $this->supportedHashFunctions[$cf[1]]; |
88 | 88 | |
89 | 89 | if (!preg_match('/^\d+$/', $cf[2]) || (($cf[2] < 4 || $cf[2] > 10) && $cf[2] != 0)) { |
90 | - throw new Exception('Invalid OCRA suite crypto function truncation length: ' . var_export($cf[2], TRUE)); |
|
90 | + throw new Exception('Invalid OCRA suite crypto function truncation length: '.var_export($cf[2], TRUE)); |
|
91 | 91 | } |
92 | 92 | $this->CryptoFunctionTruncation = intval($cf[2]); |
93 | 93 | |
94 | 94 | $di = explode('-', $s[2]); |
95 | 95 | if (count($cf) == 0) { |
96 | - throw new Exception('Invalid OCRA suite data input: ' . var_export($s[2], TRUE)); |
|
96 | + throw new Exception('Invalid OCRA suite data input: '.var_export($s[2], TRUE)); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | $data_input = array(); |
100 | - foreach($di as $elem) { |
|
100 | + foreach ($di as $elem) { |
|
101 | 101 | $letter = $elem[0]; |
102 | 102 | if (array_key_exists($letter, $data_input)) { |
103 | - throw new Exception('Duplicate field in OCRA suite data input: ' . var_export($elem, TRUE)); |
|
103 | + throw new Exception('Duplicate field in OCRA suite data input: '.var_export($elem, TRUE)); |
|
104 | 104 | } |
105 | 105 | $data_input[$letter] = 1; |
106 | 106 | |
@@ -112,13 +112,13 @@ discard block |
||
112 | 112 | } elseif (preg_match('/^Q([AHN])(\d+)$/', $elem, $match)) { |
113 | 113 | $q_len = intval($match[2]); |
114 | 114 | if ($q_len < 4 || $q_len > 64) { |
115 | - throw new Exception('Invalid OCRA suite data input question length: ' . var_export($q_len, TRUE)); |
|
115 | + throw new Exception('Invalid OCRA suite data input question length: '.var_export($q_len, TRUE)); |
|
116 | 116 | } |
117 | 117 | $this->Q = TRUE; |
118 | 118 | $this->QType = $match[1]; |
119 | 119 | $this->QLength = $q_len; |
120 | 120 | } else { |
121 | - throw new Exception('Invalid OCRA suite data input question: ' . var_export($elem, TRUE)); |
|
121 | + throw new Exception('Invalid OCRA suite data input question: '.var_export($elem, TRUE)); |
|
122 | 122 | } |
123 | 123 | } elseif ($letter === 'P') { |
124 | 124 | if (strlen($elem) == 1) { |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | } else { |
127 | 127 | $p_algo = substr($elem, 1); |
128 | 128 | if (!array_key_exists($p_algo, $this->supportedHashFunctions)) { |
129 | - throw new Exception('Unsupported OCRA suite PIN hash function: ' . var_export($elem, TRUE)); |
|
129 | + throw new Exception('Unsupported OCRA suite PIN hash function: '.var_export($elem, TRUE)); |
|
130 | 130 | } |
131 | 131 | $this->P = TRUE; |
132 | 132 | $this->PType = $p_algo; |
@@ -138,13 +138,13 @@ discard block |
||
138 | 138 | } elseif (preg_match('/^S(\d+)$/', $elem, $match)) { |
139 | 139 | $s_len = intval($match[1]); |
140 | 140 | if ($s_len <= 0 || $s_len > 512) { |
141 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($s_len, TRUE)); |
|
141 | + throw new Exception('Invalid OCRA suite data input session information length: '.var_export($s_len, TRUE)); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | $this->S = TRUE; |
145 | 145 | $this->SLength = $s_len; |
146 | 146 | } else { |
147 | - throw new Exception('Invalid OCRA suite data input session information length: ' . var_export($elem, TRUE)); |
|
147 | + throw new Exception('Invalid OCRA suite data input session information length: '.var_export($elem, TRUE)); |
|
148 | 148 | } |
149 | 149 | } elseif ($letter === 'T') { |
150 | 150 | if (strlen($elem) == 1) { |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | preg_match_all('/(\d+)([HMS])/', $elem, $match); |
154 | 154 | |
155 | 155 | if (count($match[1]) !== count(array_unique($match[2]))) { |
156 | - throw new Exception('Duplicate definitions in OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
156 | + throw new Exception('Duplicate definitions in OCRA suite data input timestamp: '.var_export($elem, TRUE)); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | $length = 0; |
@@ -161,21 +161,21 @@ discard block |
||
161 | 161 | $length += intval($match[1][$i]) * $this->TPeriods[$match[2][$i]]; |
162 | 162 | } |
163 | 163 | if ($length <= 0) { |
164 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
164 | + throw new Exception('Invalid OCRA suite data input timestamp: '.var_export($elem, TRUE)); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | $this->T = TRUE; |
168 | 168 | $this->TLength = $length; |
169 | 169 | } else { |
170 | - throw new Exception('Invalid OCRA suite data input timestamp: ' . var_export($elem, TRUE)); |
|
170 | + throw new Exception('Invalid OCRA suite data input timestamp: '.var_export($elem, TRUE)); |
|
171 | 171 | } |
172 | 172 | } else { |
173 | - throw new Exception('Unsupported OCRA suite data input field: ' . var_export($elem, TRUE)); |
|
173 | + throw new Exception('Unsupported OCRA suite data input field: '.var_export($elem, TRUE)); |
|
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | 177 | if (!$this->Q) { |
178 | - throw new Exception('OCRA suite data input question not defined: ' . var_export($s[2], TRUE)); |
|
178 | + throw new Exception('OCRA suite data input question not defined: '.var_export($s[2], TRUE)); |
|
179 | 179 | } |
180 | 180 | } |
181 | 181 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | $bytes = Tiqr_Random::randomBytes($q_length); |
198 | 198 | |
199 | - switch($q_type) { |
|
199 | + switch ($q_type) { |
|
200 | 200 | case 'A': |
201 | 201 | $challenge = base64_encode($bytes); |
202 | 202 | $tr = implode("", unpack('H*', $bytes)); |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $challenge = implode("", unpack('N*', $bytes)); |
210 | 210 | break; |
211 | 211 | default: |
212 | - throw new Exception('Unsupported OCRASuite challenge type: ' . var_export($q_type, TRUE)); |
|
212 | + throw new Exception('Unsupported OCRASuite challenge type: '.var_export($q_type, TRUE)); |
|
213 | 213 | break; |
214 | 214 | } |
215 | 215 | |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | $result &= ($s1[$i] == $s2[$i]); |
234 | 234 | } |
235 | 235 | |
236 | - return (boolean)$result; |
|
236 | + return (boolean) $result; |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | } |
@@ -74,7 +74,7 @@ |
||
74 | 74 | */ |
75 | 75 | public function getPath(): string |
76 | 76 | { |
77 | - if (substr($this->path, -1)!="/") return $this->path."/"; |
|
77 | + if (substr($this->path, -1) != "/") return $this->path."/"; |
|
78 | 78 | return $this->path; |
79 | 79 | } |
80 | 80 | } |
@@ -74,7 +74,9 @@ |
||
74 | 74 | */ |
75 | 75 | public function getPath(): string |
76 | 76 | { |
77 | - if (substr($this->path, -1)!="/") return $this->path."/"; |
|
77 | + if (substr($this->path, -1)!="/") { |
|
78 | + return $this->path."/"; |
|
79 | + } |
|
78 | 80 | return $this->path; |
79 | 81 | } |
80 | 82 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | public function createUser(string $userId, string $displayName) : void |
61 | 61 | { |
62 | 62 | $user = array("userId"=>$userId, |
63 | - "displayName"=>$displayName); |
|
63 | + "displayName"=>$displayName); |
|
64 | 64 | $this->_saveUser($userId, $user); |
65 | 65 | } |
66 | 66 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | { |
91 | 91 | if ($data = $this->_loadUser($userId)) { |
92 | 92 | if (isset($data["notificationType"])) { |
93 | - return $data["notificationType"]; |
|
93 | + return $data["notificationType"]; |
|
94 | 94 | } |
95 | 95 | } |
96 | 96 | return ''; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | { |
114 | 114 | if ($data = $this->_loadUser($userId)) { |
115 | 115 | if (isset($data["notificationAddress"])) { |
116 | - return $data["notificationAddress"]; |
|
116 | + return $data["notificationAddress"]; |
|
117 | 117 | } |
118 | 118 | } |
119 | 119 | $this->logger->info('Unable to find notification address for user'); |
@@ -170,10 +170,10 @@ |
||
170 | 170 | } |
171 | 171 | |
172 | 172 | if ($timestamp + $tempBlockDuration * 60 < time()) { |
173 | - return false; // Temp block expired |
|
173 | + return false; // Temp block expired |
|
174 | 174 | } |
175 | 175 | } |
176 | - return true; // Blocked by temp block |
|
176 | + return true; // Blocked by temp block |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | /** |
@@ -128,7 +128,7 @@ |
||
128 | 128 | * |
129 | 129 | * Note that the $tempBlockDuration is specified in MINUTES |
130 | 130 | */ |
131 | - public function isBlocked(string $userId, int $tempBlockDuration=0): bool; |
|
131 | + public function isBlocked(string $userId, int $tempBlockDuration = 0): bool; |
|
132 | 132 | |
133 | 133 | /** |
134 | 134 | * Block or unblock the user account. |
@@ -29,7 +29,7 @@ |
||
29 | 29 | * @throws ReadWriteException |
30 | 30 | * @throws Exception |
31 | 31 | */ |
32 | - public function setValue(string $key, $value, int $expire=0): void; |
|
32 | + public function setValue(string $key, $value, int $expire = 0): void; |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * Remove $key from the state storage |
@@ -54,8 +54,8 @@ |
||
54 | 54 | } |
55 | 55 | |
56 | 56 | $envelope = array("expire"=>$expire, |
57 | - "createdAt"=>time(), |
|
58 | - "value"=>$value); |
|
57 | + "createdAt"=>time(), |
|
58 | + "value"=>$value); |
|
59 | 59 | $filename = $this->getFilenameByKey($key); |
60 | 60 | |
61 | 61 | if (!file_put_contents($filename, serialize($envelope))) { |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * @see Tiqr_StateStorage_StateStorageInterface::setValue() |
49 | 49 | */ |
50 | - public function setValue(string $key, $value, int $expire=0): void |
|
50 | + public function setValue(string $key, $value, int $expire = 0): void |
|
51 | 51 | { |
52 | 52 | if (empty($key)) { |
53 | 53 | throw new InvalidArgumentException('Empty key not allowed'); |
@@ -109,8 +109,8 @@ discard block |
||
109 | 109 | |
110 | 110 | private function getPath(): string |
111 | 111 | { |
112 | - if (substr($this->path, -1)!=="/") { |
|
113 | - return $this->path . "/"; |
|
112 | + if (substr($this->path, -1) !== "/") { |
|
113 | + return $this->path."/"; |
|
114 | 114 | } |
115 | 115 | return $this->path; |
116 | 116 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * The default configuration |
52 | 52 | */ |
53 | 53 | const DEFAULT_HOST = '127.0.0.1'; |
54 | - const DEFAULT_PORT = 11211; |
|
54 | + const DEFAULT_PORT = 11211; |
|
55 | 55 | |
56 | 56 | /** |
57 | 57 | * Get the prefix to use for all keys in memcache. |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | /** |
104 | 104 | * @see Tiqr_StateStorage_StateStorageInterface::setValue() |
105 | 105 | */ |
106 | - public function setValue(string $key, $value, int $expire=0): void |
|
106 | + public function setValue(string $key, $value, int $expire = 0): void |
|
107 | 107 | { |
108 | 108 | if (empty($key)) { |
109 | 109 | throw new InvalidArgumentException('Empty key not allowed'); |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | if ($result === false) { |
158 | 158 | // Memcache interface does not provide error information, either the key does not exists or |
159 | 159 | // there was an error communicating with the memcache |
160 | - $this->logger->info( sprintf('Unable to get key "%s" from memcache StateStorage', $key) ); |
|
160 | + $this->logger->info(sprintf('Unable to get key "%s" from memcache StateStorage', $key)); |
|
161 | 161 | return null; |
162 | 162 | } |
163 | 163 | return $result; |
@@ -36,8 +36,7 @@ |
||
36 | 36 | // response as the client calculated. |
37 | 37 | try { |
38 | 38 | $expected = OCRA::generateOCRA($this->_ocraSuite, $userSecret, "", $challenge, "", $sessionInformation, ""); |
39 | - } |
|
40 | - catch (Exception $e) { |
|
39 | + } catch (Exception $e) { |
|
41 | 40 | $this->logger->warning(sprintf('Error calculating OCRA response for user "%s"', $userId), array('exception'=>$e)); |
42 | 41 | return false; |
43 | 42 | } |
@@ -81,7 +81,7 @@ |
||
81 | 81 | 'verifyResponse for user "%s" failed', |
82 | 82 | $userId |
83 | 83 | ), |
84 | - array( 'exception' => $e) |
|
84 | + array('exception' => $e) |
|
85 | 85 | ); |
86 | 86 | return false; |
87 | 87 | } |