Oauth

OAuth在”客户端”与”服务提供商”之间,设置了一个授权层(authorization layer)。”客户端”不能直接登录”服务提供商”,只能登录授权层,以此将用户与客户端区分开来。”客户端”登录授权层所用的令牌(token),与用户的密码不同。用户可以在登录的时候,指定授权层令牌的权限范围和有效期。

需要用户自行补充oauth的认证流程,方便自己更加快速的接入。

安装

请在EasySwoole根目录下执行以下命令

composer require easyswoole/o-auth

详情

根据Oauth协议,分别有如下调用方法。

  • getAuthUrl() 获取授权地址
  • getAccessToken($storeState = null, $state = null, $code = null) 获取AccessToken(只返回access_token)
  • getAccessTokenResult() 执行getAccessToken方法后,此方法获取原结果
  • getUserInfo(string $accessToken) 获取用户信息
  • validateAccessToken(string $accessToken) 验证token是否有效
  • refreshToken(string $refreshToken = null) 刷新token 返回bool
  • getRefreshTokenResult() 执行refreshToken方法后,此方法获取原结果

示例代码

微信

  1. class WeiXin extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\WeiXin\Config();
  6. $config->setAppId('appid');
  7. $config->setState('easyswoole');
  8. $config->setRedirectUri('redirect_uri');
  9. $oauth = new \EasySwoole\OAuth\WeiXin\OAuth($config);
  10. $url = $oauth->getAuthUrl();
  11. return $this->response()->redirect($url);
  12. }
  13. public function callback()
  14. {
  15. $params = $this->request()->getQueryParams();
  16. $config = new \EasySwoole\OAuth\WeiXin\Config();
  17. $config->setAppId('appid');
  18. $config->setSecret('secret');
  19. $config->setOpenIdMode(\EasySwoole\OAuth\WeiXin\Config::OPEN_ID); // 可设置UNION_ID 默认为OPEN_ID
  20. $oauth = new \EasySwoole\OAuth\WeiXin\OAuth($config);
  21. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['code']);
  22. $refreshToken = $oauth->getAccessTokenResult()['refresh_token'];
  23. $userInfo = $oauth->getUserInfo($accessToken);
  24. var_dump($userInfo);
  25. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  26. if (!$oauth->refreshToken($refreshToken)) echo 'access_token 续期失败!' . PHP_EOL;
  27. }
  28. }

QQ

  1. class QQ extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\QQ\Config();
  6. $config->setAppId('appid');
  7. $config->setState('easyswoole');
  8. $config->setRedirectUri('redirect_uri');
  9. $oauth = new \EasySwoole\OAuth\QQ\OAuth($config);
  10. $url = $oauth->getAuthUrl();
  11. return $this->response()->redirect($url);
  12. }
  13. public function callback()
  14. {
  15. $params = $this->request()->getQueryParams();
  16. $config = new \EasySwoole\OAuth\QQ\Config();
  17. $config->setAppId('appid');
  18. $config->setAppKey('appkey');
  19. $config->setRedirectUri('redirect_uri');
  20. $config->setOpenIdMode(\EasySwoole\OAuth\QQ\Config::OPEN_ID); // 可设置UNION_ID 默认为OPEN_ID
  21. $oauth = new \EasySwoole\OAuth\QQ\OAuth($config);
  22. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['code']);
  23. $refreshToken = $oauth->getAccessTokenResult()['refresh_token'];
  24. $userInfo = $oauth->getUserInfo($accessToken);
  25. var_dump($userInfo);
  26. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  27. if (!$oauth->refreshToken($refreshToken)) echo 'access_token 续期失败!' . PHP_EOL;
  28. }
  29. }

微博

  1. class Weibo extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\Weibo\Config();
  6. $config->setClientId('clientid');
  7. $config->setState('easyswoole');
  8. $config->setRedirectUri('redirect_uri');
  9. $oauth = new \EasySwoole\OAuth\Weibo\OAuth($config);
  10. $url = $oauth->getAuthUrl();
  11. return $this->response()->redirect($url);
  12. }
  13. public function callback()
  14. {
  15. $params = $this->request()->getQueryParams();
  16. $config = new \EasySwoole\OAuth\Weibo\Config();
  17. $config->setClientId('clientid');
  18. $config->setClientSecret('secret');
  19. $config->setRedirectUri('redirect_uri');
  20. $oauth = new \EasySwoole\OAuth\Weibo\OAuth($config);
  21. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['code']);
  22. $userInfo = $oauth->getUserInfo($accessToken);
  23. var_dump($userInfo);
  24. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  25. }
  26. }

支付宝

  1. class AliPay extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\AliPay\Config();
  6. $config->setState('easyswoole');
  7. $config->setAppId('appid');
  8. $config->setRedirectUri('redirect_uri');
  9. // 使用沙箱环境测试开发的时候 把OAuth的源码文件里面的 API_DOMAIN 和 AUTH_DOMAIN 进行修改
  10. $oauth = new \EasySwoole\OAuth\AliPay\OAuth($config);
  11. $url = $oauth->getAuthUrl();
  12. return $this->response()->redirect($url);
  13. }
  14. public function callback()
  15. {
  16. $params = $this->request()->getQueryParams();
  17. $config = new \EasySwoole\OAuth\AliPay\Config();
  18. $config->setAppId('appid');
  19. // $config->setAppPrivateKey('私钥');
  20. $config->setAppPrivateKeyFile('私钥文件'); // 私钥文件(非远程) 此方法与上个方法二选一
  21. $oauth = new \EasySwoole\OAuth\AliPay\OAuth($config);
  22. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['auth_code']);
  23. $refreshToken = $oauth->getAccessTokenResult()['alipay_system_oauth_token_response']['refresh_token'];
  24. $userInfo = $oauth->getUserInfo($accessToken);
  25. var_dump($userInfo);
  26. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  27. var_dump($oauth->getAccessTokenResult());
  28. if (!$oauth->refreshToken($refreshToken)) echo 'access_token 续期失败!' . PHP_EOL;
  29. var_dump($oauth->getRefreshTokenResult());
  30. }
  31. }

Github

  1. class Github extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\Github\Config();
  6. $config->setClientId('clientid');
  7. $config->setRedirectUri('redirect_uri');
  8. $config->setState('easyswoole');
  9. $oauth = new \EasySwoole\OAuth\Github\OAuth($config);
  10. $this->response()->redirect($oauth->getAuthUrl());
  11. }
  12. public function callback()
  13. {
  14. $params = $this->request()->getQueryParams();
  15. $config = new \EasySwoole\OAuth\Github\Config();
  16. $config->setClientId('clientid');
  17. $config->setClientSecret('secret');
  18. $config->setRedirectUri('redirect_uri');
  19. $oauth = new \EasySwoole\OAuth\Github\OAuth($config);
  20. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['code']);
  21. $userInfo = $oauth->getUserInfo($accessToken);
  22. var_dump($userInfo);
  23. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  24. }
  25. }

Gitee

  1. class Gitee extends \EasySwoole\Http\AbstractInterface\Controller
  2. {
  3. public function index()
  4. {
  5. $config = new \EasySwoole\OAuth\Gitee\Config();
  6. $config->setState('easyswoole');
  7. $config->setClientId('clientid');
  8. $config->setRedirectUri('redirect_uri');
  9. $oauth = new \EasySwoole\OAuth\Gitee\OAuth($config);
  10. $this->response()->redirect($oauth->getAuthUrl());
  11. }
  12. public function callback()
  13. {
  14. $params = $this->request()->getQueryParams();
  15. $config = new \EasySwoole\OAuth\Gitee\Config();
  16. $config->setClientId('client_id');
  17. $config->setClientSecret('secret');
  18. $config->setRedirectUri('redirect_uri');
  19. $oauth = new \EasySwoole\OAuth\Gitee\OAuth($config);
  20. $accessToken = $oauth->getAccessToken('easyswoole', $params['state'], $params['code']);
  21. $userInfo = $oauth->getUserInfo($accessToken);
  22. var_dump($userInfo);
  23. if (!$oauth->validateAccessToken($accessToken)) echo 'access_token 验证失败!' . PHP_EOL;
  24. var_dump($oauth->getAccessTokenResult());
  25. }
  26. }