| Conditions | 6 |
| Paths | 6 |
| Total Lines | 59 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 42 | public function handle(GetResponseEvent $event) |
||
| 43 | { |
||
| 44 | $request = $event->getRequest(); |
||
| 45 | if (!$request->isMethod('POST')) { |
||
| 46 | throw new HttpException(405, "Only POST method is allowed for JWT authentication"); |
||
| 47 | } |
||
| 48 | |||
| 49 | $username = $request->request->get('username', null); |
||
| 50 | $password = $request->request->get('password', null); |
||
| 51 | |||
| 52 | try { |
||
| 53 | $token = $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey)); |
||
| 54 | } catch (\InvalidArgumentException $e) { |
||
| 55 | // most probably failed to find user by these credentials |
||
| 56 | // let other unexpected exceptions pass through |
||
| 57 | throw new HttpException(JsonResponse::HTTP_UNAUTHORIZED, "Username or password is not valid.", $e); |
||
| 58 | } |
||
| 59 | |||
| 60 | $user = $token->getUser(); |
||
| 61 | |||
| 62 | $header = []; |
||
| 63 | |||
| 64 | // jwt token data |
||
| 65 | $payload = [ |
||
| 66 | 'username' => $user->getUsername(), |
||
| 67 | 'exp' => (new \DateTime('+1 day'))->format('U'), |
||
| 68 | 'iat' => (new \DateTime('now'))->format('U'), |
||
| 69 | ]; |
||
| 70 | |||
| 71 | // build jwt data to sign |
||
| 72 | $toSign = implode('.', array_map('base64_encode', array_map('json_encode', [$header, $payload]))); |
||
| 73 | |||
| 74 | // init openssl private key resource |
||
| 75 | $key = openssl_pkey_get_private($this->privkey, $this->passphrase); |
||
| 76 | if (!is_resource($key)) { |
||
| 77 | throw new HttpException(500, "not valid private key, {$this->privkey}"); |
||
| 78 | } |
||
| 79 | |||
| 80 | // ensure key is valid RSA private key |
||
| 81 | if (openssl_pkey_get_details($key)['type'] !== JWTUserToken::KEY_TYPE) { |
||
| 82 | throw new HttpException(500, "Only RSA keys are supported."); |
||
| 83 | } |
||
| 84 | |||
| 85 | // create signature |
||
| 86 | $signature = null; |
||
| 87 | if (!openssl_sign($toSign, $signature, $key, JWTUserToken::ALGO)) { |
||
| 88 | throw new HttpException(500, "could not sign JWT."); |
||
| 89 | } |
||
| 90 | |||
| 91 | // create jwt token |
||
| 92 | $jwt = implode('.', [$toSign, base64_encode($signature)]); |
||
| 93 | |||
| 94 | // finally create response |
||
| 95 | $event->setResponse(new JsonResponse([ |
||
| 96 | 'token' => $jwt, |
||
| 97 | 'id' => $user->getId(), |
||
| 98 | 'roles' => $user->getRoles(), |
||
| 99 | ])); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |