From 1987baefbf6fca01a8533f96cdc986e1fe0e94c9 Mon Sep 17 00:00:00 2001 From: pfinal Date: Sat, 5 Jun 2021 17:40:33 +0800 Subject: [PATCH 01/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8A=96=E9=9F=B3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Config/AuthDefaultSource.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index fbbc535..ecda2d6 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -89,6 +89,22 @@ public function userInfo(): string return "https://api.weixin.qq.com/sns/userinfo"; } }; + $this->douyin = new class extends AuthSource { + public function authorize(): string + { + return "https://open.douyin.com/platform/oauth/connect/"; + } + + public function accessToken(): string + { + return "https://open.douyin.com/oauth/access_token/"; + } + + public function userInfo(): string + { + return "https://open.douyin.com/oauth/userinfo/"; + } + }; } /** From 9a385af18a8a93568ddc924687ffb2f1b803651a Mon Sep 17 00:00:00 2001 From: heropoo Date: Sat, 5 Jun 2021 18:08:17 +0800 Subject: [PATCH 02/19] composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cd28bfa..3df4382 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "pfinal/justauth-php", - "description": "description", + "description": "library of third-party authorized login", "minimum-stability": "stable", "license": "MIT", "authors": [ From 656c9259aded338581e5e969d8cc21973f4e6c5e Mon Sep 17 00:00:00 2001 From: heropoo Date: Sat, 5 Jun 2021 18:40:08 +0800 Subject: [PATCH 03/19] test --- config/oauth.php | 8 +++---- src/Request/AuthBase.php | 14 +++++++----- src/Request/Source/AuthCommonRequest.php | 8 +++---- test/GiteeTest.php | 28 ++++++++++++++++++++++++ test/GithubTest.php | 10 +++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 test/GiteeTest.php diff --git a/config/oauth.php b/config/oauth.php index cf50021..0347b19 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -2,13 +2,13 @@ return [ 'github' => [ 'client_id'=>env('GITHUB_CLIENT_ID','f4687fdd6801354516b3'), - 'redirect_url'=>env('GITHUB_CALLBACK','dev.local.justauth.cn/test/Github/index.php'), + 'redirect_url'=>env('GITHUB_CALLBACK','dev.local.justauth.cn/test/github/index.php'), 'client_secret'=>env('GITHUB_SECRET','03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'gitee' => [ - 'client_id'=>env('GITEE_CLIENT_ID',''), - 'redirect_url'=>env('GITEE_CALLBACK',''), - 'client_secret'=>env('GITEE_SECRET',''), + 'client_id'=>env('GITEE_CLIENT_ID','f4687fdd6801354516b3'), + 'redirect_url'=>env('GITEE_CALLBACK','dev.local.justauth.cn/test/gitee/index.php'), + 'client_secret'=>env('GITEE_SECRET','03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'weibo' => [ 'client_id'=>env('WEIBO_CLIENT_ID',''), diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 243fd58..fa43024 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,33 +14,35 @@ class AuthBase { - private $driver = ['gitee', 'github','weibo']; + protected $driver = ['gitee', 'github', 'weibo']; protected $source_config; protected $config = []; + public function __construct() { $this->source_config = new AuthDefaultSource(); } - public function OAuth2($config,$driver): AuthApi + public function OAuth2($config, $driver): AuthApi { try { # 加载配置文件 $source_config = $this->source_config->getConfig($driver); - $this->config = $this->get_config($config,$driver); + $this->config = $this->get_config($config, $driver); $this->verified($driver); - return new AuthApi($driver,$this->config,$source_config); + return new AuthApi($driver, $this->config, $source_config); } catch (AuthException $e) { throw new \Exception($e->getMessage(), $e->getCode()); } } - protected function get_config($config_path,$driver) { + protected function get_config($config_path, $driver) + { if (!file_exists($config_path)) { throw new AuthException(AuthResponseStatus::CONFIG_ERROR()); } - $config = require_once $config_path; + $config = require $config_path; if (!isset($config[$driver])) { throw new AuthException(AuthResponseStatus::CONFIG_ERROR()); } diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index e0ead80..b2b3c08 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -8,6 +8,7 @@ use GuzzleHttp\Client; +use JustAuth\Config\AuthSource; abstract class AuthCommonRequest { @@ -55,16 +56,15 @@ abstract class AuthCommonRequest public $loginAgentUrl; /** - * 接口地址 - * @var array + * @var AuthSource */ - public $source_url = []; + public $source_url; public $config = []; public $http = null; - public function __construct($config, $source) + public function __construct(array $config, AuthSource $source = null) { $this->http_driver(); $this->source_url = $source; diff --git a/test/GiteeTest.php b/test/GiteeTest.php new file mode 100644 index 0000000..e3184a5 --- /dev/null +++ b/test/GiteeTest.php @@ -0,0 +1,28 @@ +assertInstanceOf('JustAuth\Request\AuthApi', $authRequest); + return $authRequest; + } + +// /** +// * @depends testInstance +// * +// * @param \JustAuth\Request\AuthApi $github +// * +// */ +// public function testRouterAdd(\JustAuth\Request\AuthApi $github) +// { +// $github->authorization(); +// } + +} \ No newline at end of file diff --git a/test/GithubTest.php b/test/GithubTest.php index f749d12..14697a3 100644 --- a/test/GithubTest.php +++ b/test/GithubTest.php @@ -14,5 +14,15 @@ public function testInstance() return $github; } +// /** +// * @depends testInstance +// * +// * @param \JustAuth\Request\AuthApi $github +// * +// */ +// public function testRouterAdd(\JustAuth\Request\AuthApi $github) +// { +// $github->authorization(); +// } } \ No newline at end of file From 57bfca77b6370c324b51a5ca933125013bf86f29 Mon Sep 17 00:00:00 2001 From: heropoo Date: Sat, 5 Jun 2021 18:40:08 +0800 Subject: [PATCH 04/19] test gitee --- config/oauth.php | 8 +++---- src/Request/AuthBase.php | 14 +++++++----- src/Request/Source/AuthCommonRequest.php | 8 +++---- test/GiteeTest.php | 28 ++++++++++++++++++++++++ test/GithubTest.php | 10 +++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 test/GiteeTest.php diff --git a/config/oauth.php b/config/oauth.php index cf50021..0347b19 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -2,13 +2,13 @@ return [ 'github' => [ 'client_id'=>env('GITHUB_CLIENT_ID','f4687fdd6801354516b3'), - 'redirect_url'=>env('GITHUB_CALLBACK','dev.local.justauth.cn/test/Github/index.php'), + 'redirect_url'=>env('GITHUB_CALLBACK','dev.local.justauth.cn/test/github/index.php'), 'client_secret'=>env('GITHUB_SECRET','03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'gitee' => [ - 'client_id'=>env('GITEE_CLIENT_ID',''), - 'redirect_url'=>env('GITEE_CALLBACK',''), - 'client_secret'=>env('GITEE_SECRET',''), + 'client_id'=>env('GITEE_CLIENT_ID','f4687fdd6801354516b3'), + 'redirect_url'=>env('GITEE_CALLBACK','dev.local.justauth.cn/test/gitee/index.php'), + 'client_secret'=>env('GITEE_SECRET','03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'weibo' => [ 'client_id'=>env('WEIBO_CLIENT_ID',''), diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 243fd58..fa43024 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,33 +14,35 @@ class AuthBase { - private $driver = ['gitee', 'github','weibo']; + protected $driver = ['gitee', 'github', 'weibo']; protected $source_config; protected $config = []; + public function __construct() { $this->source_config = new AuthDefaultSource(); } - public function OAuth2($config,$driver): AuthApi + public function OAuth2($config, $driver): AuthApi { try { # 加载配置文件 $source_config = $this->source_config->getConfig($driver); - $this->config = $this->get_config($config,$driver); + $this->config = $this->get_config($config, $driver); $this->verified($driver); - return new AuthApi($driver,$this->config,$source_config); + return new AuthApi($driver, $this->config, $source_config); } catch (AuthException $e) { throw new \Exception($e->getMessage(), $e->getCode()); } } - protected function get_config($config_path,$driver) { + protected function get_config($config_path, $driver) + { if (!file_exists($config_path)) { throw new AuthException(AuthResponseStatus::CONFIG_ERROR()); } - $config = require_once $config_path; + $config = require $config_path; if (!isset($config[$driver])) { throw new AuthException(AuthResponseStatus::CONFIG_ERROR()); } diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index e0ead80..b2b3c08 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -8,6 +8,7 @@ use GuzzleHttp\Client; +use JustAuth\Config\AuthSource; abstract class AuthCommonRequest { @@ -55,16 +56,15 @@ abstract class AuthCommonRequest public $loginAgentUrl; /** - * 接口地址 - * @var array + * @var AuthSource */ - public $source_url = []; + public $source_url; public $config = []; public $http = null; - public function __construct($config, $source) + public function __construct(array $config, AuthSource $source = null) { $this->http_driver(); $this->source_url = $source; diff --git a/test/GiteeTest.php b/test/GiteeTest.php new file mode 100644 index 0000000..e3184a5 --- /dev/null +++ b/test/GiteeTest.php @@ -0,0 +1,28 @@ +assertInstanceOf('JustAuth\Request\AuthApi', $authRequest); + return $authRequest; + } + +// /** +// * @depends testInstance +// * +// * @param \JustAuth\Request\AuthApi $github +// * +// */ +// public function testRouterAdd(\JustAuth\Request\AuthApi $github) +// { +// $github->authorization(); +// } + +} \ No newline at end of file diff --git a/test/GithubTest.php b/test/GithubTest.php index f749d12..14697a3 100644 --- a/test/GithubTest.php +++ b/test/GithubTest.php @@ -14,5 +14,15 @@ public function testInstance() return $github; } +// /** +// * @depends testInstance +// * +// * @param \JustAuth\Request\AuthApi $github +// * +// */ +// public function testRouterAdd(\JustAuth\Request\AuthApi $github) +// { +// $github->authorization(); +// } } \ No newline at end of file From eb2aa442449ff0ec358fb266a6efef820fbc25de Mon Sep 17 00:00:00 2001 From: heropoo Date: Sat, 5 Jun 2021 18:46:00 +0800 Subject: [PATCH 05/19] fix --- src/Request/Source/AuthCommonRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index b2b3c08..5b40f28 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -64,7 +64,7 @@ abstract class AuthCommonRequest public $http = null; - public function __construct(array $config, AuthSource $source = null) + public function __construct(array $config, AuthSource $source) { $this->http_driver(); $this->source_url = $source; From 18f9d27734f8ee2a9f905e2d5dfaa2c550bb206e Mon Sep 17 00:00:00 2001 From: pfinal Date: Sun, 6 Jun 2021 14:09:33 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0qq=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/oauth.php | 34 ++++++-- src/Config/AuthDefaultSource.php | 30 ++++++- src/Enums/AuthResponseStatus.php | 8 +- src/Request/AuthBase.php | 2 +- src/Request/Source/AuthDouyinRequest.php | 75 +++++++++++++++++ src/Request/Source/AuthGiteeRequest.php | 20 ++--- src/Request/Source/AuthGithubRequest.php | 18 ++-- src/Request/Source/AuthQqRequest.php | 100 +++++++++++++++++++++++ 8 files changed, 254 insertions(+), 33 deletions(-) create mode 100644 src/Request/Source/AuthDouyinRequest.php create mode 100644 src/Request/Source/AuthQqRequest.php diff --git a/config/oauth.php b/config/oauth.php index cf50021..db63e13 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -1,18 +1,34 @@ [ - 'client_id'=>env('GITHUB_CLIENT_ID','f4687fdd6801354516b3'), - 'redirect_url'=>env('GITHUB_CALLBACK','dev.local.justauth.cn/test/Github/index.php'), - 'client_secret'=>env('GITHUB_SECRET','03201d93b38f7eac722899420aaa35e6f375a8c4'), + 'client_id' => env('GITHUB_CLIENT_ID', 'f4687fdd6801354516b3'), + 'redirect_url' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), + 'client_secret' => env('GITHUB_SECRET', '03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'gitee' => [ - 'client_id'=>env('GITEE_CLIENT_ID',''), - 'redirect_url'=>env('GITEE_CALLBACK',''), - 'client_secret'=>env('GITEE_SECRET',''), + 'client_id' => env('GITEE_CLIENT_ID', ''), + 'redirect_url' => env('GITEE_CALLBACK', ''), + 'client_secret' => env('GITEE_SECRET', ''), ], 'weibo' => [ - 'client_id'=>env('WEIBO_CLIENT_ID',''), - 'redirect_url'=>env('WEIBO_CALLBACK',''), - 'client_secret'=>env('WEIBO_SECRET',''), + 'client_id' => env('WEIBO_CLIENT_ID', ''), + 'redirect_url' => env('WEIBO_CALLBACK', ''), + 'client_secret' => env('WEIBO_SECRET', ''), + ], + 'douyin' => [ + 'client_id' => env('WEIBO_CLIENT_ID', ''), + 'redirect_url' => env('WEIBO_CALLBACK', ''), + 'client_secret' => env('WEIBO_SECRET', ''), + 'state' => env('WEIBO_STATE', ''), + ], + 'weixin' => [ + 'client_id' => env('WEIXIN_CLIENT_ID', ''), + 'redirect_url' => env('WEIXIN_CALLBACK', ''), + 'client_secret' => env('WEIXIN_SECRET', ''), + ], + 'qq' => [ + 'client_id' => env('QQ_CLIENT_ID', ''), + 'redirect_url' => env('QQs_CALLBACK', ''), + 'client_secret' => env('WEIBO_SECRET', ''), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index ecda2d6..55eb5be 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -20,7 +20,7 @@ class AuthDefaultSource public function __construct() { - $this->gitee = new class extends AuthSource { + $this->gitee = new class extends AuthSource { public function authorize(): string { return "https://gitee.com/oauth/authorize"; @@ -52,7 +52,7 @@ public function userInfo(): string return "https://api.github.com/user"; } }; - $this->weibo = new class extends AuthSource { + $this->weibo = new class extends AuthSource { public function authorize(): string { return "https://api.weibo.com/oauth2/authorize"; @@ -105,6 +105,32 @@ public function userInfo(): string return "https://open.douyin.com/oauth/userinfo/"; } }; + $this->qq = new class extends AuthSource { + public function authorize(): string + { + return "https://graph.qq.com/oauth2.0/authorize"; + } + + public function accessToken(): string + { + return "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code"; + } + + public function userInfo(): string + { + return "https://graph.qq.com/user/get_user_info"; + } + + public function getUid() + { + return "https://graph.qq.com/oauth2.0/me?access_token="; + } + + public function getUnionid() + { + return "https://graph.qq.com/oauth2.0/me?access_token="; + } + }; } /** diff --git a/src/Enums/AuthResponseStatus.php b/src/Enums/AuthResponseStatus.php index 0e8836e..256af11 100644 --- a/src/Enums/AuthResponseStatus.php +++ b/src/Enums/AuthResponseStatus.php @@ -34,9 +34,13 @@ class AuthResponseStatus extends Enum /** @msg('Illegal state') */ const ILLEGAL_STATUS = 5009; /** @msg('Config Error') */ - const CONFIG_ERROR=5010; + const CONFIG_ERROR = 5010; /** @msg('Config Source Error') */ - const CONFIG_SOURCE_ERROR=5011; + const CONFIG_SOURCE_ERROR = 5011; + /** @msg('Get user openid eroor') * */ + const GET_OPENID_ERROR = 5012; + /** @msg('Get user info eroor') * */ + const GET_USERINFO_ERROR = 5013; } \ No newline at end of file diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 243fd58..908684b 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github','weibo']; + private $driver = ['gitee', 'github','weibo','weixin','douyin','qq']; protected $source_config; protected $config = []; public function __construct() diff --git a/src/Request/Source/AuthDouyinRequest.php b/src/Request/Source/AuthDouyinRequest.php new file mode 100644 index 0000000..c2de08d --- /dev/null +++ b/src/Request/Source/AuthDouyinRequest.php @@ -0,0 +1,75 @@ +source_url->authorize(); + $query = array_filter([ + 'response_type' => 'code', + 'client_key' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'scope' => '', + 'state' => $this->config['state'], + ]); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_key' => $this->config['client_id'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + 'redirect_uri' => $this->config['redirect_uri'], + ]); + $res = json_decode($this->http->request('GET', $token_url, [ + 'query' => $query, + ])->getBody()->getContents())->data(); + $this->openid = $res->open_id; + $this->unionid = $res->unionid; + return $res->access_token; + } + + /** + * 获取用户信息 + * @param $access_token + * @return mixed + */ + public function getUserInfo($access_token) + { + $query = array_filter([ + 'open_id' => $this->openid, + + 'access_token' => $access_token, + ]); + $userinfo_url = $this->source_url->userInfo(); + $userinfo = json_decode($this->http->request('GET', $userinfo_url, [ + 'query' => $query, + ])->getBody()->getContents())->data; + + $userinfo->openid = $userinfo->open_id; + $userinfo->unionid = $userinfo->union_id; + + return $userinfo; + } +} \ No newline at end of file diff --git a/src/Request/Source/AuthGiteeRequest.php b/src/Request/Source/AuthGiteeRequest.php index 9063790..bdcc97a 100644 --- a/src/Request/Source/AuthGiteeRequest.php +++ b/src/Request/Source/AuthGiteeRequest.php @@ -16,13 +16,13 @@ class AuthGiteeRequest extends AuthCommonRequest public function authorization() { $auth_url = $this->source_url->authorize(); - $query = array_filter([ - 'client_id' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_uri'], + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], 'response_type' => 'code', ]); - $url = $auth_url.'?'.http_build_query($query); - header('Location:'.$url); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); exit(); } @@ -30,11 +30,11 @@ public function authorization() public function getAccessToken() { $token_url = $this->source_url->accessToken(); - $query = array_filter([ - 'client_id' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_uri'], - 'code' => Request::get('code'), - 'grant_type' => 'authorization_code', + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', 'client_secret' => $this->config['client_secret'], ]); return $this->http->request('POST', $token_url, [ diff --git a/src/Request/Source/AuthGithubRequest.php b/src/Request/Source/AuthGithubRequest.php index 860d4f4..e1d5bbd 100644 --- a/src/Request/Source/AuthGithubRequest.php +++ b/src/Request/Source/AuthGithubRequest.php @@ -16,12 +16,12 @@ class AuthGithubRequest extends AuthCommonRequest public function authorization() { $auth_url = $this->source_url->authorize(); - $query = array_filter([ - 'client_id' => $this->config['client_id'], + $query = array_filter([ + 'client_id' => $this->config['client_id'], 'redirect_uri' => $this->config['redirect_url'], ]); - $url = $auth_url.'?'.http_build_query($query); - header('Location:'.$url); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); exit(); } @@ -29,10 +29,10 @@ public function authorization() public function getAccessToken() { $token_url = $this->source_url->accessToken(); - $query = array_filter([ - 'client_id' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_url'], - 'code' => Request::get('code'), + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_url'], + 'code' => Request::get('code'), 'client_secret' => $this->config['client_secret'], ]); return $this->http->request('POST', $token_url, [ @@ -50,7 +50,7 @@ public function getUserInfo($access_token) $user_info_url = $this->source_url->userInfo(); return json_decode($this->http->request('GET', $user_info_url, [ 'headers' => [ - 'Authorization' => 'Bearer '.$access_token, + 'Authorization' => 'Bearer ' . $access_token, ], ])->getBody()->getContents()); } diff --git a/src/Request/Source/AuthQqRequest.php b/src/Request/Source/AuthQqRequest.php new file mode 100644 index 0000000..15e5782 --- /dev/null +++ b/src/Request/Source/AuthQqRequest.php @@ -0,0 +1,100 @@ +source_url->authorize(); + $query = array_filter([ + 'response_type' => 'code', + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'scope' => '', + 'state' => $this->config['state'], + ]); + $url = $auth_url . '?' . http_build_query($query); + + header('Location:' . $url); + exit(); + + } + + public function getAccessToken() + { + if (isset($_GET['access_token'])) {//兼容app授权登陆 dcloud返回access_token; + return $_GET['access_token']; + } + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + 'redirect_uri' => $this->config['redirect_uri'], + 'fmt' => 'json', + ]); + $token_url = $this->source_url->accessToken(); + $res = $this->http->request('get', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + $data = json_decode($res); + if (isset($data->access_token)) { + return $data->access_token; + } else { + return new AuthException(AuthResponseStatus::FAILURE()); + } + } + + public function getUserInfo($access_token) + { + $result = $this->getUid($access_token); + $userinfo_url = $this->source_url->userInfo(); + $query = array_filter([ + 'openid' => $result->openid, + 'oauth_consumer_key' => $result->client_id, + 'access_token' => $access_token, + ]); + $userinfo = json_decode($this->http->request('GET', $userinfo_url, [ + 'query' => $query, + ])->getBody()->getContents()); + if (0 != $userinfo->ret) { + return new AuthException(AuthResponseStatus::GET_USERINFO_ERROR()); + } + $userinfo->openid = $this->getUid($access_token)->openid; + + $userinfo->unionid = $this->getUnionid($access_token)->unionid; + $user = new \stdClass(); + $user->openid = $userinfo->openid; + $user->unionid = $this->getUnionid($access_token)->unionid ?? ''; + $user->email = $user->openid . '@open.qq.com'; + $user->nickname = $userinfo->nickname; + $user->avatar = $userinfo->figureurl_2; + return $user; + } + + public function getUid($access_token) + { + $uid_url = $this->source_url->getUid(); + $str = $this->http->get($uid_url . $access_token . '&fmt=json')->getBody()->getContents(); + $user = json_decode($str); + if (isset($user->openid)) { + return $user; + } else { + return new AuthException(AuthResponseStatus::GET_OPENID_ERROR()); + } + } + + public function getUnionid($access_token) + { + $id_url = $this->source_url->getUnionid(); + $str = $this->http->get($id_url . $access_token . '&unionid=1&fmt=json')->getBody()->getContents(); + return json_decode($str); + } +} \ No newline at end of file From eb6ce625c2e5e508b91afde05edac34406cf326d Mon Sep 17 00:00:00 2001 From: pfinal Date: Sun, 6 Jun 2021 14:30:51 +0800 Subject: [PATCH 07/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=8E=E4=B8=BA?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/oauth.php | 41 +++++++++++-------- src/Config/AuthDefaultSource.php | 19 +++++++++ src/Request/Source/AuthCommonRequest.php | 2 +- src/Request/Source/AuthHuaweiRequest.php | 50 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 src/Request/Source/AuthHuaweiRequest.php diff --git a/config/oauth.php b/config/oauth.php index db63e13..59b1e28 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -1,34 +1,41 @@ [ - 'client_id' => env('GITHUB_CLIENT_ID', 'f4687fdd6801354516b3'), - 'redirect_url' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), + 'client_id' => env('GITHUB_CLIENT_ID', 'f4687fdd6801354516b3'), + 'redirect_url' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), 'client_secret' => env('GITHUB_SECRET', '03201d93b38f7eac722899420aaa35e6f375a8c4'), ], - 'gitee' => [ - 'client_id' => env('GITEE_CLIENT_ID', ''), - 'redirect_url' => env('GITEE_CALLBACK', ''), + 'gitee' => [ + 'client_id' => env('GITEE_CLIENT_ID', ''), + 'redirect_url' => env('GITEE_CALLBACK', ''), 'client_secret' => env('GITEE_SECRET', ''), ], - 'weibo' => [ - 'client_id' => env('WEIBO_CLIENT_ID', ''), - 'redirect_url' => env('WEIBO_CALLBACK', ''), + 'weibo' => [ + 'client_id' => env('WEIBO_CLIENT_ID', ''), + 'redirect_url' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), ], 'douyin' => [ - 'client_id' => env('WEIBO_CLIENT_ID', ''), - 'redirect_url' => env('WEIBO_CALLBACK', ''), + 'client_id' => env('WEIBO_CLIENT_ID', ''), + 'redirect_url' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), - 'state' => env('WEIBO_STATE', ''), + 'state' => env('WEIBO_STATE', ''), ], 'weixin' => [ - 'client_id' => env('WEIXIN_CLIENT_ID', ''), - 'redirect_url' => env('WEIXIN_CALLBACK', ''), + 'client_id' => env('WEIXIN_CLIENT_ID', ''), + 'redirect_url' => env('WEIXIN_CALLBACK', ''), 'client_secret' => env('WEIXIN_SECRET', ''), ], - 'qq' => [ - 'client_id' => env('QQ_CLIENT_ID', ''), - 'redirect_url' => env('QQs_CALLBACK', ''), - 'client_secret' => env('WEIBO_SECRET', ''), + 'qq' => [ + 'client_id' => env('QQ_CLIENT_ID', ''), + 'redirect_url' => env('QQ_CALLBACK', ''), + 'client_secret' => env('QQ_SECRET', ''), + ], + 'huawei' => [ + 'client_id' => env('HUAWEI_CLIENT_ID', ''), + 'redirect_url' => env('HUAWEI_CALLBACK', ''), + 'client_secret' => env('HUAWEI_SECRET', ''), + 'scope' => env('HUAWEI_SECRET', ''), + 'state' => env('HUAWEI_STATE', ''), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index 55eb5be..5757008 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -17,6 +17,9 @@ class AuthDefaultSource public $github; public $weibo; public $weixin; + public $douyin; + public $qq; + public $huawei; public function __construct() { @@ -131,6 +134,22 @@ public function getUnionid() return "https://graph.qq.com/oauth2.0/me?access_token="; } }; + $this->huawei = new class extends AuthSource { + public function authorize(): string + { + return "https://oauth-login.cloud.huawei.com/oauth2/v2/authorize"; + } + + public function accessToken(): string + { + return "https://oauth-login.cloud.huawei.com/oauth2/v3/token"; + } + + public function userInfo(): string + { + return "https://api.cloud.huawei.com/rest.php?nsp_fmt=JSON&nsp_svc=huawei.oauth2.user.getTokenInfo"; + } + }; } /** diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index a77fba4..6b1c5eb 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -10,7 +10,7 @@ use GuzzleHttp\Client; use JustAuth\Config\AuthSource; -abstract class AuthCommonRequest +abstract class AuthCommonRequest { /** diff --git a/src/Request/Source/AuthHuaweiRequest.php b/src/Request/Source/AuthHuaweiRequest.php new file mode 100644 index 0000000..e0f6593 --- /dev/null +++ b/src/Request/Source/AuthHuaweiRequest.php @@ -0,0 +1,50 @@ +source_url->authorize(); + $query = array_filter([ + 'response_type' => 'code', + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'access_type' => 'offline', + 'scope' => $this->config['scope'], + 'state' => $this->config['state'], + ]); + + $url = $auth_url . '?' . http_build_query($query); + + header('Location:' . $url); + exit(); + } + + public function getAccessToken() + { + $token_Url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'code' => Request::get('code') ?? Request::get('authorization_code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + 'redirect_uri' => $this->config['redirect_uri'], + ]); + $res = json_decode($this->http->request('post', $token_Url, [ + 'form_params' => $query, + ])->getBody()->getContents()); + + } + + public function getUserInfo($access_token) + { + // TODO: Implement getUserInfo() method. + } + +} \ No newline at end of file From 36f5abb9ec31891f9ff2675940433ba207410a61 Mon Sep 17 00:00:00 2001 From: pfinal Date: Sun, 6 Jun 2021 14:40:32 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=8E=E4=B8=BA?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 +++++++++------- src/Request/Source/AuthHuaweiRequest.php | 23 ++++++++++++++++------- test/Gitee/login.php | 15 --------------- test/Github/login.php | 14 -------------- test/OSChina/login.php | 5 ----- test/Weibo/login.php | 14 -------------- 6 files changed, 25 insertions(+), 62 deletions(-) delete mode 100644 test/Gitee/login.php delete mode 100644 test/Github/login.php delete mode 100644 test/OSChina/login.php delete mode 100644 test/Weibo/login.php diff --git a/README.md b/README.md index eef201d..7a43a8b 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,20 @@ PHP 版 JustAuth ## TODO -- [x] 先能用 -- [x] 支持 GITEE 平台 -- [x] 支持 GITHUB 平台 -- [x] 支持 微博 平台 +- [x] 先能用 +- [x] 支持 GITEE 平台 +- [x] 支持 GITHUB 平台 +- [x] 支持 微博 平台 +- [x] 支持 抖音 平台 +- [x] 支持 QQ 平台 +- [x] 支持 微信 平台 +- [x] 支持 华为 平台 - [ ] 支持 OSChina 平台 - [ ] 支持 百度 平台 -- [ ] 支持 抖音 平台 - [ ] 支持 钉钉 平台 - [ ] 支持 Coding 平台 - [ ] 支持 腾讯云开发者平台 平台 -- [ ] 支持 QQ 平台 -- [ ] 支持 微信 平台 + - [ ] 支持 淘宝 平台 - [ ] 支持 Google 平台 diff --git a/src/Request/Source/AuthHuaweiRequest.php b/src/Request/Source/AuthHuaweiRequest.php index e0f6593..6f577bb 100644 --- a/src/Request/Source/AuthHuaweiRequest.php +++ b/src/Request/Source/AuthHuaweiRequest.php @@ -29,22 +29,31 @@ public function authorization() public function getAccessToken() { $token_Url = $this->source_url->accessToken(); - $query = array_filter([ - 'client_id' => $this->config['client_id'], - 'code' => Request::get('code') ?? Request::get('authorization_code'), - 'grant_type' => 'authorization_code', + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'code' => Request::get('code') ?? Request::get('authorization_code'), + 'grant_type' => 'authorization_code', 'client_secret' => $this->config['client_secret'], - 'redirect_uri' => $this->config['redirect_uri'], + 'redirect_uri' => $this->config['redirect_uri'], ]); - $res = json_decode($this->http->request('post', $token_Url, [ + $res = json_decode($this->http->request('post', $token_Url, [ 'form_params' => $query, ])->getBody()->getContents()); + return $res->access_token; } public function getUserInfo($access_token) { - // TODO: Implement getUserInfo() method. + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'openid' => 'OPENID', + + 'access_token' => $access_token, + ]); + return json_decode($this->http->request('POST', $user_info_url, [ + 'form_params' => $query, + ])->getBody()->getContents()); } } \ No newline at end of file diff --git a/test/Gitee/login.php b/test/Gitee/login.php deleted file mode 100644 index 879aed7..0000000 --- a/test/Gitee/login.php +++ /dev/null @@ -1,15 +0,0 @@ -authorization(); -} catch (\Exception $e) { - echo $e->getMessage()."\n"; -} - diff --git a/test/Github/login.php b/test/Github/login.php deleted file mode 100644 index 1e5f5d0..0000000 --- a/test/Github/login.php +++ /dev/null @@ -1,14 +0,0 @@ -authorization(); -} catch (\Exception $e) { - echo $e->getMessage()."\n"; -} diff --git a/test/OSChina/login.php b/test/OSChina/login.php deleted file mode 100644 index fb83673..0000000 --- a/test/OSChina/login.php +++ /dev/null @@ -1,5 +0,0 @@ -authorization(); -} catch (\Exception $e) { - echo $e->getMessage()."\n"; -} From 84732a7cfe886257e8f9321b6e42a9100c9687b9 Mon Sep 17 00:00:00 2001 From: LXZ Date: Mon, 7 Jun 2021 15:45:20 +0800 Subject: [PATCH 09/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=B7=E6=AD=8C?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 27 +----------- config/oauth.php | 5 +++ src/Config/AuthDefaultSource.php | 26 ++++++++++- src/Request/AuthBase.php | 4 +- src/Request/Source/AuthGoogleRequest.php | 56 ++++++++++++++++++++++++ test/WeiboTest.php | 19 ++++++++ 6 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/Request/Source/AuthGoogleRequest.php create mode 100644 test/WeiboTest.php diff --git a/.gitignore b/.gitignore index a106d9f..37683d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,28 +1,5 @@ -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - /vendor/ .idea/ .phpunit.* -composer.lock \ No newline at end of file +composer.lock +/test/Web/ \ No newline at end of file diff --git a/config/oauth.php b/config/oauth.php index 59b1e28..b558c55 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -37,5 +37,10 @@ 'client_secret' => env('HUAWEI_SECRET', ''), 'scope' => env('HUAWEI_SECRET', ''), 'state' => env('HUAWEI_STATE', ''), + ], + 'google' => [ + 'client_id' => env('GOOGLE_CLIENT_ID', '967030938781-cuj9u0mtqsvu1b6gb80v5gl0s27iiltm.apps.googleusercontent.com'), + 'redirect_url' => env('GOOGLE_CALLBACK', 'dev.local.justauth.cn/test/Web/index.php'), + 'client_secret' => env('GOOGLE_SECRET', '9BBBRUWTaEswsOydqvzlX2GI'), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index 5757008..8998634 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -20,6 +20,7 @@ class AuthDefaultSource public $douyin; public $qq; public $huawei; + public $google; public function __construct() { @@ -124,12 +125,12 @@ public function userInfo(): string return "https://graph.qq.com/user/get_user_info"; } - public function getUid() + public function getUid(): string { return "https://graph.qq.com/oauth2.0/me?access_token="; } - public function getUnionid() + public function getUnionid(): string { return "https://graph.qq.com/oauth2.0/me?access_token="; } @@ -150,6 +151,27 @@ public function userInfo(): string return "https://api.cloud.huawei.com/rest.php?nsp_fmt=JSON&nsp_svc=huawei.oauth2.user.getTokenInfo"; } }; + $this->google = new class extends AuthSource { + public function authorize(): string + { + return "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline"; + } + + public function accessToken(): string + { + return "https://accounts.google.com/o/oauth2/token"; + } + + public function userInfo(): string + { + return "https://www.googleapis.com/oauth2/v1/userinfo"; + } + + public function scope(): string + { + return "https://www.googleapis.com/auth/userinfo.profile"; + } + }; } /** diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 66baf77..e35c923 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq']; + private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google']; protected $source_config; protected $config = []; @@ -23,7 +23,7 @@ public function __construct() $this->source_config = new AuthDefaultSource(); } - public function OAuth2($config, $driver): AuthApi + public function OAuth2($config, $driver) { try { # 加载配置文件 diff --git a/src/Request/Source/AuthGoogleRequest.php b/src/Request/Source/AuthGoogleRequest.php new file mode 100644 index 0000000..5d8be05 --- /dev/null +++ b/src/Request/Source/AuthGoogleRequest.php @@ -0,0 +1,56 @@ +source_url->authorize(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => urlencode($this->config['redirect_url']), + 'approval_prompt' => 'auto', + 'scope' => urlencode($this->source_url->scope()) + ]); + $url = $auth_url . '&state&' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + 'redirect_uri' => $this->config['redirect_uri'], + ]); + return $this->http->request('post', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + } + + public function getUserInfo($access_token) + { + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'access_token' => $access_token, + ]); + return json_decode($this->http->request('GET', $user_info_url, [ + 'query' => $query, + ])->getBody()->getContents()); + } + +} \ No newline at end of file diff --git a/test/WeiboTest.php b/test/WeiboTest.php new file mode 100644 index 0000000..696d63d --- /dev/null +++ b/test/WeiboTest.php @@ -0,0 +1,19 @@ +assertInstanceOf('JustAuth\Request\AuthApi', $weibo); + return $weibo; + } +} \ No newline at end of file From 219ff6310237effba3169af47bfa954558f47714 Mon Sep 17 00:00:00 2001 From: LXZ Date: Mon, 7 Jun 2021 17:01:01 +0800 Subject: [PATCH 10/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BE=E5=BA=A6?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +-- config/oauth.php | 19 +++++--- src/Config/AuthDefaultSource.php | 17 +++++++ src/Request/AuthBase.php | 2 +- src/Request/Source/AuthBaiduRequest.php | 56 ++++++++++++++++++++++++ src/Request/Source/AuthGoogleRequest.php | 2 +- 6 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 src/Request/Source/AuthBaiduRequest.php diff --git a/README.md b/README.md index 7a43a8b..e609cae 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,14 @@ PHP 版 JustAuth - [x] 支持 QQ 平台 - [x] 支持 微信 平台 - [x] 支持 华为 平台 -- [ ] 支持 OSChina 平台 +- [x] 支持 Google 平台 - [ ] 支持 百度 平台 +- [ ] 支持 OSChina 平台 - [ ] 支持 钉钉 平台 - [ ] 支持 Coding 平台 - [ ] 支持 腾讯云开发者平台 平台 - - [ ] 支持 淘宝 平台 -- [ ] 支持 Google 平台 + # JustAuth 项目一览 diff --git a/config/oauth.php b/config/oauth.php index b558c55..7a991f9 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -2,12 +2,12 @@ return [ 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID', 'f4687fdd6801354516b3'), - 'redirect_url' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), + 'redirect_uri' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), 'client_secret' => env('GITHUB_SECRET', '03201d93b38f7eac722899420aaa35e6f375a8c4'), ], 'gitee' => [ 'client_id' => env('GITEE_CLIENT_ID', ''), - 'redirect_url' => env('GITEE_CALLBACK', ''), + 'redirect_uri' => env('GITEE_CALLBACK', ''), 'client_secret' => env('GITEE_SECRET', ''), ], 'weibo' => [ @@ -17,30 +17,35 @@ ], 'douyin' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), - 'redirect_url' => env('WEIBO_CALLBACK', ''), + 'redirect_uri' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), 'state' => env('WEIBO_STATE', ''), ], 'weixin' => [ 'client_id' => env('WEIXIN_CLIENT_ID', ''), - 'redirect_url' => env('WEIXIN_CALLBACK', ''), + 'redirect_uri' => env('WEIXIN_CALLBACK', ''), 'client_secret' => env('WEIXIN_SECRET', ''), ], 'qq' => [ 'client_id' => env('QQ_CLIENT_ID', ''), - 'redirect_url' => env('QQ_CALLBACK', ''), + 'redirect_uri' => env('QQ_CALLBACK', ''), 'client_secret' => env('QQ_SECRET', ''), ], 'huawei' => [ 'client_id' => env('HUAWEI_CLIENT_ID', ''), - 'redirect_url' => env('HUAWEI_CALLBACK', ''), + 'redirect_uri' => env('HUAWEI_CALLBACK', ''), 'client_secret' => env('HUAWEI_SECRET', ''), 'scope' => env('HUAWEI_SECRET', ''), 'state' => env('HUAWEI_STATE', ''), ], 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID', '967030938781-cuj9u0mtqsvu1b6gb80v5gl0s27iiltm.apps.googleusercontent.com'), - 'redirect_url' => env('GOOGLE_CALLBACK', 'dev.local.justauth.cn/test/Web/index.php'), + 'redirect_uri' => env('GOOGLE_CALLBACK', 'dev.local.justauth.cn/test/Web/index.php'), 'client_secret' => env('GOOGLE_SECRET', '9BBBRUWTaEswsOydqvzlX2GI'), + ], + 'baidu' => [ + 'client_id' => env('BAIDU_CLIENT_ID', ''), + 'redirect_uri' => env('BAIDU_CALLBACK', ''), + 'client_secret' => env('GOOGLE_SECRET', ''), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index 8998634..059f5b3 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -21,6 +21,7 @@ class AuthDefaultSource public $qq; public $huawei; public $google; + public $baidu; public function __construct() { @@ -172,6 +173,22 @@ public function scope(): string return "https://www.googleapis.com/auth/userinfo.profile"; } }; + $this->baidu = new class extends AuthSource { + public function authorize(): string + { + return "http://openapi.baidu.com/oauth/2.0/authorize"; + } + + public function accessToken(): string + { + return "https://accounts.google.com/o/oauth2/token"; + } + + public function userInfo(): string + { + return "https://openapi.baidu.com/rest/2.0/passport/users/getInfo"; + } + }; } /** diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index e35c923..94aa0f2 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google']; + private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu']; protected $source_config; protected $config = []; diff --git a/src/Request/Source/AuthBaiduRequest.php b/src/Request/Source/AuthBaiduRequest.php new file mode 100644 index 0000000..4ef04ac --- /dev/null +++ b/src/Request/Source/AuthBaiduRequest.php @@ -0,0 +1,56 @@ +source_url->authorize(); + $query = array_filter([ + 'response_type' => 'code', + 'client_id' => $this->config['client_id'], + 'redirect_uri' => urlencode($this->config['redirect_uri']), + 'scope' => 'email', + 'display' => 'popup' + ]); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + 'redirect_uri' => $this->config['redirect_uri'], + ]); + return $this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + } + + public function getUserInfo($access_token) + { + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'access_token' => $access_token, + ]); + return json_decode($this->http->request('GET', $user_info_url, [ + 'query' => $query, + ])->getBody()->getContents()); + } +} \ No newline at end of file diff --git a/src/Request/Source/AuthGoogleRequest.php b/src/Request/Source/AuthGoogleRequest.php index 5d8be05..62194a6 100644 --- a/src/Request/Source/AuthGoogleRequest.php +++ b/src/Request/Source/AuthGoogleRequest.php @@ -18,7 +18,7 @@ public function authorization() $auth_url = $this->source_url->authorize(); $query = array_filter([ 'client_id' => $this->config['client_id'], - 'redirect_uri' => urlencode($this->config['redirect_url']), + 'redirect_uri' => urlencode($this->config['redirect_uri']), 'approval_prompt' => 'auto', 'scope' => urlencode($this->source_url->scope()) ]); From cc2de5980f3834c97fad700cd1ed3609d263ffe3 Mon Sep 17 00:00:00 2001 From: LXZ Date: Wed, 9 Jun 2021 14:37:27 +0800 Subject: [PATCH 11/19] update --- config/oauth.php | 22 +++++++++---------- src/Config/AuthDefaultSource.php | 2 +- src/Enums/AuthResponseStatus.php | 1 - src/Request/AuthApi.php | 28 +++++++++++++++--------- src/Request/Source/AuthGiteeRequest.php | 19 ++++++++++++---- src/Request/Source/AuthGithubRequest.php | 11 +++++++--- 6 files changed, 53 insertions(+), 30 deletions(-) diff --git a/config/oauth.php b/config/oauth.php index 7a991f9..e4cbab0 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -1,14 +1,14 @@ [ - 'client_id' => env('GITHUB_CLIENT_ID', 'f4687fdd6801354516b3'), - 'redirect_uri' => env('GITHUB_CALLBACK', 'dev.local.justauth.cn/test/Github/index.php'), - 'client_secret' => env('GITHUB_SECRET', '03201d93b38f7eac722899420aaa35e6f375a8c4'), + 'client_id' => env('GITHUB_CLIENT_ID', ''), + 'redirect_uri' => env('GITHUB_CALLBACK', ''), + 'client_secret' => env('GITHUB_SECRET', ''), ], 'gitee' => [ - 'client_id' => env('GITEE_CLIENT_ID', ''), - 'redirect_uri' => env('GITEE_CALLBACK', ''), - 'client_secret' => env('GITEE_SECRET', ''), + 'client_id' => env('GITEE_CLIENT_ID', '1b6f294f8a949a9eda9bc52d5c6514fdd61f7fdd05aec890f1611ba4281bf8e3'), + 'client_secret' => env('GITEE_SECRET', 'b297579d782c289d1a1988f5886d7dbdfc4dc301a8b9edfa8977b4c153a42717'), + 'redirect_uri' => env('GITEE_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/giteecallback.php'), ], 'weibo' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), @@ -39,13 +39,13 @@ 'state' => env('HUAWEI_STATE', ''), ], 'google' => [ - 'client_id' => env('GOOGLE_CLIENT_ID', '967030938781-cuj9u0mtqsvu1b6gb80v5gl0s27iiltm.apps.googleusercontent.com'), - 'redirect_uri' => env('GOOGLE_CALLBACK', 'dev.local.justauth.cn/test/Web/index.php'), - 'client_secret' => env('GOOGLE_SECRET', '9BBBRUWTaEswsOydqvzlX2GI'), + 'client_id' => env('GOOGLE_CLIENT_ID', ''), + 'redirect_uri' => env('GOOGLE_CALLBACK', ''), + 'client_secret' => env('GOOGLE_SECRET', ''), ], 'baidu' => [ - 'client_id' => env('BAIDU_CLIENT_ID', ''), + 'client_id' => env('BAIDU_CLIENT_ID', '24327410'), 'redirect_uri' => env('BAIDU_CALLBACK', ''), - 'client_secret' => env('GOOGLE_SECRET', ''), + 'client_secret' => env('GOOGLE_SECRET', 'cylPe0ReyOEsxIdXpKDq5hFG69oGQ3AV'), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index 059f5b3..f32035c 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -54,7 +54,7 @@ public function accessToken(): string public function userInfo(): string { - return "https://api.github.com/user"; + return "https://api.github.com/user/repos"; } }; $this->weibo = new class extends AuthSource { diff --git a/src/Enums/AuthResponseStatus.php b/src/Enums/AuthResponseStatus.php index 256af11..3202914 100644 --- a/src/Enums/AuthResponseStatus.php +++ b/src/Enums/AuthResponseStatus.php @@ -42,5 +42,4 @@ class AuthResponseStatus extends Enum /** @msg('Get user info eroor') * */ const GET_USERINFO_ERROR = 5013; - } \ No newline at end of file diff --git a/src/Request/AuthApi.php b/src/Request/AuthApi.php index be75c4d..8024ded 100644 --- a/src/Request/AuthApi.php +++ b/src/Request/AuthApi.php @@ -7,7 +7,6 @@ namespace JustAuth\Request; - use JustAuth\Enums\AuthResponseStatus; use JustAuth\Exception\AuthException; @@ -17,15 +16,15 @@ class AuthApi protected $driver; private $base_api_name_space = 'JustAuth\Request\Source\\'; - public function __construct($driver, array $config,$url) + public function __construct($driver, array $config, $url) { $this->driver = $driver; - $platform = ucfirst(strtolower($driver)); - $className = $this->base_api_name_space.'Auth'.$platform.'Request'; - if(!class_exists($className)) { + $platform = ucfirst(strtolower($driver)); + $className = $this->base_api_name_space . 'Auth' . $platform . 'Request'; + if (!class_exists($className)) { throw new AuthException(AuthResponseStatus::NOT_IMPLEMENTED()); } - return $this->api = new $className($config,$url); + return $this->api = new $className($config, $url); } public function authorization() @@ -35,12 +34,21 @@ public function authorization() public function getAccessToken(): string { - return $this->api->getAccessToken(); + try { + return $this->api->getAccessToken(); + }catch (AuthException $e) { + throw new AuthException($e->getCode(),$e->getMessage()); + } + } - public function getUserInfo(): object + public function getUserInfo(): array { - $oauth = $this->getAccessToken(); - return $this->api->getUserInfo($oauth); + try { + $oauth = $this->getAccessToken(); + return $this->api->getUserInfo($oauth); + }catch (AuthException $e) { + throw new AuthException($e->getCode(),$e->getMessage()); + } } } \ No newline at end of file diff --git a/src/Request/Source/AuthGiteeRequest.php b/src/Request/Source/AuthGiteeRequest.php index bdcc97a..9c2f627 100644 --- a/src/Request/Source/AuthGiteeRequest.php +++ b/src/Request/Source/AuthGiteeRequest.php @@ -7,6 +7,7 @@ namespace JustAuth\Request\Source; use pf\request\Request; +use JustAuth\Exception\AuthException; class AuthGiteeRequest extends AuthCommonRequest { @@ -37,9 +38,13 @@ public function getAccessToken() 'grant_type' => 'authorization_code', 'client_secret' => $this->config['client_secret'], ]); - return $this->http->request('POST', $token_url, [ - 'query' => $query, - ])->getBody()->getContents(); + try { + return $this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + }catch (\Throwable $throwable) { + throw new AuthException($throwable->getCode(),$throwable->getMessage()); + } } /** @@ -49,7 +54,13 @@ public function getAccessToken() */ public function getUserInfo($access_token) { + $access_data = json_decode($access_token); $user_info_url = $this->source_url->userInfo(); - return json_decode($this->http->get($user_info_url)->getBody()->getContents()); + $query = array_filter([ + 'access_token'=>$access_data->access_token + ]); + return json_decode($this->http->request('GET', $user_info_url, [ + 'query' => $query + ])->getBody()->getContents(),true); } } \ No newline at end of file diff --git a/src/Request/Source/AuthGithubRequest.php b/src/Request/Source/AuthGithubRequest.php index e1d5bbd..b3847bf 100644 --- a/src/Request/Source/AuthGithubRequest.php +++ b/src/Request/Source/AuthGithubRequest.php @@ -7,7 +7,7 @@ namespace JustAuth\Request\Source; use pf\request\Request; - +use JustAuth\Exception\AuthException; class AuthGithubRequest extends AuthCommonRequest { /** @@ -47,11 +47,16 @@ public function getAccessToken() */ public function getUserInfo($access_token) { + $access_data = []; + parse_str($access_token,$access_data); + if(count($access_data)<=0 || isset($access_data['error'])) { + throw new AuthException(401,$access_data['error_description']); + } $user_info_url = $this->source_url->userInfo(); return json_decode($this->http->request('GET', $user_info_url, [ 'headers' => [ - 'Authorization' => 'Bearer ' . $access_token, + 'Authorization' => 'Bearer ' . $access_data['access_token'], ], - ])->getBody()->getContents()); + ])->getBody()->getContents(),true); } } \ No newline at end of file From f5d3504150498c902d5fd7ae293bfdcb40d1aba3 Mon Sep 17 00:00:00 2001 From: LXZ Date: Wed, 9 Jun 2021 15:34:17 +0800 Subject: [PATCH 12/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/oauth.php | 4 ++-- src/Config/AuthDefaultSource.php | 2 +- src/Request/Source/AuthBaiduRequest.php | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/config/oauth.php b/config/oauth.php index e4cbab0..53cbb40 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -44,8 +44,8 @@ 'client_secret' => env('GOOGLE_SECRET', ''), ], 'baidu' => [ - 'client_id' => env('BAIDU_CLIENT_ID', '24327410'), - 'redirect_uri' => env('BAIDU_CALLBACK', ''), + 'client_id' => env('BAIDU_CLIENT_ID', '5W1Zf0nzztcdhwi0f5epcT0p'), + 'redirect_uri' => env('BAIDU_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/baiducallback.php'), 'client_secret' => env('GOOGLE_SECRET', 'cylPe0ReyOEsxIdXpKDq5hFG69oGQ3AV'), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index f32035c..d141aa8 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -181,7 +181,7 @@ public function authorize(): string public function accessToken(): string { - return "https://accounts.google.com/o/oauth2/token"; + return "https://openapi.baidu.com/oauth/2.0/token"; } public function userInfo(): string diff --git a/src/Request/Source/AuthBaiduRequest.php b/src/Request/Source/AuthBaiduRequest.php index 4ef04ac..af630f8 100644 --- a/src/Request/Source/AuthBaiduRequest.php +++ b/src/Request/Source/AuthBaiduRequest.php @@ -19,8 +19,7 @@ public function authorization() $query = array_filter([ 'response_type' => 'code', 'client_id' => $this->config['client_id'], - 'redirect_uri' => urlencode($this->config['redirect_uri']), - 'scope' => 'email', + 'redirect_uri' => $this->config['redirect_uri'], 'display' => 'popup' ]); $url = $auth_url . '?' . http_build_query($query); @@ -38,19 +37,26 @@ public function getAccessToken() 'client_secret' => $this->config['client_secret'], 'redirect_uri' => $this->config['redirect_uri'], ]); - return $this->http->request('POST', $token_url, [ - 'query' => $query, - ])->getBody()->getContents(); + try { + return $this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + } catch (\Throwable $throwable) { + var_dump($throwable->getCode()); + throw new AuthException($throwable->getCode(), $throwable->getMessage()); + } + } public function getUserInfo($access_token) { + $access_data = json_decode($access_token); $user_info_url = $this->source_url->userInfo(); $query = array_filter([ - 'access_token' => $access_token, + 'access_token' => $access_data->access_token ]); return json_decode($this->http->request('GET', $user_info_url, [ 'query' => $query, - ])->getBody()->getContents()); + ])->getBody()->getContents(),true); } } \ No newline at end of file From 7a2fba5fe862fc2ca4509d0df3f2394ef2dfb5a8 Mon Sep 17 00:00:00 2001 From: LXZ Date: Wed, 9 Jun 2021 15:50:57 +0800 Subject: [PATCH 13/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +-- config/oauth.php | 35 ++++++------ src/Config/AuthDefaultSource.php | 17 ++++++ src/Request/AuthBase.php | 2 +- src/Request/Source/AuthOschinaRequest.php | 66 +++++++++++++++++++++++ test/WeiboTest.php | 19 ------- test/common.php | 7 --- 7 files changed, 107 insertions(+), 45 deletions(-) create mode 100644 src/Request/Source/AuthOschinaRequest.php delete mode 100644 test/WeiboTest.php delete mode 100644 test/common.php diff --git a/README.md b/README.md index e609cae..696a879 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,14 @@ PHP 版 JustAuth - [x] 先能用 - [x] 支持 GITEE 平台 - [x] 支持 GITHUB 平台 +- [x] 支持 OSChina 平台 +- [x] 支持 百度 平台 - [x] 支持 微博 平台 - [x] 支持 抖音 平台 - [x] 支持 QQ 平台 - [x] 支持 微信 平台 -- [x] 支持 华为 平台 +- [x] 支持 华为 平台 - [x] 支持 Google 平台 -- [ ] 支持 百度 平台 -- [ ] 支持 OSChina 平台 - [ ] 支持 钉钉 平台 - [ ] 支持 Coding 平台 - [ ] 支持 腾讯云开发者平台 平台 diff --git a/config/oauth.php b/config/oauth.php index 53cbb40..98a16b4 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -1,51 +1,56 @@ [ + 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID', ''), 'redirect_uri' => env('GITHUB_CALLBACK', ''), 'client_secret' => env('GITHUB_SECRET', ''), ], - 'gitee' => [ - 'client_id' => env('GITEE_CLIENT_ID', '1b6f294f8a949a9eda9bc52d5c6514fdd61f7fdd05aec890f1611ba4281bf8e3'), - 'client_secret' => env('GITEE_SECRET', 'b297579d782c289d1a1988f5886d7dbdfc4dc301a8b9edfa8977b4c153a42717'), - 'redirect_uri' => env('GITEE_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/giteecallback.php'), + 'gitee' => [ + 'client_id' => env('GITEE_CLIENT_ID', ''), + 'client_secret' => env('GITEE_SECRET', ''), + 'redirect_uri' => env('GITEE_CALLBACK', ''), ], - 'weibo' => [ + 'weibo' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), 'redirect_url' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), ], - 'douyin' => [ + 'douyin' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), 'redirect_uri' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), 'state' => env('WEIBO_STATE', ''), ], - 'weixin' => [ + 'weixin' => [ 'client_id' => env('WEIXIN_CLIENT_ID', ''), 'redirect_uri' => env('WEIXIN_CALLBACK', ''), 'client_secret' => env('WEIXIN_SECRET', ''), ], - 'qq' => [ + 'qq' => [ 'client_id' => env('QQ_CLIENT_ID', ''), 'redirect_uri' => env('QQ_CALLBACK', ''), 'client_secret' => env('QQ_SECRET', ''), ], - 'huawei' => [ + 'huawei' => [ 'client_id' => env('HUAWEI_CLIENT_ID', ''), 'redirect_uri' => env('HUAWEI_CALLBACK', ''), 'client_secret' => env('HUAWEI_SECRET', ''), 'scope' => env('HUAWEI_SECRET', ''), 'state' => env('HUAWEI_STATE', ''), ], - 'google' => [ + 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID', ''), 'redirect_uri' => env('GOOGLE_CALLBACK', ''), 'client_secret' => env('GOOGLE_SECRET', ''), ], - 'baidu' => [ - 'client_id' => env('BAIDU_CLIENT_ID', '5W1Zf0nzztcdhwi0f5epcT0p'), - 'redirect_uri' => env('BAIDU_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/baiducallback.php'), - 'client_secret' => env('GOOGLE_SECRET', 'cylPe0ReyOEsxIdXpKDq5hFG69oGQ3AV'), + 'baidu' => [ + 'client_id' => env('BAIDU_CLIENT_ID', ''), + 'redirect_uri' => env('BAIDU_CALLBACK', ''), + 'client_secret' => env('GOOGLE_SECRET', ''), + ], + 'oschina' => [ + 'client_id' => env('OSCHINA_CLIENT_ID', ''), + 'redirect_uri' => env('OSCHINA_CALLBACK', ''), + 'client_secret' => env('OSCHINA_SECRET', ''), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index d141aa8..c56fb5c 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -22,6 +22,7 @@ class AuthDefaultSource public $huawei; public $google; public $baidu; + public $oschina; public function __construct() { @@ -189,6 +190,22 @@ public function userInfo(): string return "https://openapi.baidu.com/rest/2.0/passport/users/getInfo"; } }; + $this->oschina = new class extends AuthSource { + public function authorize(): string + { + return "https://www.oschina.net/action/oauth2/authorize"; + } + + public function accessToken(): string + { + return "https://www.oschina.net/action/openapi/token"; + } + + public function userInfo(): string + { + return "https://www.oschina.net/action/openapi/my_information"; + } + }; } /** diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 94aa0f2..9038bad 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu']; + private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu','oschina']; protected $source_config; protected $config = []; diff --git a/src/Request/Source/AuthOschinaRequest.php b/src/Request/Source/AuthOschinaRequest.php new file mode 100644 index 0000000..a287181 --- /dev/null +++ b/src/Request/Source/AuthOschinaRequest.php @@ -0,0 +1,66 @@ +source_url->authorize(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'response_type' => 'code', + ]); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'code' => Request::get('code'), + 'grant_type' => 'authorization_code', + 'client_secret' => $this->config['client_secret'], + ]); + try { + return $this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + }catch (\Throwable $throwable) { + throw new AuthException($throwable->getCode(),$throwable->getMessage()); + } + } + + /** + * 获取用户信息 + * @param $access_token + * @return mixed + */ + public function getUserInfo($access_token) + { + $access_data = json_decode($access_token); + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'access_token'=>$access_data->access_token + ]); + return json_decode($this->http->request('GET', $user_info_url, [ + 'query' => $query + ])->getBody()->getContents(),true); + } +} \ No newline at end of file diff --git a/test/WeiboTest.php b/test/WeiboTest.php deleted file mode 100644 index 696d63d..0000000 --- a/test/WeiboTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertInstanceOf('JustAuth\Request\AuthApi', $weibo); - return $weibo; - } -} \ No newline at end of file diff --git a/test/common.php b/test/common.php deleted file mode 100644 index f38b224..0000000 --- a/test/common.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Wed, 9 Jun 2021 19:01:03 +0800 Subject: [PATCH 14/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +- config/oauth.php | 6 ++ src/Config/AuthDefaultSource.php | 18 +++++ src/Request/AuthBase.php | 2 +- .../Source/AuthStackoverflowRequest.php | 70 +++++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 src/Request/Source/AuthStackoverflowRequest.php diff --git a/README.md b/README.md index 696a879..e420c70 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,14 @@ PHP 版 JustAuth - [x] 支持 GITEE 平台 - [x] 支持 GITHUB 平台 - [x] 支持 OSChina 平台 +- [x] 支持 StackOverflow 平台 - [x] 支持 百度 平台 - [x] 支持 微博 平台 - [x] 支持 抖音 平台 - [x] 支持 QQ 平台 -- [x] 支持 微信 平台 -- [x] 支持 华为 平台 -- [x] 支持 Google 平台 +- [ ] 支持 微信 平台 +- [ ] 支持 华为 平台 +- [ ] 支持 Google 平台 - [ ] 支持 钉钉 平台 - [ ] 支持 Coding 平台 - [ ] 支持 腾讯云开发者平台 平台 diff --git a/config/oauth.php b/config/oauth.php index 98a16b4..6aab309 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -52,5 +52,11 @@ 'client_id' => env('OSCHINA_CLIENT_ID', ''), 'redirect_uri' => env('OSCHINA_CALLBACK', ''), 'client_secret' => env('OSCHINA_SECRET', ''), + ], + 'stackoverflow'=>[ + 'client_id' => env('OSCHINA_CLIENT_ID', '20387'), + 'redirect_uri' => env('OSCHINA_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/stackoverflowcallback.php'), + 'client_secret' => env('OSCHINA_SECRET', 'TGbMONBLFplTXX6UsZ69fA(('), + 'client_key' => env('OSCHINA_SECRET', '9sNV4N359bH0Q1Go4gdJbw(('), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index c56fb5c..f806402 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -23,6 +23,7 @@ class AuthDefaultSource public $google; public $baidu; public $oschina; + public $stackoverflow; public function __construct() { @@ -206,6 +207,23 @@ public function userInfo(): string return "https://www.oschina.net/action/openapi/my_information"; } }; + $this->stackoverflow = new class extends AuthSource { + public function authorize(): string + { + return "https://stackoverflow.com/oauth"; + } + + public function accessToken(): string + { + return "https://stackoverflow.com/oauth/access_token/json"; + } + + public function userInfo(): string + { + return "https://api.stackexchange.com/2.2/me"; + } + }; + } /** diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 9038bad..942444e 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu','oschina']; + private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu','oschina','stackoverflow']; protected $source_config; protected $config = []; diff --git a/src/Request/Source/AuthStackoverflowRequest.php b/src/Request/Source/AuthStackoverflowRequest.php new file mode 100644 index 0000000..75a01c6 --- /dev/null +++ b/src/Request/Source/AuthStackoverflowRequest.php @@ -0,0 +1,70 @@ +source_url->authorize(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'response_type' => 'code', + ]); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'client_id' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'code' => Request::get('code'), + 'client_secret' => $this->config['client_secret'], + ]); + try { + return $this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents(); + } catch (\Throwable $throwable) { + throw new AuthException($throwable->getCode(), $throwable->getMessage()); + } + } + + /** + * 获取用户信息 + * @param $access_token + * @return mixed + */ + public function getUserInfo($access_token) + { + $access_data = json_decode($access_token); + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'order' => 'desc', + 'sort' => 'reputation', + 'site' => 'stackoverflow', + 'filter' => '!6W.6dPDp64PF2', + 'key' => $this->config['client_key'], + 'access_token' => $access_data->access_token + ]); + return json_decode($this->http->request('GET', $user_info_url, [ + 'query' => $query + ])->getBody()->getContents(), true); + } +} \ No newline at end of file From 20c6a4cbfe0dfbea27e388e620e4790d9d156508 Mon Sep 17 00:00:00 2001 From: LXZ Date: Wed, 9 Jun 2021 19:32:26 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/oauth.php | 8 ++++---- src/Request/Source/AuthGithubRequest.php | 4 ++-- test/GithubTest.php | 12 ------------ test/OSChinaTest.php | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 test/OSChinaTest.php diff --git a/config/oauth.php b/config/oauth.php index 6aab309..336e271 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -54,9 +54,9 @@ 'client_secret' => env('OSCHINA_SECRET', ''), ], 'stackoverflow'=>[ - 'client_id' => env('OSCHINA_CLIENT_ID', '20387'), - 'redirect_uri' => env('OSCHINA_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/stackoverflowcallback.php'), - 'client_secret' => env('OSCHINA_SECRET', 'TGbMONBLFplTXX6UsZ69fA(('), - 'client_key' => env('OSCHINA_SECRET', '9sNV4N359bH0Q1Go4gdJbw(('), + 'client_id' => env('OSCHINA_CLIENT_ID', ''), + 'redirect_uri' => env('OSCHINA_CALLBACK', ''), + 'client_secret' => env('OSCHINA_SECRET', ''), + 'client_key' => env('OSCHINA_SECRET', ''), ] ]; \ No newline at end of file diff --git a/src/Request/Source/AuthGithubRequest.php b/src/Request/Source/AuthGithubRequest.php index b3847bf..2e144d5 100644 --- a/src/Request/Source/AuthGithubRequest.php +++ b/src/Request/Source/AuthGithubRequest.php @@ -18,7 +18,7 @@ public function authorization() $auth_url = $this->source_url->authorize(); $query = array_filter([ 'client_id' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_url'], + 'redirect_uri' => $this->config['redirect_uri'], ]); $url = $auth_url . '?' . http_build_query($query); header('Location:' . $url); @@ -31,7 +31,7 @@ public function getAccessToken() $token_url = $this->source_url->accessToken(); $query = array_filter([ 'client_id' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_url'], + 'redirect_uri' => $this->config['redirect_uri'], 'code' => Request::get('code'), 'client_secret' => $this->config['client_secret'], ]); diff --git a/test/GithubTest.php b/test/GithubTest.php index 14697a3..1488591 100644 --- a/test/GithubTest.php +++ b/test/GithubTest.php @@ -13,16 +13,4 @@ public function testInstance() $this->assertInstanceOf('JustAuth\Request\AuthApi', $github); return $github; } - -// /** -// * @depends testInstance -// * -// * @param \JustAuth\Request\AuthApi $github -// * -// */ -// public function testRouterAdd(\JustAuth\Request\AuthApi $github) -// { -// $github->authorization(); -// } - } \ No newline at end of file diff --git a/test/OSChinaTest.php b/test/OSChinaTest.php new file mode 100644 index 0000000..f94f460 --- /dev/null +++ b/test/OSChinaTest.php @@ -0,0 +1,16 @@ +assertInstanceOf('JustAuth\Request\AuthApi', $github); + return $github; + } +} \ No newline at end of file From 8d13fc5a7a51f70d722db57798de564345aac39c Mon Sep 17 00:00:00 2001 From: LXZ Date: Fri, 11 Jun 2021 19:07:43 +0800 Subject: [PATCH 16/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=92=89=E9=92=89?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 +- config/oauth.php | 29 ++++---- src/Config/AuthDefaultSource.php | 17 +++++ src/Request/AuthApi.php | 15 ++-- src/Request/AuthBase.php | 2 +- src/Request/Source/AuthCommonRequest.php | 2 +- src/Request/Source/AuthDingtalkRequest.php | 83 ++++++++++++++++++++++ src/helper.php | 15 +++- 8 files changed, 145 insertions(+), 22 deletions(-) create mode 100644 src/Request/Source/AuthDingtalkRequest.php diff --git a/composer.json b/composer.json index 3df4382..6eadc56 100755 --- a/composer.json +++ b/composer.json @@ -26,7 +26,9 @@ "psr-4": { "JustAuth\\": "src/" }, - "files": [] + "files": [ + "src/helper.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/config/oauth.php b/config/oauth.php index 336e271..230fbc5 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -1,62 +1,67 @@ [ + 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID', ''), 'redirect_uri' => env('GITHUB_CALLBACK', ''), 'client_secret' => env('GITHUB_SECRET', ''), ], - 'gitee' => [ + 'gitee' => [ 'client_id' => env('GITEE_CLIENT_ID', ''), 'client_secret' => env('GITEE_SECRET', ''), 'redirect_uri' => env('GITEE_CALLBACK', ''), ], - 'weibo' => [ + 'weibo' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), 'redirect_url' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), ], - 'douyin' => [ + 'douyin' => [ 'client_id' => env('WEIBO_CLIENT_ID', ''), 'redirect_uri' => env('WEIBO_CALLBACK', ''), 'client_secret' => env('WEIBO_SECRET', ''), 'state' => env('WEIBO_STATE', ''), ], - 'weixin' => [ + 'weixin' => [ 'client_id' => env('WEIXIN_CLIENT_ID', ''), 'redirect_uri' => env('WEIXIN_CALLBACK', ''), 'client_secret' => env('WEIXIN_SECRET', ''), ], - 'qq' => [ + 'qq' => [ 'client_id' => env('QQ_CLIENT_ID', ''), 'redirect_uri' => env('QQ_CALLBACK', ''), 'client_secret' => env('QQ_SECRET', ''), ], - 'huawei' => [ + 'huawei' => [ 'client_id' => env('HUAWEI_CLIENT_ID', ''), 'redirect_uri' => env('HUAWEI_CALLBACK', ''), 'client_secret' => env('HUAWEI_SECRET', ''), 'scope' => env('HUAWEI_SECRET', ''), 'state' => env('HUAWEI_STATE', ''), ], - 'google' => [ + 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID', ''), 'redirect_uri' => env('GOOGLE_CALLBACK', ''), 'client_secret' => env('GOOGLE_SECRET', ''), ], - 'baidu' => [ + 'baidu' => [ 'client_id' => env('BAIDU_CLIENT_ID', ''), 'redirect_uri' => env('BAIDU_CALLBACK', ''), 'client_secret' => env('GOOGLE_SECRET', ''), ], - 'oschina' => [ + 'oschina' => [ 'client_id' => env('OSCHINA_CLIENT_ID', ''), 'redirect_uri' => env('OSCHINA_CALLBACK', ''), 'client_secret' => env('OSCHINA_SECRET', ''), ], - 'stackoverflow'=>[ + 'stackoverflow' => [ 'client_id' => env('OSCHINA_CLIENT_ID', ''), 'redirect_uri' => env('OSCHINA_CALLBACK', ''), 'client_secret' => env('OSCHINA_SECRET', ''), - 'client_key' => env('OSCHINA_SECRET', ''), + 'client_key' => env('OSCHINA_SECRET', ''), + ], + 'dingtalk' => [ + 'client_id' => env('OSCHINA_CLIENT_ID', 'dingoaot7zvjufgpczovnh'), + 'redirect_uri' => env('OSCHINA_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/dingtalkcallback.php'), + 'client_secret' => env('OSCHINA_SECRET', 'imZeDdjjapSejLDZ3IBhW9qbSj9gjtxOGzBb_7Aq_ZlQ2eodCgM3ScQNQFGtk_d1'), ] ]; \ No newline at end of file diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index f806402..bc45005 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -24,6 +24,7 @@ class AuthDefaultSource public $baidu; public $oschina; public $stackoverflow; + public $dingtalk; public function __construct() { @@ -223,6 +224,22 @@ public function userInfo(): string return "https://api.stackexchange.com/2.2/me"; } }; + $this->dingtalk = new class extends AuthSource { + public function authorize(): string + { + return "https://oapi.dingtalk.com/connect/qrconnect"; + } + + public function accessToken(): string + { + return "https://oapi.dingtalk.com/sns/getuserinfo_bycode"; + } + + public function userInfo(): string + { + return "https://oapi.dingtalk.com/sns/getuserinfo_bycode"; + } + }; } diff --git a/src/Request/AuthApi.php b/src/Request/AuthApi.php index 8024ded..6f837d4 100644 --- a/src/Request/AuthApi.php +++ b/src/Request/AuthApi.php @@ -36,8 +36,8 @@ public function getAccessToken(): string { try { return $this->api->getAccessToken(); - }catch (AuthException $e) { - throw new AuthException($e->getCode(),$e->getMessage()); + } catch (AuthException $e) { + throw new AuthException($e->getCode(), $e->getMessage()); } } @@ -45,10 +45,13 @@ public function getAccessToken(): string public function getUserInfo(): array { try { - $oauth = $this->getAccessToken(); - return $this->api->getUserInfo($oauth); - }catch (AuthException $e) { - throw new AuthException($e->getCode(),$e->getMessage()); + if (!in_array($this->driver, ['dingtalk'])) { + $oauth = $this->getAccessToken(); + return $this->api->getUserInfo($oauth); + } + return $this->api->getUserInfo(); + } catch (AuthException $e) { + throw new AuthException($e->getCode(), $e->getMessage()); } } } \ No newline at end of file diff --git a/src/Request/AuthBase.php b/src/Request/AuthBase.php index 942444e..c79fe6a 100644 --- a/src/Request/AuthBase.php +++ b/src/Request/AuthBase.php @@ -14,7 +14,7 @@ class AuthBase { - private $driver = ['gitee', 'github', 'weibo', 'weixin', 'douyin', 'qq','huawei','google','baidu','oschina','stackoverflow']; + private $driver = ['gitee', 'github', 'dingtalk', 'weibo', 'weixin', 'douyin', 'qq', 'huawei', 'google', 'baidu', 'oschina', 'stackoverflow']; protected $source_config; protected $config = []; diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index 6b1c5eb..69b64a6 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -85,6 +85,6 @@ abstract public function authorization(); abstract public function getAccessToken(); - abstract public function getUserInfo($access_token); + abstract public function getUserInfo($access_token = ''); } \ No newline at end of file diff --git a/src/Request/Source/AuthDingtalkRequest.php b/src/Request/Source/AuthDingtalkRequest.php new file mode 100644 index 0000000..32b2511 --- /dev/null +++ b/src/Request/Source/AuthDingtalkRequest.php @@ -0,0 +1,83 @@ +source_url->authorize(); + $query = array_filter([ + 'appid' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_uri'], + 'response_type' => 'code', + 'scope' => 'snsapi_login', + 'state' => 'STATE', + ]); + $url = $auth_url . '?' . http_build_query($query); + header('Location:' . $url); + exit(); + } + + + public function getAccessToken() + { + $token_url = $this->source_url->accessToken(); + $query = array_filter([ + 'accessKey' => $this->config['client_id'], + 'redirect_uri' => $this->config['redirect_url'], + 'code' => Request::get('code'), + 'client_secret' => $this->config['client_secret'], + 'grant_type' => 'authorization_code' + ]); + return json_decode($this->http->request('POST', $token_url, [ + 'query' => $query, + ])->getBody()->getContents())->access_token; + } + + + public function getUserInfo($access_token = '') + { + $time = millisecondWay(); + $signature = $this->_setSignature($time); + $user_info_url = $this->source_url->userInfo(); + $query = array_filter([ + 'accessKey' => $this->config['client_id'], + 'signature' => $signature, + 'timestamp' => $time, + ]); + var_dump($this->http->request('POST', $user_info_url, [ + 'query' => $query, + 'form_params' => [ + 'tmp_auth_code' => Request::get('code') + ] + ])->getBody()->getContents());exit(); + return json_decode($this->http->request('POST', $user_info_url, [ + 'query' => $query, + 'body' => [ + 'tmp_auth_code' => Request::get('code') + ] + ])->getBody()->getContents()); + } + + private function _setSignature($time) + { + $s = hash_hmac('sha256', $time, $this->config['client_secret'], true); + $signature = base64_encode($s); + return urlencode($signature); + } + + public function getUid($access_token) + { + $user_id_url = $this->source_url->userId(); + $result = $this->http->post($user_id_url . $access_token); + $result = json_decode($result->getBody()->getContents(), true); + return $result['uid']; + } +} \ No newline at end of file diff --git a/src/helper.php b/src/helper.php index f618f57..eed15f9 100644 --- a/src/helper.php +++ b/src/helper.php @@ -2,4 +2,17 @@ /** * @author pfinal南丞 * @date 2021年04月06日 下午6:18 - */ \ No newline at end of file + */ + +if (!function_exists('millisecondWay')) { + /** + * 获取一个毫秒级的时间戳 13位 + * @return float + */ + function millisecondWay(): float + { + ini_set('date.timezone', 'PRC'); + list($msec, $sec) = explode(' ', microtime()); + return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); + } +} \ No newline at end of file From 0460a87fe401acd030e4d9669ea38b801a63461e Mon Sep 17 00:00:00 2001 From: LXZ Date: Fri, 11 Jun 2021 19:54:51 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=92=89=E9=92=89?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Config/AuthDefaultSource.php | 2 +- src/Request/AuthApi.php | 15 ++++++-------- src/Request/Source/AuthCommonRequest.php | 2 +- src/Request/Source/AuthDingtalkRequest.php | 23 +++++++++++----------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index bc45005..9c4a345 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -232,7 +232,7 @@ public function authorize(): string public function accessToken(): string { - return "https://oapi.dingtalk.com/sns/getuserinfo_bycode"; + return "https://oapi.dingtalk.com/sns/gettoken"; } public function userInfo(): string diff --git a/src/Request/AuthApi.php b/src/Request/AuthApi.php index 6f837d4..8024ded 100644 --- a/src/Request/AuthApi.php +++ b/src/Request/AuthApi.php @@ -36,8 +36,8 @@ public function getAccessToken(): string { try { return $this->api->getAccessToken(); - } catch (AuthException $e) { - throw new AuthException($e->getCode(), $e->getMessage()); + }catch (AuthException $e) { + throw new AuthException($e->getCode(),$e->getMessage()); } } @@ -45,13 +45,10 @@ public function getAccessToken(): string public function getUserInfo(): array { try { - if (!in_array($this->driver, ['dingtalk'])) { - $oauth = $this->getAccessToken(); - return $this->api->getUserInfo($oauth); - } - return $this->api->getUserInfo(); - } catch (AuthException $e) { - throw new AuthException($e->getCode(), $e->getMessage()); + $oauth = $this->getAccessToken(); + return $this->api->getUserInfo($oauth); + }catch (AuthException $e) { + throw new AuthException($e->getCode(),$e->getMessage()); } } } \ No newline at end of file diff --git a/src/Request/Source/AuthCommonRequest.php b/src/Request/Source/AuthCommonRequest.php index 69b64a6..6b1c5eb 100644 --- a/src/Request/Source/AuthCommonRequest.php +++ b/src/Request/Source/AuthCommonRequest.php @@ -85,6 +85,6 @@ abstract public function authorization(); abstract public function getAccessToken(); - abstract public function getUserInfo($access_token = ''); + abstract public function getUserInfo($access_token); } \ No newline at end of file diff --git a/src/Request/Source/AuthDingtalkRequest.php b/src/Request/Source/AuthDingtalkRequest.php index 32b2511..af5e512 100644 --- a/src/Request/Source/AuthDingtalkRequest.php +++ b/src/Request/Source/AuthDingtalkRequest.php @@ -30,20 +30,20 @@ public function getAccessToken() { $token_url = $this->source_url->accessToken(); $query = array_filter([ - 'accessKey' => $this->config['client_id'], - 'redirect_uri' => $this->config['redirect_url'], - 'code' => Request::get('code'), - 'client_secret' => $this->config['client_secret'], - 'grant_type' => 'authorization_code' + 'appid' => $this->config['client_id'], + 'appsecret' => $this->config['client_secret'], ]); - return json_decode($this->http->request('POST', $token_url, [ + return $this->http->request('GET', $token_url, [ 'query' => $query, - ])->getBody()->getContents())->access_token; + ])->getBody()->getContents(); } - public function getUserInfo($access_token = '') + public function getUserInfo($access_token) { + $access_data = json_decode($access_token, true); + print_r($access_data); + exit(); $time = millisecondWay(); $signature = $this->_setSignature($time); $user_info_url = $this->source_url->userInfo(); @@ -53,11 +53,12 @@ public function getUserInfo($access_token = '') 'timestamp' => $time, ]); var_dump($this->http->request('POST', $user_info_url, [ - 'query' => $query, - 'form_params' => [ + 'query' => $query, + 'form_params' => [ 'tmp_auth_code' => Request::get('code') ] - ])->getBody()->getContents());exit(); + ])->getBody()->getContents()); + exit(); return json_decode($this->http->request('POST', $user_info_url, [ 'query' => $query, 'body' => [ From 6577070732cf3622fe6000a97251f341b6e1aeba Mon Sep 17 00:00:00 2001 From: pfinal Date: Sun, 13 Jun 2021 16:49:09 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=92=89=E9=92=89?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/Config/AuthDefaultSource.php | 29 +++++++------ src/Request/Source/AuthDingtalkRequest.php | 49 +++++++++++++++------- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index e420c70..6d3bf59 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ PHP 版 JustAuth - [x] 支持 GITHUB 平台 - [x] 支持 OSChina 平台 - [x] 支持 StackOverflow 平台 +- [x] 支持 钉钉 平台 - [x] 支持 百度 平台 - [x] 支持 微博 平台 - [x] 支持 抖音 平台 @@ -14,7 +15,6 @@ PHP 版 JustAuth - [ ] 支持 微信 平台 - [ ] 支持 华为 平台 - [ ] 支持 Google 平台 -- [ ] 支持 钉钉 平台 - [ ] 支持 Coding 平台 - [ ] 支持 腾讯云开发者平台 平台 - [ ] 支持 淘宝 平台 diff --git a/src/Config/AuthDefaultSource.php b/src/Config/AuthDefaultSource.php index 9c4a345..60c03d1 100644 --- a/src/Config/AuthDefaultSource.php +++ b/src/Config/AuthDefaultSource.php @@ -28,7 +28,7 @@ class AuthDefaultSource public function __construct() { - $this->gitee = new class extends AuthSource { + $this->gitee = new class extends AuthSource { public function authorize(): string { return "https://gitee.com/oauth/authorize"; @@ -44,7 +44,7 @@ public function userInfo(): string return "https://gitee.com/api/v5/user"; } }; - $this->github = new class extends AuthSource { + $this->github = new class extends AuthSource { public function authorize(): string { return "https://github.com/login/oauth/authorize"; @@ -60,7 +60,7 @@ public function userInfo(): string return "https://api.github.com/user/repos"; } }; - $this->weibo = new class extends AuthSource { + $this->weibo = new class extends AuthSource { public function authorize(): string { return "https://api.weibo.com/oauth2/authorize"; @@ -81,7 +81,7 @@ public function userId(): string return "https://api.weibo.com/oauth2/get_token_info?access_token="; } }; - $this->weixin = new class extends AuthSource { + $this->weixin = new class extends AuthSource { public function authorize(): string { return "https://open.weixin.qq.com/connect/qrconnect"; @@ -97,7 +97,7 @@ public function userInfo(): string return "https://api.weixin.qq.com/sns/userinfo"; } }; - $this->douyin = new class extends AuthSource { + $this->douyin = new class extends AuthSource { public function authorize(): string { return "https://open.douyin.com/platform/oauth/connect/"; @@ -113,7 +113,7 @@ public function userInfo(): string return "https://open.douyin.com/oauth/userinfo/"; } }; - $this->qq = new class extends AuthSource { + $this->qq = new class extends AuthSource { public function authorize(): string { return "https://graph.qq.com/oauth2.0/authorize"; @@ -139,7 +139,7 @@ public function getUnionid(): string return "https://graph.qq.com/oauth2.0/me?access_token="; } }; - $this->huawei = new class extends AuthSource { + $this->huawei = new class extends AuthSource { public function authorize(): string { return "https://oauth-login.cloud.huawei.com/oauth2/v2/authorize"; @@ -155,7 +155,7 @@ public function userInfo(): string return "https://api.cloud.huawei.com/rest.php?nsp_fmt=JSON&nsp_svc=huawei.oauth2.user.getTokenInfo"; } }; - $this->google = new class extends AuthSource { + $this->google = new class extends AuthSource { public function authorize(): string { return "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline"; @@ -173,10 +173,10 @@ public function userInfo(): string public function scope(): string { - return "https://www.googleapis.com/auth/userinfo.profile"; + return "https://www.googleapis.com/auth/userinfo.profile"; } }; - $this->baidu = new class extends AuthSource { + $this->baidu = new class extends AuthSource { public function authorize(): string { return "http://openapi.baidu.com/oauth/2.0/authorize"; @@ -192,7 +192,7 @@ public function userInfo(): string return "https://openapi.baidu.com/rest/2.0/passport/users/getInfo"; } }; - $this->oschina = new class extends AuthSource { + $this->oschina = new class extends AuthSource { public function authorize(): string { return "https://www.oschina.net/action/oauth2/authorize"; @@ -224,7 +224,7 @@ public function userInfo(): string return "https://api.stackexchange.com/2.2/me"; } }; - $this->dingtalk = new class extends AuthSource { + $this->dingtalk = new class extends AuthSource { public function authorize(): string { return "https://oapi.dingtalk.com/connect/qrconnect"; @@ -239,6 +239,11 @@ public function userInfo(): string { return "https://oapi.dingtalk.com/sns/getuserinfo_bycode"; } + + public function getPersistentCode(): string + { + return "https://oapi.dingtalk.com/sns/get_persistent_code"; + } }; } diff --git a/src/Request/Source/AuthDingtalkRequest.php b/src/Request/Source/AuthDingtalkRequest.php index af5e512..921b748 100644 --- a/src/Request/Source/AuthDingtalkRequest.php +++ b/src/Request/Source/AuthDingtalkRequest.php @@ -6,6 +6,7 @@ namespace JustAuth\Request\Source; +use JustAuth\Exception\AuthException; use pf\request\Request; class AuthDingtalkRequest extends AuthCommonRequest @@ -42,8 +43,7 @@ public function getAccessToken() public function getUserInfo($access_token) { $access_data = json_decode($access_token, true); - print_r($access_data); - exit(); + //$permanent_code = $this->getPersistentCode($access_data['access_token'], Request::get('code')); $time = millisecondWay(); $signature = $this->_setSignature($time); $user_info_url = $this->source_url->userInfo(); @@ -52,26 +52,25 @@ public function getUserInfo($access_token) 'signature' => $signature, 'timestamp' => $time, ]); - var_dump($this->http->request('POST', $user_info_url, [ - 'query' => $query, - 'form_params' => [ - 'tmp_auth_code' => Request::get('code') - ] - ])->getBody()->getContents()); - exit(); - return json_decode($this->http->request('POST', $user_info_url, [ - 'query' => $query, - 'body' => [ + $result = json_decode($this->http->request('POST', $user_info_url, [ + 'query' => $query, + 'headers' => ['content-type' => 'application/json'], + 'body' => json_encode([ 'tmp_auth_code' => Request::get('code') - ] - ])->getBody()->getContents()); + ]) + ])->getBody()->getContents(), true); + if ($result && isset($result['errcode'])) { + throw new AuthException($result['errcode'], $result['errmsg']); + } + return $result; } private function _setSignature($time) { $s = hash_hmac('sha256', $time, $this->config['client_secret'], true); $signature = base64_encode($s); - return urlencode($signature); + return $signature; + //return urlencode($signature); } public function getUid($access_token) @@ -81,4 +80,24 @@ public function getUid($access_token) $result = json_decode($result->getBody()->getContents(), true); return $result['uid']; } + + private function getPersistentCode($access_token, $tmp_auth_code) + { + $get_permanent_code_url = $this->source_url->getPersistentCode(); + $query = array_filter([ + 'access_token' => $access_token + ]); + try { + $result = $this->http->request('POST', $get_permanent_code_url, [ + 'query' => $query, + 'headers' => ['content-type' => 'application/json'], + 'body' => json_encode([ + 'tmp_auth_code' => $tmp_auth_code + ]) + ]); + return json_decode($result->getBody()->getContents(), true); + } catch (\Exception $e) { + return new AuthException(GET_OPENID_ERROR()); + } + } } \ No newline at end of file From c89e9f27518ec7ff2d367bac96a72603b8912030 Mon Sep 17 00:00:00 2001 From: pfinal Date: Sun, 13 Jun 2021 16:51:14 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=92=89=E9=92=89?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/oauth.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/oauth.php b/config/oauth.php index 230fbc5..5c452a9 100644 --- a/config/oauth.php +++ b/config/oauth.php @@ -60,8 +60,8 @@ 'client_key' => env('OSCHINA_SECRET', ''), ], 'dingtalk' => [ - 'client_id' => env('OSCHINA_CLIENT_ID', 'dingoaot7zvjufgpczovnh'), - 'redirect_uri' => env('OSCHINA_CALLBACK', 'http://3646x08n70.zicp.vip/test/Web/dingtalkcallback.php'), - 'client_secret' => env('OSCHINA_SECRET', 'imZeDdjjapSejLDZ3IBhW9qbSj9gjtxOGzBb_7Aq_ZlQ2eodCgM3ScQNQFGtk_d1'), + 'client_id' => env('OSCHINA_CLIENT_ID', ''), + 'redirect_uri' => env('OSCHINA_CALLBACK', ''), + 'client_secret' => env('OSCHINA_SECRET', ''), ] ]; \ No newline at end of file