TokenRequest.php (5813B)
1 <?php 2 3 /** 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 namespace fkooman\OAuth\Client; 19 20 class TokenRequest 21 { 22 /** @var \Guzzle\Http\Client */ 23 private $c; 24 25 /** @var \fkooman\OAuth\Client\ClientConfigInterface */ 26 private $clientConfig; 27 28 public function __construct(\Guzzle\Http\Client $c, ClientConfigInterface $clientConfig) 29 { 30 $this->c = $c; 31 $this->clientConfig = $clientConfig; 32 } 33 34 public function withAuthorizationCode($authorizationCode) 35 { 36 $p = array ( 37 "code" => $authorizationCode, 38 "grant_type" => "authorization_code", 39 ); 40 if (null !== $this->clientConfig->getRedirectUri()) { 41 $p['redirect_uri'] = $this->clientConfig->getRedirectUri(); 42 } 43 44 return $this->accessTokenRequest($p); 45 } 46 47 public function withRefreshToken($refreshToken) 48 { 49 $p = array ( 50 "refresh_token" => $refreshToken, 51 "grant_type" => "refresh_token", 52 ); 53 // Some services require specifying the redirect_uri also when using 54 // the refresh_token. 55 // issue: https://github.com/fkooman/php-oauth-client/issues/20 56 if ($this->clientConfig->getUseRedirectUriOnRefreshTokenRequest()) { 57 if (null !== $this->clientConfig->getRedirectUri()) { 58 $p['redirect_uri'] = $this->clientConfig->getRedirectUri(); 59 } 60 } 61 62 return $this->accessTokenRequest($p); 63 } 64 65 private function accessTokenRequest(array $p) 66 { 67 if ($this->clientConfig->getCredentialsInRequestBody()) { 68 // provide credentials in the POST body 69 $p['client_id'] = $this->clientConfig->getClientId(); 70 $p['client_secret'] = $this->clientConfig->getClientSecret(); 71 } else { 72 // use basic authentication 73 $curlAuth = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin( 74 $this->clientConfig->getClientId(), 75 $this->clientConfig->getClientSecret() 76 ); 77 $this->c->addSubscriber($curlAuth); 78 } 79 80 try { 81 $request = $this->c->post($this->clientConfig->getTokenEndpoint()); 82 $request->addPostFields($p); 83 $request->addHeader('Accept', 'application/json'); 84 85 $responseData = $request->send()->json(); 86 87 // some servers do not provide token_type, so we allow for setting 88 // a default 89 // issue: https://github.com/fkooman/php-oauth-client/issues/13 90 if (null !== $this->clientConfig->getDefaultTokenType()) { 91 if (is_array($responseData) && !isset($responseData['token_type'])) { 92 $responseData['token_type'] = $this->clientConfig->getDefaultTokenType(); 93 } 94 } 95 96 // if the field "expires_in" has the value null, remove it 97 // issue: https://github.com/fkooman/php-oauth-client/issues/17 98 if ($this->clientConfig->getAllowNullExpiresIn()) { 99 if (is_array($responseData) && array_key_exists("expires_in", $responseData)) { 100 if (null === $responseData['expires_in']) { 101 unset($responseData['expires_in']); 102 } 103 } 104 } 105 106 // if the field "scope" is empty string a default can be set 107 // through the client configuration 108 // issue: https://github.com/fkooman/php-oauth-client/issues/20 109 if (null !== $this->clientConfig->getDefaultServerScope()) { 110 if (is_array($responseData) && isset($responseData['scope']) && '' === $responseData['scope']) { 111 $responseData['scope'] = $this->clientConfig->getDefaultServerScope(); 112 } 113 } 114 115 // the service can return a string value of the expires_in 116 // parameter, allow to convert to number instead 117 // issue: https://github.com/fkooman/php-oauth-client/issues/40 118 if ($this->clientConfig->getAllowStringExpiresIn()) { 119 if (is_array($responseData) && isset($responseData['expires_in']) && is_string($responseData['expires_in'])) { 120 $responseData['expires_in'] = intval($responseData['expires_in']); 121 } 122 } 123 124 if ($this->clientConfig->getUseCommaSeparatedScope()) { 125 if (is_array($responseData) && isset($responseData['scope'])) { 126 $responseData['scope'] = str_replace(",", " ", $responseData['scope']); 127 } 128 } 129 130 // issue: https://github.com/fkooman/php-oauth-client/issues/41 131 if ($this->clientConfig->getUseArrayScope()) { 132 if (is_array($responseData) && isset($responseData['scope'])) { 133 if (is_array($responseData['scope'])) { 134 $responseData['scope'] = implode(" ", $responseData['scope']); 135 } 136 } 137 } 138 139 return new TokenResponse($responseData); 140 } catch (\Guzzle\Common\Exception\RuntimeException $e) { 141 return false; 142 } 143 } 144 }