1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 20:06:09 +08:00
koogua 0336a54911 1.源文件增加版权信息
2.群组状态和课程协同
2021-06-13 15:49:47 +08:00

108 lines
2.1 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services;
use GuzzleHttp\Client as HttpClient;
use Phalcon\Crypt;
use Phalcon\Di;
abstract class OAuth extends Service
{
/**
* @var string
*/
protected $clientId;
/**
* @var string
*/
protected $clientSecret;
/**
* @var string
*/
protected $redirectUri;
/**
* @var string
*/
protected $accessToken;
/**
* @var string
*/
protected $openId;
public function __construct($clientId, $clientSecret, $redirectUri)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->redirectUri = $redirectUri;
}
public function httpGet($uri, $params = [], $headers = [])
{
$client = new HttpClient();
$options = ['query' => $params, 'headers' => $headers];
$response = $client->get($uri, $options);
return $response->getBody();
}
public function httpPost($uri, $params = [], $headers = [])
{
$client = new HttpClient();
$options = ['query' => $params, 'headers' => $headers];
$response = $client->post($uri, $options);
return $response->getBody();
}
public function getState()
{
/**
* @var $crypt Crypt
*/
$crypt = Di::getDefault()->get('crypt');
$text = rand(1000, 9999);
return $crypt->encryptBase64($text, null, true);
}
public function checkState($state)
{
/**
* @var $crypt Crypt
*/
$crypt = Di::getDefault()->get('crypt');
$value = $crypt->decryptBase64($state, null, true);
if ($value < 1000 || $value > 9999) {
throw new \Exception('Invalid OAuth State Value');
}
return true;
}
abstract public function getAuthorizeUrl();
abstract public function getAccessToken($code);
abstract public function getOpenId($accessToken);
abstract public function getUserInfo($accessToken, $openId);
}