private function accessTokenRequest(array $p) { if ($this->clientConfig->getCredentialsInRequestBody()) { // provide credentials in the POST body $p['client_id'] = $this->clientConfig->getClientId(); $p['client_secret'] = $this->clientConfig->getClientSecret(); } else { // use basic authentication $curlAuth = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin($this->clientConfig->getClientId(), $this->clientConfig->getClientSecret()); $this->c->addSubscriber($curlAuth); } try { $request = $this->c->post($this->clientConfig->getTokenEndpoint()); $request->addPostFields($p); $request->addHeader('Accept', 'application/json'); $responseData = $request->send()->json(); // some servers do not provide token_type, so we allow for setting // a default // issue: https://github.com/fkooman/php-oauth-client/issues/13 if (null !== $this->clientConfig->getDefaultTokenType()) { if (is_array($responseData) && !isset($responseData['token_type'])) { $responseData['token_type'] = $this->clientConfig->getDefaultTokenType(); } } // if the field "expires_in" has the value null, remove it // issue: https://github.com/fkooman/php-oauth-client/issues/17 if ($this->clientConfig->getAllowNullExpiresIn()) { if (is_array($responseData) && array_key_exists("expires_in", $responseData)) { if (null === $responseData['expires_in']) { unset($responseData['expires_in']); } } } // if the field "scope" is empty string a default can be set // through the client configuration // issue: https://github.com/fkooman/php-oauth-client/issues/20 if (null !== $this->clientConfig->getDefaultServerScope()) { if (is_array($responseData) && isset($responseData['scope']) && '' === $responseData['scope']) { $responseData['scope'] = $this->clientConfig->getDefaultServerScope(); } } return new TokenResponse($responseData); } catch (\Guzzle\Common\Exception\RuntimeException $e) { return false; } }
public function getToken() { $context = new Context($this->clientConfig->getClientId(), new Scope(array("read", "write"))); $accessToken = parent::getAccessToken($context); if (false === $accessToken) { // request for access token using client_credentials when invalid or expired. $tokenRequest = new CodesWholesaleTokenRequest($this->httpClient, $this->clientConfig); $tokenResponse = $tokenRequest->withClientCredentials(); if (false === $tokenResponse) { // unable to fetch with new access token return false; } $accessToken = new AccessToken(array("client_config_id" => $this->clientConfigId, "user_id" => $context->getUserId(), "scope" => $context->getScope(), "access_token" => $tokenResponse->getAccessToken(), "token_type" => $tokenResponse->getTokenType(), "issue_time" => time(), "expires_in" => $tokenResponse->getExpiresIn())); $this->tokenStorage->storeAccessToken($accessToken); } if (false !== $accessToken) { return $accessToken; } return false; }
/** * Tries to authenticate a user * @param Request $request The request * @return \Exception|RedirectResponse Returns an exception when authentication fails, or a redirect response when a redirect is required * @throws \fkooman\OAuth\Client\Exception\ApiException */ public function tryAuthentication(Request $request) { $this->clientConfig->setRedirectUri($request->getUri()); if ($request->query->has('code') || $request->query->has('error')) { try { $this->callback->handleCallback($request->query->all()); } catch (AuthorizeException $ex) { return $ex; } catch (CallbackException $ex) { return $ex; } } if ($request->query->has('code')) { $request->query->remove('code'); $request->query->remove('state'); $request->server->set('QUERY_STRING', http_build_query($request->query->all())); return new RedirectResponse($request->getUri()); } if (!$this->getAccessToken()) { return new RedirectResponse($this->api->getAuthorizeUri($this->context)); } }
private function accessTokenRequest(array $p) { if ($this->clientConfig->getCredentialsInRequestBody()) { // provide credentials in the POST body $p['client_id'] = $this->clientConfig->getClientId(); $p['client_secret'] = $this->clientConfig->getClientSecret(); } else { // use basic authentication $this->httpClient->setBasicAuth($this->clientConfig->getClientId(), $this->clientConfig->getClientSecret()); } try { $this->httpClient->addHeader('Accept', 'application/json'); $this->httpClient->addPostFields($p); $responseData = $this->httpClient->post($this->clientConfig->getTokenEndpoint()); // some servers do not provide token_type, so we allow for setting // a default // issue: https://github.com/fkooman/php-oauth-client/issues/13 if (null !== $this->clientConfig->getDefaultTokenType()) { if (is_array($responseData) && !isset($responseData['token_type'])) { $responseData['token_type'] = $this->clientConfig->getDefaultTokenType(); } } // if the field "expires_in" has the value null, remove it // issue: https://github.com/fkooman/php-oauth-client/issues/17 if ($this->clientConfig->getAllowNullExpiresIn()) { if (is_array($responseData) && array_key_exists('expires_in', $responseData)) { if (null === $responseData['expires_in']) { unset($responseData['expires_in']); } } } // if the field "scope" is empty string a default can be set // through the client configuration // issue: https://github.com/fkooman/php-oauth-client/issues/20 if (null !== $this->clientConfig->getDefaultServerScope()) { if (is_array($responseData) && isset($responseData['scope']) && '' === $responseData['scope']) { $responseData['scope'] = $this->clientConfig->getDefaultServerScope(); } } // the service can return a string value of the expires_in // parameter, allow to convert to number instead // issue: https://github.com/fkooman/php-oauth-client/issues/40 if ($this->clientConfig->getAllowStringExpiresIn()) { if (is_array($responseData) && isset($responseData['expires_in']) && is_string($responseData['expires_in'])) { $responseData['expires_in'] = intval($responseData['expires_in']); } } if ($this->clientConfig->getUseCommaSeparatedScope()) { if (is_array($responseData) && isset($responseData['scope'])) { $responseData['scope'] = str_replace(',', ' ', $responseData['scope']); } } // issue: https://github.com/fkooman/php-oauth-client/issues/41 if ($this->clientConfig->getUseArrayScope()) { if (is_array($responseData) && isset($responseData['scope'])) { if (is_array($responseData['scope'])) { $responseData['scope'] = implode(' ', $responseData['scope']); } } } return new TokenResponse($responseData); } catch (RuntimeException $e) { return false; } }
/** * * @param array $p * @return bool|TokenResponse */ protected function accessTokenRequest(array $p) { $this->c->setConfig(array(\Guzzle\Http\Client::REQUEST_OPTIONS => array('allow_redirects' => false, 'exceptions' => false, 'verify' => false))); if ($this->clientConfig->getCredentialsInRequestBody()) { // provide credentials in the POST body $p['client_id'] = $this->clientConfig->getClientId(); $p['client_secret'] = $this->clientConfig->getClientSecret(); } else { // use basic authentication $curlAuth = new \Guzzle\Plugin\CurlAuth\CurlAuthPlugin($this->clientConfig->getClientId(), $this->clientConfig->getClientSecret()); $this->c->addSubscriber($curlAuth); } try { $request = $this->c->post($this->clientConfig->getTokenEndpoint()); $request->addPostFields($p); $request->addHeader('Accept', 'application/json'); $clientHeaders = $this->clientConfig->getClientHeaders(); if (isset($clientHeaders['User-Agent'])) { $request->addHeader('User-Agent', $clientHeaders['User-Agent']); } $response = $request->send(); $responseData = $response->json(); // some servers do not provide token_type, so we allow for setting // a default // issue: https://github.com/fkooman/php-oauth-client/issues/13 if (null !== $this->clientConfig->getDefaultTokenType()) { if (is_array($responseData) && !isset($responseData['token_type'])) { $responseData['token_type'] = $this->clientConfig->getDefaultTokenType(); } } // if the field "expires_in" has the value null, remove it // issue: https://github.com/fkooman/php-oauth-client/issues/17 if ($this->clientConfig->getAllowNullExpiresIn()) { if (is_array($responseData) && array_key_exists("expires_in", $responseData)) { if (null === $responseData['expires_in']) { unset($responseData['expires_in']); } } } // if the field "scope" is empty string a default can be set // through the client configuration // issue: https://github.com/fkooman/php-oauth-client/issues/20 if (null !== $this->clientConfig->getDefaultServerScope()) { if (is_array($responseData) && isset($responseData['scope']) && '' === $responseData['scope']) { $responseData['scope'] = $this->clientConfig->getDefaultServerScope(); } } if ($response->isError()) { $errorResult = null; if (!$responseData) { // @codeCoverageIgnoreStart $status = $response->getHttpStatus(); $errorResult = new \stdClass(); $errorResult->{$status} = $status; // @codeCoverageIgnoreEnd } else { $errorResult = json_decode($response->getBody()); } $error = new \CodesWholesale\Resource\Error($errorResult); throw new \CodesWholesale\Resource\ResourceError($error); } return new TokenResponse($responseData); } catch (\Guzzle\Common\Exception\RuntimeException $e) { return false; } }