123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /**
- * OAuth.php UTF-8
- *
- *
- * @date : 2018/4/25 15:17
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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();
- }
|