diff --git a/src/Oauth2/Client/IDP.php b/src/Oauth2/Client/IDP.php old mode 100755 new mode 100644 index 07dcbbd4..4f8c324f --- a/src/Oauth2/Client/IDP.php +++ b/src/Oauth2/Client/IDP.php @@ -1,143 +1,64 @@ name) - { - // Attempt to guess the name from the class name - $this->name = strtolower(substr(get_class($this), strlen('OAuth2_Provider_'))); - } - - if (empty($options['id'])) - { - throw new Exception('Required option not provided: id'); - } - - $this->clientId = $options['id']; - - isset($options['callback']) and $this->callback = $options['callback']; - isset($options['secret']) and $this->client_secret = $options['secret']; - isset($options['scope']) and $this->scope = $options['scope']; - - $this->redirectUri = site_url(get_instance()->uri->uri_string()); + //$this->redirectUri = $_SERVER[] } - /** - * Return the value of any protected class variable. - * - * // Get the provider signature - * $signature = $provider->signature; - * - * @param string variable name - * @return mixed - */ public function __get($key) { return $this->$key; } - /** - * Returns the authorization URL for the provider. - * - * $url = $provider->urlAuthorize(); - * - * @return string - */ abstract public function urlAuthorize(); - /** - * Returns the access token endpoint for the provider. - * - * $url = $provider->urlAccessToken(); - * - * @return string - */ abstract public function urlAccessToken(); - /* - * Get an authorization code from Facebook. Redirects to Facebook, which this redirects back to the app using the redirect address you've set. - */ public function authorize($options = array()) { $state = md5(uniqid(rand(), TRUE)); - get_instance()->session->set_userdata('state', $state); + setcookie($this->name.'_authorize_state', $state); $params = array( - 'client_id' => $this->client_id, - 'redirect_uri' => isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri, + 'client_id' => $this->clientId, + 'redirect_uri' => $this->redirectUri, 'state' => $state, 'scope' => is_array($this->scope) ? implode($this->scopeSeperator, $this->scope) : $this->scope, - 'response_type' => 'code', + 'response_type' => isset($options['response_type'] ? $options['response_type'] : 'code', 'approval_prompt' => 'force' // - google force-recheck ); - redirect($this->urlAuthorize().'?'.http_build_query($params)); + header('Location: ' . $this->urlAuthorize().'?'.http_build_query($params)); + exit; } - /* - * Get access to the API - * - * @param string The access code - * @return object Success or failure along with the response details - */ - public function access($code, $options = array()) + public function getAccessToken() { $params = array( 'client_id' => $this->clientId, @@ -158,72 +79,49 @@ abstract class IDP } - $response = null; - $url = $this->urlAccessToken(); - switch ($this->method) { - case 'GET': - - // Need to switch to Request library, but need to test it on one that works - $url .= '?'.http_build_query($params); - $response = file_get_contents($url); - - parse_str($response, $return); - - break; - - case 'POST': - - /* $ci = get_instance(); - - $ci->load->spark('curl/1.2.1'); - - $ci->curl - ->create($url) - ->post($params, array('failonerror' => false)); - - $response = $ci->curl->execute(); - */ - - $opts = array( - 'http' => array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => http_build_query($params), - ) - ); - - $defaultOpts = stream_context_get_params(stream_context_get_default()); - $context = stream_context_create(array_merge_recursive($defaultOpts['options'], $opts)); - $response = file_get_contents($url, false, $context); - - $return = json_decode($response, true); - - break; + case 'get': + $client = new Client($this->urlAccessToken() .= '?'.http_build_query($params)); + $response = $client->get(); + break; default: - throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); - } - - if ( ! empty($return['error'])) { - - throw new OAuth2_Exception($return); + $client = new Client($this->urlAccessToken()); + $response = $client->{$this->method}(null, null, $params); + break; } - + + switch ($this->responseType) { + + case 'json': + $result = json_decode($response, true); + break; + + case 'string': + parse_str($response, $result); + break; + + } + + if (isset($result['error']) && ! empty($result['error'])) { + + throw new Oauth2\Client\IDPException($result); + + } + switch ($params['grant_type']) { case 'authorization_code': - return OAuth2_Token::factory('access', $return); + return Oauth2\Client\Token::factory('access', $result); break; case 'refresh_token': - return OAuth2_Token::factory('refresh', $return); + return Oauth2\Client\Token::factory('refresh', $result); break; } - } -} +} \ No newline at end of file