Conditions | 11 |
Paths | 6 |
Total Lines | 98 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function getUserAuth(string $login, string $password, string $apikey): array |
||
50 | { |
||
51 | // Sanitize |
||
52 | include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
||
53 | $inputData = dataSanitizer( |
||
54 | [ |
||
55 | 'login' => isset($login) === true ? $login : '', |
||
56 | 'password' => isset($password) === true ? $password : '', |
||
57 | 'apikey' => isset($apikey) === true ? $apikey : '', |
||
58 | ], |
||
59 | [ |
||
60 | 'login' => 'trim|escape|strip_tags', |
||
61 | 'password' => 'trim|escape', |
||
62 | 'apikey' => 'trim|escape|strip_tags', |
||
63 | ] |
||
64 | ); |
||
65 | |||
66 | // Check apikey and credentials |
||
67 | if (empty($inputData['login']) === true || empty($inputData['apikey']) === true || empty($inputData['password']) === true) { |
||
68 | // case where it is a generic key |
||
69 | // Not allowed to use this API |
||
70 | |||
71 | return ["error" => "Login failed.", "info" => "User password is requested"]; |
||
72 | } else { |
||
73 | // case where it is a user api key |
||
74 | // Check if user exists |
||
75 | $userInfo = DB::queryfirstrow( |
||
76 | "SELECT u.id, u.pw, u.login, u.admin, u.gestionnaire, u.can_manage_all_users, u.fonction_id, u.can_create_root_folder, u.public_key, u.private_key, u.personal_folder, u.fonction_id, u.groupes_visibles, u.groupes_interdits, a.value AS user_api_key, a.allowed_folders as user_api_allowed_folders, a.enabled, a.allowed_to_create, a.allowed_to_read, a.allowed_to_update, a.allowed_to_delete |
||
77 | FROM " . prefixTable('users') . " AS u |
||
78 | INNER JOIN " . prefixTable('api') . " AS a ON (a.user_id=u.id) |
||
79 | WHERE login = %s", |
||
80 | $inputData['login'] |
||
81 | ); |
||
82 | if (DB::count() === 0) { |
||
83 | return ["error" => "Login failed.", "info" => "apikey : Not valid"]; |
||
84 | } |
||
85 | |||
86 | // Check if user is enabled |
||
87 | if ((int) $userInfo['enabled'] === 0) { |
||
88 | return ["error" => "Login failed.", "info" => "User not allowed to use API"]; |
||
89 | } |
||
90 | |||
91 | // Check password |
||
92 | $passwordManager = new PasswordManager(); |
||
93 | if ($passwordManager->verifyPassword($userInfo['pw'], $inputData['password']) === true) { |
||
94 | // Correct credentials |
||
95 | // get user keys |
||
96 | $privateKeyClear = decryptPrivateKey($inputData['password'], (string) $userInfo['private_key']); |
||
97 | |||
98 | // check API key |
||
99 | if ($inputData['apikey'] !== base64_decode(decryptUserObjectKey($userInfo['user_api_key'], $privateKeyClear))) { |
||
100 | return ["error" => "Login failed.", "apikey" => "Not valid"]; |
||
101 | } |
||
102 | |||
103 | // Update user's key_tempo |
||
104 | $keyTempo = bin2hex(random_bytes(16)); |
||
105 | DB::update( |
||
106 | prefixTable('users'), |
||
107 | [ |
||
108 | 'key_tempo' => $keyTempo, |
||
109 | ], |
||
110 | 'id = %i', |
||
111 | $userInfo['id'] |
||
112 | ); |
||
113 | |||
114 | // get user folders list |
||
115 | $ret = $this->buildUserFoldersList($userInfo); |
||
116 | |||
117 | // Load config |
||
118 | $configManager = new ConfigManager(); |
||
119 | $SETTINGS = $configManager->getAllSettings(); |
||
120 | |||
121 | // Log user |
||
122 | logEvents($SETTINGS, 'api', 'user_connection', (string) $userInfo['id'], stripslashes($userInfo['login'])); |
||
123 | |||
124 | // create JWT |
||
125 | return $this->createUserJWT( |
||
126 | (int) $userInfo['id'], |
||
127 | (string) $inputData['login'], |
||
128 | (int) $userInfo['personal_folder'], |
||
129 | (string) $userInfo['public_key'], |
||
130 | (string) $privateKeyClear, |
||
131 | (string) implode(",", $ret['folders']), |
||
132 | (string) implode(",", $ret['items']), |
||
133 | (string) $keyTempo, |
||
134 | (int) $userInfo['admin'], |
||
135 | (int) $userInfo['gestionnaire'], |
||
136 | (int) $userInfo['can_create_root_folder'], |
||
137 | (int) $userInfo['can_manage_all_users'], |
||
138 | (string) $userInfo['fonction_id'], |
||
139 | (string) $userInfo['user_api_allowed_folders'], |
||
140 | (int) $userInfo['allowed_to_create'], |
||
141 | (int) $userInfo['allowed_to_read'], |
||
142 | (int) $userInfo['allowed_to_update'], |
||
143 | (int) $userInfo['allowed_to_delete'], |
||
144 | ); |
||
145 | } else { |
||
146 | return ["error" => "Login failed.", "info" => "password : Not valid"]; |
||
147 | } |
||
345 | } |