* @version : HUOSDK 8.0 */ namespace huolib\oauth; use think\Exception; use think\Session; abstract class OAuth { /** * oauth版本 * * @var string */ protected $version = '2.0'; /** * 申请应用时分配的app_key * * @var string */ protected $app_key = ''; /** * 申请应用时分配的 app_secret * * @var string */ protected $app_secret = ''; /** * 授权类型 response_type 目前只能为code * * @var string */ protected $response_type = 'code'; /** * grant_type 目前只能为 authorization_code * * @var string */ protected $grant_type = 'authorization_code'; /** * 回调页面URL 可以通过配置文件配置 * * @var string */ protected $callback = ''; /** * 获取request_code的额外参数 URL查询字符串格式 * * @var string */ protected $authorize = ''; /** * 获取request_code请求的URL * * @var string */ protected $request_code_url = ''; /** * 获取access_token请求的URL * * @var string */ protected $access_token_url = ''; /** * 更新Access token的api接口 * * @var String */ protected $refresh_token_url = ''; /** * API根路径 * * @var string */ protected $api_base = ''; /** * 授权后获取到的token信息 * * @var array */ protected $token = ['openid' => '', 'access_token' => '', 'expires_in' => 0]; /** * 调用接口类型 * * @var string */ private $type = ''; protected $state = ''; /** * 构造方法,配置应用信息 * * @param array $config * @param array $token * * @throws Exception */ public function __construct($config = [], $token = null) { //设置SDK类型 $class = get_class($this); $this->type = strtoupper(substr($class, 0, strlen($class) - 3)); //获取应用配置 if (empty($config['APP_KEY']) || empty($config['APP_SECRET'])) { throw new Exception('请配置您申请的APP_KEY和APP_SECRET'); } else { $this->app_key = $config['APP_KEY']; if (!empty($token['oauth_app_key'])) { $this->app_key = $token['oauth_app_key']; } $this->app_secret = $config['APP_SECRET']; $this->callback = $config['CALLBACK']; $this->token = $token; //设置获取到的token } } /** * 取得Oauth实例 * * @static * * @param $type * @param array $config * @param null $token * * @return mixed 返回Oauth * @throws Exception */ public static function ins($type, $config = [], $token = null) { $_name = ucfirst(strtolower($type)); $_class = '\\huolib\\oauth\\driver\\'.$_name; if (class_exists($_class)) { return new $_class($config, $token); } else { throw new Exception(lang('_CLASS_NOT_EXIST_').':'.$_name); } } /** * 用于保持请求和回调的状态,授权请求后原样带回给第三方。 * 该参数可用于防止csrf攻击(跨站请求伪造攻击), * 建议第三方带上该参数,可设置为简单的随机数加session进行校验 * * @return string */ public function getState() { if (empty($this->state)) { $this->state = time(); } return $this->state; } public function setState() { $this->state = md5(uniqid(rand(), true)); Session::set('oauth.state', $this->state); } public function compareState($state) { //$_old_state = Session::get('state', 'oauth'); $_old_state = Session::get('oauth.state'); if ($_old_state != $state) { return false; } return true; } public function getToken() { return $this->token['expires_in']; } /** * @return string */ public function getCallback() { return $this->callback; } /** * @param string $callback */ public function setCallback($callback) { $this->callback = $callback; } /** * 获取指定API请求的URL * * @param string $api API名称 * @param string $fix api后缀 * * @return string 请求的完整URL */ protected function url($api, $fix = '') { return $this->api_base.$api.$fix; } /** * 请求code * * @param string $display * */ abstract public function getRequestCodeUrl($display = 'pc'); /** * 获取access_token * * @param string $code 上一步请求到的code * @param null $extend * */ abstract public function getAccessToken($code, $extend = null); /** * 抽象方法,在SNSSDK中实现 * 组装接口调用参数 并调用接口 * * @param string $api * @param string $param * @param string $method * @param bool $multi * */ abstract protected function call($api, $param = '', $method = 'GET', $multi = false); /** * 抽象方法,在SNSSDK中实现 * 解析access_token方法请求后的返回值 * * @param $result * @param $extend * */ abstract protected function parseToken($result, $extend); /** * 抽象方法,在SNSSDK中实现 * 获取当前授权用户的SNS标识 */ abstract public function getOpenid(); }