| Conditions | 12 |
| Paths | 7 |
| Total Lines | 124 |
| Code Lines | 68 |
| 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 | // Generate a unique session key for this API session (256 bits / 32 bytes) |
||
| 115 | // This key will be stored in the JWT and used to decrypt the private key |
||
| 116 | $sessionKey = random_bytes(32); |
||
| 117 | $sessionKeySalt = bin2hex(random_bytes(16)); |
||
| 118 | |||
| 119 | // Encrypt the decrypted private key with the session key |
||
| 120 | // This allows us to store it securely in the database without exposing it |
||
| 121 | require_once API_ROOT_PATH . '/inc/encryption_utils.php'; |
||
| 122 | $encryptedPrivateKey = encrypt_with_session_key($privateKeyClear, $sessionKey); |
||
| 123 | |||
| 124 | if ($encryptedPrivateKey === false) { |
||
| 125 | return ["error" => "Login failed.", "info" => "Failed to encrypt private key"]; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Store the ENCRYPTED private key in the API table |
||
| 129 | // Even if the database is compromised, the key cannot be used without the session_key from the JWT |
||
| 130 | DB::update( |
||
| 131 | prefixTable('api'), |
||
| 132 | [ |
||
| 133 | 'encrypted_private_key' => $encryptedPrivateKey, |
||
| 134 | 'session_key_salt' => $sessionKeySalt, |
||
| 135 | 'timestamp' => time(), |
||
| 136 | ], |
||
| 137 | 'user_id = %i', |
||
| 138 | $userInfo['id'] |
||
| 139 | ); |
||
| 140 | |||
| 141 | // get user folders list |
||
| 142 | $ret = $this->buildUserFoldersList($userInfo); |
||
| 143 | |||
| 144 | // Load config |
||
| 145 | $configManager = new ConfigManager(); |
||
| 146 | $SETTINGS = $configManager->getAllSettings(); |
||
| 147 | |||
| 148 | // Log user |
||
| 149 | logEvents($SETTINGS, 'api', 'user_connection', (string) $userInfo['id'], stripslashes($userInfo['login'])); |
||
| 150 | |||
| 151 | // create JWT with session key |
||
| 152 | return $this->createUserJWT( |
||
| 153 | (int) $userInfo['id'], |
||
| 154 | (string) $inputData['login'], |
||
| 155 | (int) $userInfo['personal_folder'], |
||
| 156 | (string) implode(",", $ret['folders']), |
||
| 157 | (string) implode(",", $ret['items']), |
||
| 158 | (string) $keyTempo, |
||
| 159 | (string) base64_encode($sessionKey), // Session key for decrypting private key |
||
| 160 | (int) $userInfo['admin'], |
||
| 161 | (int) $userInfo['gestionnaire'], |
||
| 162 | (int) $userInfo['can_create_root_folder'], |
||
| 163 | (int) $userInfo['can_manage_all_users'], |
||
| 164 | (string) $userInfo['fonction_id'], |
||
| 165 | (string) $userInfo['user_api_allowed_folders'], |
||
| 166 | (int) $userInfo['allowed_to_create'], |
||
| 167 | (int) $userInfo['allowed_to_read'], |
||
| 168 | (int) $userInfo['allowed_to_update'], |
||
| 169 | (int) $userInfo['allowed_to_delete'], |
||
| 170 | ); |
||
| 171 | } else { |
||
| 172 | return ["error" => "Login failed.", "info" => "password : Not valid"]; |
||
| 173 | } |
||
| 379 | } |