| Conditions | 12 |
| Paths | 7 |
| Total Lines | 122 |
| Code Lines | 60 |
| 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 | // IMPORTANT: Password should NOT be escaped/sanitized - treat as opaque binary data |
||
| 53 | // Only trim whitespace which is safe and expected (fix 3.1.5.10) |
||
| 54 | include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
||
| 55 | $inputData = dataSanitizer( |
||
| 56 | [ |
||
| 57 | 'login' => isset($login) === true ? $login : '', |
||
| 58 | 'password' => isset($password) === true ? $password : '', |
||
| 59 | 'apikey' => isset($apikey) === true ? $apikey : '', |
||
| 60 | ], |
||
| 61 | [ |
||
| 62 | 'login' => 'trim|escape|strip_tags', |
||
| 63 | 'password' => 'trim', // Only trim, NO escape/sanitization |
||
| 64 | 'apikey' => 'trim|escape|strip_tags', |
||
| 65 | ] |
||
| 66 | ); |
||
| 67 | |||
| 68 | // Check apikey and credentials |
||
| 69 | if (empty($inputData['login']) === true || empty($inputData['apikey']) === true || empty($inputData['password']) === true) { |
||
| 70 | // case where it is a generic key |
||
| 71 | // Not allowed to use this API |
||
| 72 | |||
| 73 | return ["error" => "Login failed.", "info" => "User password is requested"]; |
||
| 74 | } else { |
||
| 75 | // case where it is a user api key |
||
| 76 | // Check if user exists |
||
| 77 | $userInfo = getUserCompleteData($inputData['login']); |
||
| 78 | |||
| 79 | if ($userInfo === null) { |
||
| 80 | return ["error" => "Login failed.", "info" => "Credentials not valid"]; |
||
| 81 | } |
||
| 82 | |||
| 83 | // Check if user is enabled |
||
| 84 | if ((int) $userInfo['api_enabled'] === 0) { |
||
| 85 | return ["error" => "Login failed.", "info" => "User not allowed to use API"]; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Check password |
||
| 89 | $passwordManager = new PasswordManager(); |
||
| 90 | if ($passwordManager->verifyPassword($userInfo['pw'], $inputData['password']) === true) { |
||
| 91 | // Correct credentials |
||
| 92 | // get user keys |
||
| 93 | $privateKeyClear = decryptPrivateKey($inputData['password'], (string) $userInfo['private_key']); |
||
| 94 | |||
| 95 | // check API key |
||
| 96 | if ($inputData['apikey'] !== base64_decode(decryptUserObjectKey($userInfo['api_key'], $privateKeyClear))) { |
||
| 97 | return ["error" => "Login failed.", "info" => "API Key not valid"]; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Update user's key_tempo |
||
| 101 | $keyTempo = bin2hex(random_bytes(16)); |
||
| 102 | /*DB::update( |
||
| 103 | prefixTable('users'), |
||
| 104 | [ |
||
| 105 | 'key_tempo' => $keyTempo, |
||
| 106 | ], |
||
| 107 | 'id = %i', |
||
| 108 | $userInfo['id'] |
||
| 109 | );*/ |
||
| 110 | |||
| 111 | // Generate a unique session key for this API session (256 bits / 32 bytes) |
||
| 112 | // This key will be stored in the JWT and used to decrypt the private key |
||
| 113 | $sessionKey = random_bytes(32); |
||
| 114 | $sessionKeySalt = bin2hex(random_bytes(16)); |
||
| 115 | |||
| 116 | // Encrypt the decrypted private key with the session key |
||
| 117 | // This allows us to store it securely in the database without exposing it |
||
| 118 | require_once API_ROOT_PATH . '/inc/encryption_utils.php'; |
||
| 119 | $encryptedPrivateKey = encrypt_with_session_key($privateKeyClear, $sessionKey); |
||
| 120 | |||
| 121 | if ($encryptedPrivateKey === false) { |
||
| 122 | return ["error" => "Login failed.", "info" => "Failed to encrypt private key"]; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Store the ENCRYPTED private key in the API table |
||
| 126 | // Even if the database is compromised, the key cannot be used without the session_key from the JWT |
||
| 127 | DB::update( |
||
| 128 | prefixTable('api'), |
||
| 129 | [ |
||
| 130 | 'encrypted_private_key' => $encryptedPrivateKey, |
||
| 131 | 'session_key_salt' => $sessionKeySalt, |
||
| 132 | 'session_key' => $keyTempo, |
||
| 133 | 'timestamp' => time(), |
||
| 134 | ], |
||
| 135 | 'user_id = %i', |
||
| 136 | $userInfo['id'] |
||
| 137 | ); |
||
| 138 | |||
| 139 | // get user folders list |
||
| 140 | $ret = $this->buildUserFoldersList($userInfo); |
||
| 141 | |||
| 142 | // Load config |
||
| 143 | $configManager = new ConfigManager(); |
||
| 144 | $SETTINGS = $configManager->getAllSettings(); |
||
| 145 | |||
| 146 | // Log user |
||
| 147 | logEvents($SETTINGS, 'api', 'user_connection', (string) $userInfo['id'], stripslashes($userInfo['login'])); |
||
| 148 | |||
| 149 | // create JWT with session key |
||
| 150 | return $this->createUserJWT( |
||
| 151 | (int) $userInfo['id'], |
||
| 152 | (string) $inputData['login'], |
||
| 153 | (int) $userInfo['personal_folder'], |
||
| 154 | (string) implode(",", $ret['folders']), |
||
| 155 | (string) implode(",", $ret['items']), |
||
| 156 | (string) $keyTempo, |
||
| 157 | (string) base64_encode($sessionKey), // Session key for decrypting private key |
||
| 158 | (int) $userInfo['admin'], |
||
| 159 | (int) $userInfo['gestionnaire'], |
||
| 160 | (int) $userInfo['can_create_root_folder'], |
||
| 161 | (int) $userInfo['can_manage_all_users'], |
||
| 162 | (string) $userInfo['fonction_id'], |
||
| 163 | (string) $userInfo['api_allowed_folders'], |
||
| 164 | (int) $userInfo['api_allowed_to_create'], |
||
| 165 | (int) $userInfo['api_allowed_to_read'], |
||
| 166 | (int) $userInfo['api_allowed_to_update'], |
||
| 167 | (int) $userInfo['api_allowed_to_delete'], |
||
| 168 | ); |
||
| 169 | } else { |
||
| 170 | return ["error" => "Login failed.", "info" => "Credentials not valid"]; |
||
| 171 | } |
||
| 377 | } |