| Conditions | 7 |
| Paths | 1 |
| Total Lines | 181 |
| Code Lines | 99 |
| Lines | 55 |
| Ratio | 30.39 % |
| 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 |
||
| 46 | public function init(Service $service) |
||
| 47 | { |
||
| 48 | $service->get( |
||
| 49 | '/user_list', |
||
| 50 | function (Request $request, array $hookData) { |
||
| 51 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 52 | |||
| 53 | return new ApiResponse('user_list', $this->storage->getUsers()); |
||
| 54 | } |
||
| 55 | ); |
||
| 56 | |||
| 57 | $service->post( |
||
| 58 | '/set_totp_secret', |
||
| 59 | function (Request $request, array $hookData) { |
||
| 60 | AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
||
| 61 | |||
| 62 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 63 | $totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret')); |
||
| 64 | $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
||
| 65 | |||
| 66 | $otp = new Otp(); |
||
| 67 | if (false === $otp->checkTotp(Base32::decode($totpSecret), $totpKey)) { |
||
| 68 | // wrong otp key |
||
| 69 | return new ApiErrorResponse('set_totp_secret', 'invalid OTP key'); |
||
| 70 | } |
||
| 71 | |||
| 72 | // XXX use DateTime here, easier for testing |
||
| 73 | |||
| 74 | // XXX check if all these things worked! |
||
| 75 | |||
| 76 | View Code Duplication | if (false === $this->storage->recordTotpKey($userId, $totpKey, time())) { |
|
|
|
|||
| 77 | return new ApiErrorResponse('set_totp_secret', 'OTP key replay'); |
||
| 78 | } |
||
| 79 | $this->storage->setTotpSecret($userId, $totpSecret); |
||
| 80 | |||
| 81 | return new ApiResponse('set_totp_secret'); |
||
| 82 | } |
||
| 83 | ); |
||
| 84 | |||
| 85 | $service->post( |
||
| 86 | '/verify_totp_key', |
||
| 87 | function (Request $request, array $hookData) { |
||
| 88 | AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
||
| 89 | |||
| 90 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 91 | $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
||
| 92 | |||
| 93 | if (!$this->storage->hasTotpSecret($userId)) { |
||
| 94 | return new ApiErrorResponse('verify_totp_key', 'user has no OTP secret'); |
||
| 95 | } |
||
| 96 | $totpSecret = $this->storage->getTotpSecret($userId); |
||
| 97 | |||
| 98 | $otp = new Otp(); |
||
| 99 | if (!$otp->checkTotp(Base32::decode($totpSecret), $totpKey)) { |
||
| 100 | return new ApiErrorResponse('verify_totp_key', 'invalid OTP key'); |
||
| 101 | } |
||
| 102 | |||
| 103 | View Code Duplication | if (false === $this->storage->recordTotpKey($userId, $totpKey, time())) { |
|
| 104 | return new ApiErrorResponse('verify_totp_key', 'OTP key replay'); |
||
| 105 | } |
||
| 106 | |||
| 107 | return new ApiResponse('verify_totp_key'); |
||
| 108 | } |
||
| 109 | ); |
||
| 110 | |||
| 111 | $service->get( |
||
| 112 | '/has_totp_secret', |
||
| 113 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 114 | AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
||
| 115 | |||
| 116 | $userId = InputValidation::userId($request->getQueryParameter('user_id')); |
||
| 117 | |||
| 118 | return new ApiResponse('has_totp_secret', $this->storage->hasTotpSecret($userId)); |
||
| 119 | } |
||
| 120 | ); |
||
| 121 | |||
| 122 | $service->post( |
||
| 123 | '/delete_totp_secret', |
||
| 124 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 125 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 126 | |||
| 127 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 128 | |||
| 129 | return new ApiResponse('delete_totp_secret', $this->storage->deleteTotpSecret($userId)); |
||
| 130 | } |
||
| 131 | ); |
||
| 132 | |||
| 133 | $service->post( |
||
| 134 | '/set_voot_token', |
||
| 135 | function (Request $request, array $hookData) { |
||
| 136 | AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
||
| 137 | |||
| 138 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 139 | $vootToken = InputValidation::vootToken($request->getPostParameter('voot_token')); |
||
| 140 | |||
| 141 | return new ApiResponse('set_voot_token', $this->storage->setVootToken($userId, $vootToken)); |
||
| 142 | } |
||
| 143 | ); |
||
| 144 | |||
| 145 | $service->post( |
||
| 146 | '/delete_voot_token', |
||
| 147 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 148 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 149 | |||
| 150 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 151 | |||
| 152 | return new ApiResponse('delete_voot_token', $this->storage->deleteVootToken($userId)); |
||
| 153 | } |
||
| 154 | ); |
||
| 155 | |||
| 156 | $service->get( |
||
| 157 | '/has_voot_token', |
||
| 158 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 159 | AuthUtils::requireUser($hookData, ['vpn-user-portal', 'vpn-admin-portal']); |
||
| 160 | |||
| 161 | $userId = InputValidation::userId($request->getQueryParameter('user_id')); |
||
| 162 | |||
| 163 | return new ApiResponse('has_voot_token', $this->storage->hasVootToken($userId)); |
||
| 164 | } |
||
| 165 | ); |
||
| 166 | |||
| 167 | $service->get( |
||
| 168 | '/is_disabled_user', |
||
| 169 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 170 | AuthUtils::requireUser($hookData, ['vpn-admin-portal', 'vpn-user-portal']); |
||
| 171 | |||
| 172 | $userId = InputValidation::userId($request->getQueryParameter('user_id')); |
||
| 173 | |||
| 174 | return new ApiResponse('is_disabled_user', $this->storage->isDisabledUser($userId)); |
||
| 175 | } |
||
| 176 | ); |
||
| 177 | |||
| 178 | $service->post( |
||
| 179 | '/disable_user', |
||
| 180 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 181 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 182 | |||
| 183 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 184 | |||
| 185 | return new ApiResponse('disable_user', $this->storage->disableUser($userId)); |
||
| 186 | } |
||
| 187 | ); |
||
| 188 | |||
| 189 | $service->post( |
||
| 190 | '/enable_user', |
||
| 191 | function (Request $request, array $hookData) { |
||
| 192 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 193 | |||
| 194 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 195 | |||
| 196 | return new ApiResponse('enable_user', $this->storage->enableUser($userId)); |
||
| 197 | } |
||
| 198 | ); |
||
| 199 | |||
| 200 | $service->post( |
||
| 201 | '/delete_user', |
||
| 202 | View Code Duplication | function (Request $request, array $hookData) { |
|
| 203 | AuthUtils::requireUser($hookData, ['vpn-admin-portal']); |
||
| 204 | |||
| 205 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
||
| 206 | |||
| 207 | return new ApiResponse('delete_user', $this->storage->deleteUser($userId)); |
||
| 208 | } |
||
| 209 | ); |
||
| 210 | |||
| 211 | $service->get( |
||
| 212 | '/user_groups', |
||
| 213 | function (Request $request, array $hookData) { |
||
| 214 | AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
||
| 215 | |||
| 216 | $userId = $request->getQueryParameter('user_id'); |
||
| 217 | |||
| 218 | $userGroups = []; |
||
| 219 | foreach ($this->groupProviders as $groupProvider) { |
||
| 220 | $userGroups = array_merge($userGroups, $groupProvider->getGroups($userId)); |
||
| 221 | } |
||
| 222 | |||
| 223 | return new ApiResponse('user_groups', $userGroups); |
||
| 224 | } |
||
| 225 | ); |
||
| 226 | } |
||
| 227 | } |
||
| 228 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.