1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Controller; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Base\Config; |
22
|
|
|
use CoreModules\Admin\Model\User; |
23
|
|
|
use Firebase\JWT\JWT; |
24
|
|
|
use Luracast\Restler\RestException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ApiController. The generic API controller contains what is necessary for any API controller |
28
|
|
|
* |
29
|
|
|
* @package Alxarafe\Base |
30
|
|
|
*/ |
31
|
|
|
abstract class ApiController |
32
|
|
|
{ |
33
|
|
|
private static $security_key=null; |
34
|
|
|
|
35
|
|
|
protected static function getSecurityKey() |
36
|
|
|
{ |
37
|
|
|
if (self::$security_key !== null) { |
38
|
|
|
return self::$security_key; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$config = Config::getConfig(); |
42
|
|
|
if (!isset($config->security->jwt_secret_key)) { |
43
|
|
|
$config->security->jwt_secret_key = bin2hex(random_bytes(20)); |
44
|
|
|
if (!Config::setConfig($config)) { |
45
|
|
|
static::badApiCall(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
self::$security_key = $config->security->jwt_secret_key; |
50
|
|
|
return self::$security_key; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function badApiCall() |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function jsonResponse($response, $httpCode = 200) { |
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.