public function get_token($config) { // TODO Validation of expires date if (isset($config['expires']) && $config['expires'] < 1) { $token = $this->access($config['refresh_token'], array('grant_type' => 'refresh_token')); } else { $token = \OAuth2\Token::factory('access', $config); } return $token; }
public function access($code, $options = array()) { $type = isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'; $params = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'redirect_uri' => isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri); switch ($type) { case 'authorization_code': $params['code'] = $code; $params['grant_type'] = $type; break; case 'refresh_token': $params['refresh_token'] = $code; $params['response_type'] = 'refresh_token'; $params['UserID'] = $options['uid']; break; } $response = null; $url = $this->url_access_token(); 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); $return = json_decode($response, true); break; case 'POST': $postdata = http_build_query($params); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata)); $_default_opts = stream_context_get_params(stream_context_get_default()); $context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts)); $response = file_get_contents($url, false, $context); $return = json_decode($response, true); break; default: throw new Exception('Method \'' . $this->method . '\' must be either GET or POST'); } if (isset($return['Error'])) { throw new Exception($return['Error'], $return['ErrorCode']); } // Converts keys to the equivalent $return['access_token'] = $return['AccessToken']; $return['expires'] = $return['Expires']; $return['refresh_token'] = $return['RefreshToken']; $return['uid'] = $return['UserID']; // Unsets no longer used indexes unset($return['AccessToken'], $return['Expires'], $return['RefreshToken'], $return['UserID']); switch ($type) { case 'authorization_code': return Token::factory('access', $return); break; case 'refresh_token': return Token::factory('refresh', $return); break; } }
public function access($code, $options = array()) { $params = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => \Arr::get($options, 'grant_type', 'authorization_code')); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params['redirect_uri'] = \Arr::get($options, 'redirect_uri', $this->redirect_uri); break; case 'refresh_token': $params['refresh_token'] = $code; break; } $response = null; $url = $this->url_access_token(); // Get ready to make a request // $request = \Request::forge($url, 'curl'); // // $request->set_params($params); 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': $postdata = http_build_query($params); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata)); $context = stream_context_create($opts); $response = file_get_contents($url, false, $context); $return = get_object_vars(json_decode($response)); /* F**k the request class try { $request->set_header('Accept', 'application/json'); $request->set_method('POST'); $request = $request->execute(); } catch (RequestException $e) { \Debug::dump($request->response()); exit; } catch (HttpNotFoundException $e) { \Debug::dump($request->response()); exit; } $response = $request->response(); logger(\Fuel::L_INFO, 'Access token response: '.print_r($body, true), __METHOD__); // Try to get the actual response, its hopefully an array $body = $response->body(); */ break; default: throw new \OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (isset($return['error'])) { throw new Exception($return); } return Token::forge('access', $return); }
public function access($code, $options = array()) { $params = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code', 'header' => isset($options['header']) ? $options['header'] : ''); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params['redirect_uri'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri; break; case 'refresh_token': $params['refresh_token'] = $code; break; } $response = null; $url = $this->url_access_token(); 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': $postdata = http_build_query($params); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . PHP_EOL . $params['header'], 'content' => $postdata)); $_default_opts = stream_context_get_params(stream_context_get_default()); $context = stream_context_create(array_merge_recursive($_default_opts['options'], $opts)); $response = file_get_contents($url, false, $context); $return = json_decode($response, true); break; default: throw new \OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (isset($return['error'])) { throw new Exception($return); } switch ($params['grant_type']) { case 'authorization_code': return Token::factory('access', $return); break; case 'refresh_token': return Token::factory('refresh', $return); break; } }
public function access($code, $options = array()) { $params = array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => \Arr::get($options, 'grant_type', 'authorization_code')); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params['redirect_uri'] = \Arr::get($options, 'redirect_uri', $this->redirect_uri); break; case 'refresh_token': $params['refresh_token'] = $code; break; } $response = null; $url = $this->url_access_token(); // Get ready to make a request // $request = \Request::forge($url, 'curl'); // // $request->set_params($params); 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': $postdata = http_build_query($params); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata)); $context = stream_context_create($opts); $response = file_get_contents($url, false, $context); $return = get_object_vars(json_decode($response)); break; default: throw new \OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (isset($return['error'])) { throw new Exception($return); } return Token::forge('access', $return); }