public function access_token(OAuth2_Client $client, $code, array $params = NULL) { $request = OAuth2_Request::factory('token', 'POST', $this->url_access_token(), array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $client->id, 'client_secret' => $client->secret)); if ($client->callback) { $request->param('redirect_uri', $client->callback); } if ($params) { // Load user parameters $request->params($params); } $response = $request->execute(); return OAuth2_Token::factory('access', array('token' => $response->param('access_token'))); }
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'); 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(); $curl = curl_init($url); $headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8;'; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // curl_setopt($curl, CURLOPT_USERAGENT, 'yamolib-php'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 80); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); // curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/../data/ca-certificate.crt'); $response = curl_exec($curl); curl_close($curl); $return = json_decode($response, true); if (!empty($return['error'])) { throw new OAuth2_Exception($return); } switch ($params['grant_type']) { case 'authorization_code': return OAuth2_Token::factory('access', $return); break; case 'refresh_token': return OAuth2_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' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); $params = array_merge($params, $this->params); 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': /* $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))); if (EXTERNAL_API_PROXY) { $opts['http']['proxy'] = EXTERNAL_API_PROXY_URL . ':' . EXTERNAL_API_PROXY_PORT; $opts['http']['request_fulluri'] = true; } $_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 (!empty($return['error'])) { throw new OAuth2_Exception($return); } switch ($params['grant_type']) { case 'authorization_code': return OAuth2_Token::factory('access', $return); break; case 'refresh_token': return OAuth2_Token::factory('refresh', $return); break; } }
public function access($code, $options = array()) { $params = array('appid' => $this->client_id, 'secret' => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; 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); $return = $this->parse_response($response); 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 = $this->parse_response($response); break; default: throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (!empty($return['error'])) { throw new OAuth2_Exception($return); } return OAuth2_Token::factory('access', $return); }
public function access($code, $options = array()) { // 验证状态值是否一致 if (isset($_GET[$this->state_key]) and $_GET[$this->state_key] != get_instance()->session->userdata('state')) { throw new OAuth2_Exception("状态不匹配,小心账号被恶意网站盗用"); } // 组装配置参数获取返回数据 $params = array($this->client_id_key => $this->client_id, $this->client_secret_key => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); $params = array_merge($params, $this->params); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params[$this->redirect_uri_key] = isset($options[$this->redirect_uri_key]) ? $options[$this->redirect_uri_key] : $this->redirect_uri; break; case 'refresh_token': $params['refresh_token'] = $code; break; } // 请求的地址 $url = $this->url_access_token(); $response = null; switch ($this->method) { case 'GET': $url .= '?' . http_build_query($params); $response = file_get_contents($url); $return = $this->parse_response($response); break; case 'POST': if (function_exists('curl_init')) { // curl方式 $oCurl = curl_init(); if (stripos($url, 'https://') !== FALSE) { curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); } $aPOST = array(); foreach ($params as $key => $val) { $aPOST[] = $key . '=' . urlencode($val); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($oCurl, CURLOPT_POST, TRUE); curl_setopt($oCurl, CURLOPT_POSTFIELDS, join('&', $aPOST)); $response = curl_exec($oCurl); curl_close($oCurl); $return = $this->parse_response($response); } elseif (function_exists('stream_context_create')) { // php5.3以上 $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($params))); $_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 = $this->parse_response($response); } else { // 服务器不支持,抛出异常 throw new OAuth2_Exception('服务器必须开启CURL扩展'); } break; default: throw new OutOfBoundsException('提交方式必须选择POST或者GET'); } // 判断返回值,抛出异常 if (!empty($return[$this->error_key]) or !isset($return['access_token'])) { throw new OAuth2_Exception("<br>请求地址:{$url}<br>返回信息:{$response}"); } // $return['uid_key'] = $this->uid_key; $return['nick_key'] = $this->nick_key; $return['access_token_key'] = $this->access_token_key; switch ($params['grant_type']) { case 'authorization_code': return OAuth2_Token::factory('access', $return); break; case 'refresh_token': return OAuth2_Token::factory('refresh', $return); break; } }
public function access($code, $options = array()) { // 验证状态值是否一致 /*if (isset($_GET[$this->state_key]) AND $_GET[$this->state_key] != get_instance()->session->userdata('state')) { throw new OAuth2_Exception("状态不匹配,小心账号被恶意网站盗用"); }*/ // 组装配置参数获取返回数据 $params = array($this->client_id_key => $this->client_id, $this->client_secret_key => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); $params = array_merge($params, $this->params); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params[$this->redirect_uri_key] = isset($options[$this->redirect_uri_key]) ? $options[$this->redirect_uri_key] : $this->redirect_uri; break; case 'refresh_token': $params['refresh_token'] = $code; break; } // 请求的地址 $url = $this->url_access_token(); $response = null; switch ($this->method) { case 'GET': $url .= '?' . http_build_query($params); $response = file_get_contents($url); $return = $this->parse_response($response); break; case 'POST': if (function_exists('curl_init')) { // curl方式 $ci = get_instance(); $ci->load->library('space/curl'); $ci->curl->create($url)->post($params, array('failonerror' => false)); $ci->curl->ssl(false); $response = $ci->curl->execute(); $return = $this->parse_response($response); } elseif (function_exists('stream_context_create')) { // php5.3以上 $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($params))); $_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 = $this->parse_response($response); } else { // 服务器不支持,抛出异常 throw new OAuth2_Exception('服务器必须开启CURL扩展'); } break; default: throw new OutOfBoundsException('提交方式必须选择POST或者GET'); } // 判断返回值,抛出异常 if (!empty($return[$this->error_key]) or !isset($return['access_token'])) { throw new OAuth2_Exception("请求地址:{$url}<br>返回信息:{$response}"); } // $return['uid_key'] = $this->uid_key; $return['access_token_key'] = $this->access_token_key; switch ($params['grant_type']) { case 'authorization_code': return OAuth2_Token::factory('access', $return); break; case 'refresh_token': return OAuth2_Token::factory('refresh', $return); break; } }
public function sign($method, $url, $params = array(), $postBody = false, &$headers = array()) { $params['oauth_token'] = $this->accessToken->getAccessToken(); return $url . '?' . http_build_query($params); }
/** * @param $name Token type * @param array Token options * @return OAuth2_Token */ public function token($name, array $options = NULL) { return OAuth2_Token::factory($name, $options); }
/** * parse the response of an access token request and store it in dataStore * * @param OAuth2_HttpClient $http */ private function _parseAccessTokenResponse(OAuth2_HttpClient $http) { $headers = $http->getHeaders(); $type = 'text'; if (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/json') !== false) { $type = 'json'; } switch ($type) { case 'json': $response = json_decode($http->getResponse(), true); break; case 'text': default: $response = OAuth2_HttpClient::parseStringToArray($http->getResponse(), '&', '='); break; } if (isset($response['error'])) { throw new OAuth2_Exception('got error while requesting access token: ' . $response['error']); } if (!isset($response['access_token'])) { throw new OAuth2_Exception('no access_token found'); } $token = new OAuth2_Token($response['access_token'], isset($response['refresh_token']) ? $response['refresh_token'] : null, isset($response['expires_in']) ? $response['expires_in'] : null); unset($response['access_token']); unset($response['refresh_token']); unset($response['expires_in']); // add additional parameters which may be returned depending on service and scope foreach ($response as $key => $value) { $token->{'set' . $key}($value); } if (isset($_GET['platform'])) { $token->setplatform($_GET['platform']); } $this->_dataStore->set($this->_storageKey, $token); }
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'); 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); if (config_item('proxy_host') && config_item('proxy_port')) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); $proxy = config_item('proxy_host') . ":" . config_item('proxy_port'); curl_setopt($ch, CURLOPT_PROXY, $proxy); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); } else { $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 (!empty($return['error'])) { throw new OAuth2_Exception($return); } return OAuth2_Token::factory('access', $return); }
public function access($code, $options = array()) { //check we csrf first if (isset($_GET[$this->state_key]) and $_GET[$this->state_key] != get_instance()->session->userdata('state')) { throw new OAuth2_Exception(array('code' => '403', 'message' => 'The state does not match. Maybe you are a victim of CSRF.')); } $params = array($this->client_id_key => $this->client_id, $this->client_secret_key => $this->client_secret, 'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code'); $params = array_merge($params, $this->params); switch ($params['grant_type']) { case 'authorization_code': $params['code'] = $code; $params[$this->redirect_uri_key] = isset($options[$this->redirect_uri_key]) ? $options[$this->redirect_uri_key] : $this->redirect_uri; break; case 'refresh_token': $params['refresh_token'] = $code; break; } $response = null; $url = $this->url_access_token(); switch ($this->method) { case 'GET': $url .= '?' . http_build_query($params); $response = file_get_contents($url); $return = $this->parse_response($response); break; case 'POST': $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($params))); $_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 = $this->parse_response($response); break; default: throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); } if (!empty($return[$this->error_key]) or !isset($return['access_token'])) { throw new OAuth2_Exception($return); } $return['uid_key'] = $this->uid_key; $return['access_token_key'] = $this->access_token_key; switch ($params['grant_type']) { case 'authorization_code': return OAuth2_Token::factory('access', $return); break; case 'refresh_token': return OAuth2_Token::factory('refresh', $return); break; } }