Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Apikey | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
1 / 1 |
getDefaults | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setApiClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getApiClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHash | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isVerifiedHash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
withCaptchaData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getQuotaPositionByRoute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
addQuota | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
updateQuota | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities; |
4 | |
5 | class Apikey extends Schema\Entity |
6 | { |
7 | const PRIMARY = 'key'; |
8 | |
9 | public static $schema = "apikey.json"; |
10 | |
11 | public function getDefaults() |
12 | { |
13 | return [ |
14 | 'apiclient' => new Apiclient(), |
15 | ]; |
16 | } |
17 | |
18 | public function setApiClient(Apiclient $apiClient) |
19 | { |
20 | $this['apiclient'] = $apiClient; |
21 | } |
22 | |
23 | public function getApiClient(): Apiclient |
24 | { |
25 | return $this['apiclient']; |
26 | } |
27 | |
28 | /** |
29 | * @param String $string usually the captcha text or a token |
30 | * @param String $secret use a secret to avoid crafted api keys |
31 | */ |
32 | public function getHash($string, $secret = '4Td8x5Qn5hjW3uSc6MWWVQPwrw6b74fL') |
33 | { |
34 | $hash = password_hash($string . $secret, PASSWORD_BCRYPT); |
35 | $hash = substr($hash, 7); |
36 | return base64_encode($hash); |
37 | } |
38 | |
39 | /** |
40 | * @param String $text usually the captcha text |
41 | * @param String $hash usually the apikey |
42 | * @param String $secret has to be same secret used by self::getHashFromCaptcha() |
43 | */ |
44 | public function isVerifiedHash($text, $hash, $secret = '4Td8x5Qn5hjW3uSc6MWWVQPwrw6b74fL') |
45 | { |
46 | return password_verify($text . $secret, '$2y$10$' . base64_decode($hash)); |
47 | } |
48 | |
49 | public function withCaptchaData($base64_jpg) |
50 | { |
51 | $this->captcha = new Mimepart([ |
52 | 'content' => $base64_jpg, |
53 | 'mime' => 'image/jpeg;base64', |
54 | 'base64' => true |
55 | ]); |
56 | return $this; |
57 | } |
58 | |
59 | public function getQuotaPositionByRoute($route) |
60 | { |
61 | return (isset($this->quota) && is_array($this->quota)) ? |
62 | array_search($route, array_column($this->quota, 'route')) |
63 | : false; |
64 | } |
65 | |
66 | public function addQuota($route, $period) |
67 | { |
68 | $this->quota[] = [ |
69 | 'route' => $route, |
70 | 'period' => $period, |
71 | 'requests' => 1 |
72 | ]; |
73 | return $this; |
74 | } |
75 | |
76 | public function updateQuota($position) |
77 | { |
78 | $this->quota[$position]['requests']++; |
79 | return $this; |
80 | } |
81 | } |