Conditions | 11 |
Paths | 28 |
Total Lines | 85 |
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 |
||
36 | public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseTypeInterface $responseType, DateInterval $accessTokenTTL) |
||
37 | { |
||
38 | [$clientId] = $this->getClientCredentials($request); |
||
39 | $client = $this->getClientEntityOrFail($clientId, $request); |
||
40 | |||
41 | // Only validate the client if it is confidential |
||
42 | if ($client->isConfidential()) { |
||
43 | $this->validateClient($request); |
||
44 | } |
||
45 | |||
46 | $encryptedAuthCode = $this->getRequestParameter('code', $request, null); |
||
47 | |||
48 | if ($encryptedAuthCode === null) { |
||
49 | throw OAuthServerException::invalidRequest('code'); |
||
50 | } |
||
51 | |||
52 | try { |
||
53 | $authCodePayload = \json_decode($this->decrypt($encryptedAuthCode)); |
||
54 | |||
55 | $this->validateAuthorizationCode($authCodePayload, $client, $request); |
||
56 | |||
57 | $scopes = $this->scopeRepository->finalizeScopes( |
||
58 | $this->validateScopes($authCodePayload->scopes), |
||
59 | $this->getIdentifier(), |
||
60 | $client, |
||
61 | $authCodePayload->user_id |
||
62 | ); |
||
63 | |||
64 | } catch (LogicException $e) { |
||
65 | throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the authorization code', $e); |
||
66 | } |
||
67 | |||
68 | // Validate code challenge |
||
69 | if (!empty($authCodePayload->code_challenge)) { |
||
70 | $codeVerifier = $this->getRequestParameter('code_verifier', $request, null); |
||
71 | |||
72 | if ($codeVerifier === null) { |
||
73 | throw OAuthServerException::invalidRequest('code_verifier'); |
||
74 | } |
||
75 | |||
76 | // Validate code_verifier according to RFC-7636 |
||
77 | // @see: https://tools.ietf.org/html/rfc7636#section-4.1 |
||
78 | if (preg_match('/^[A-Za-z0-9-._~]{43,128}$/', $codeVerifier) !== 1) { |
||
79 | throw OAuthServerException::invalidRequest( |
||
80 | 'code_verifier', |
||
81 | 'Code Verifier must follow the specifications of RFC-7636.' |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | if (property_exists($authCodePayload, 'code_challenge_method')) { |
||
86 | if (isset($this->codeChallengeVerifiers[$authCodePayload->code_challenge_method])) { |
||
87 | $codeChallengeVerifier = $this->codeChallengeVerifiers[$authCodePayload->code_challenge_method]; |
||
88 | |||
89 | if ($codeChallengeVerifier->verifyCodeChallenge($codeVerifier, $authCodePayload->code_challenge) === false) { |
||
90 | throw OAuthServerException::invalidGrant('Failed to verify `code_verifier`.'); |
||
91 | } |
||
92 | } else { |
||
93 | throw OAuthServerException::serverError( |
||
94 | sprintf( |
||
95 | 'Unsupported code challenge method `%s`', |
||
96 | $authCodePayload->code_challenge_method |
||
97 | ) |
||
98 | ); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // Issue and persist new access token |
||
104 | $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $authCodePayload->user_id, $scopes); |
||
105 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request)); |
||
106 | $responseType->setAccessToken($accessToken); |
||
107 | |||
108 | // Issue and persist new refresh token if given |
||
109 | $refreshToken = $this->issueRefreshToken($accessToken); |
||
110 | |||
111 | if ($refreshToken !== null) { |
||
112 | $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request)); |
||
113 | $responseType->setRefreshToken($refreshToken); |
||
114 | } |
||
115 | |||
116 | // Revoke used auth code |
||
117 | $this->authCodeRepository->revokeAuthCode($authCodePayload->auth_code_id); |
||
118 | |||
119 | return $responseType; |
||
120 | } |
||
121 | |||
174 |