Skip to content

Commit

Permalink
增加OAuth接口
Browse files Browse the repository at this point in the history
  • Loading branch information
herojhc committed Jan 11, 2021
1 parent 5666653 commit c10a948
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"XinXiHua\\SDK\\XXHServiceProvider"
],
"aliases": {
"XXH": "XinXiHua\\SDK\\Facades\\XXH"
"XXH": "XinXiHua\\SDK\\Facades\\XXH",
"OAuth": "XinXiHua\\SDK\\Facades\\OAuth"
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion config/xxh-sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@

'agent' => [
'agent_id' => env('AUTH_AGENT_ID', ''),
'platform_url' => env('AUTH_PALTFORM_URL', 'https://oa.xinxihua.com'),
'platform_url' => env('AUTH_PLATFORM_URL', 'https://oa.xinxihua.com'),
'gateway_url' => env('AUTH_GATEWAY_URL', 'https://oa.xinxihua.com/sso-login'),
'oauth_url' => env('AUTH_OAUTH_URL', 'https://oa.xinxihua.com/oauth2/wechat/authorize/redirect'),
'corp_user_api' => 'auth_user',
'corp_oauth_user_api' => 'oauth_user',
'corp_token_api' => 'corp_tokens',
'corp_info' => 'auth_corp',
'token' => env('AUTH_TOKEN', ''),
Expand Down
84 changes: 84 additions & 0 deletions src/XinXiHua/SDK/Auth/OauthManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Created by PhpStorm.
* User: JHC
* Date: 2021-01-09
* Time: 18:36
*/

namespace XinXiHua\SDK\Auth;

use Illuminate\Contracts\Session\Session;
use XinXiHua\SDK\Models\OauthUser;


class OauthManager
{
/**
* @var OauthUser|null
*/
protected $oauth = null;
protected $session;


/**
* OauthManager constructor.
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function __construct($app)
{
$this->session = $app->make(Session::class);
}

/**
* @return bool
*/
public function check()
{
return !is_null($this->oauth());
}

/**
* @return OauthUser|null
*/
public function oauth()
{

if (!is_null($this->oauth)) {
return $this->oauth;
}

if ($this->session->isStarted()) {
$oauth = json_decode($this->session->get($this->getName()), true);
$this->setOauth($oauth);
}

return $this->oauth;
}

public function id()
{
return $this->oauth() ? $this->oauth->oauthId : null;
}

protected function getName()
{
return 'login_oauth_' . sha1(static::class);
}

public function setOauth($oauth)
{
$oauthUser = new OauthUser();
$oauthUser->oauthId = $oauth['open_id'];
$oauthUser->oauthType = $oauth['oauth_type'] ?? 'wechat';
$this->oauth = $oauthUser;
}

public function login($oauth)
{
$this->session->put($this->getName(), json_encode($oauth, JSON_UNESCAPED_UNICODE));
$this->setOauth($oauth);
return $this->oauth;

}
}
34 changes: 34 additions & 0 deletions src/XinXiHua/SDK/Facades/OAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: JHC
* Date: 2021-01-11
* Time: 11:36
*/

namespace XinXiHua\SDK\Facades;


use Illuminate\Support\Facades\Facade;

/**
* @method static bool check()
* @method static \XinXiHua\SDK\Models\OauthUser|null oauth()
* @method static string|null id()
* @method static void login(array $oauth)
*
* @see \XinXiHua\SDK\Auth\OauthManager
*/
class OAuth extends Facade
{

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'oauth';
}
}
11 changes: 3 additions & 8 deletions src/XinXiHua/SDK/Facades/XXH.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ protected static function getFacadeAccessor()
*/
public static function routes()
{
Route::get('login', '\XinXiHua\SDK\Http\Controllers\LoginController@login')->name('login');
Route::post('logout', '\XinXiHua\SDK\Http\Controllers\LoginController@logout')->name('logout');
Route::get('callback', '\XinXiHua\SDK\Http\Controllers\LoginController@callback')->name('callback');
Route::get('admin/login', '\XinXiHua\SDK\Http\Controllers\Admin\LoginController@login')->name('admin.login');
Route::post('admin/logout', '\XinXiHua\SDK\Http\Controllers\Admin\LoginController@logout')->name('admin.logout');
Route::get('admin/callback', '\XinXiHua\SDK\Http\Controllers\Admin\LoginController@callback')->name('admin.callback');
Route::any('serve', '\XinXiHua\SDK\Http\Controllers\ServeController@serve')->name('serve');

self::homeRoutes();
self::adminRoutes();
}

/**
Expand All @@ -74,6 +68,7 @@ public static function homeRoutes()
Route::post('logout', '\XinXiHua\SDK\Http\Controllers\LoginController@logout')->name('logout');
Route::get('callback', '\XinXiHua\SDK\Http\Controllers\LoginController@callback')->name('callback');
Route::any('serve', '\XinXiHua\SDK\Http\Controllers\ServeController@serve')->name('serve');
Route::get('oauth', '\XinXiHua\SDK\Http\Controllers\LoginController@oauth')->name('oauth');

}

Expand Down
35 changes: 34 additions & 1 deletion src/XinXiHua/SDK/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use XinXiHua\SDK\Facades\OAuth;
use XinXiHua\SDK\Facades\XXH;
use XinXiHua\SDK\Services\AuthService as Service;

Expand Down Expand Up @@ -97,7 +98,6 @@ public function login(Request $request)
return redirect($gatewayUri);
}


public function callback(Request $request)
{

Expand Down Expand Up @@ -137,4 +137,37 @@ public function callback(Request $request)
return '登陆失败或授权码已过期';
}

public function oauth(Request $request)
{
$authCode = $request->get('auth_code');
$corpId = $request->get('corp_id');

if (!$authCode || !$corpId) {
return '参数错误';
}

try {

$oauth = $this->service->getOauthUser($corpId, $authCode);
if (!isset($oauth['open_id']) || empty($oauth['open_id'])) {
return '获取授权信息失败';
}

// 执行登录
OAuth::login($oauth);

// 定义跳转url
if (!empty($request->get('service', ''))) {
$request->session()->put('url.intended', base64_decode($request->get('service', '')));
}
return redirect()->intended($this->redirectPath());

} catch (\Exception $exception) {
Artisan::call('cache:clear');
Log::error($exception->getMessage(), ['exception' => $exception]);
}

return '授权失败或授权码已过期';
}

}
17 changes: 17 additions & 0 deletions src/XinXiHua/SDK/Models/OauthUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: JHC
* Date: 2021-01-09
* Time: 18:58
*/

namespace XinXiHua\SDK\Models;


class OauthUser
{

public $oauthId;
public $oauthType;
}
16 changes: 16 additions & 0 deletions src/XinXiHua/SDK/Services/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,20 @@ public function setUserInfo()
return null;

}

public function getOauthUser($corpId, $authCode)
{
$isvCorpClient = $this->getIsvCorpClient($corpId);
$response = $isvCorpClient->get(config('xxh-sdk.agent.corp_oauth_user_api'),
[
'auth_code' => $authCode
]
);
Log::debug($response->getResponse());
if ($response->isResponseSuccess()) {
$result = $response->getResponseData();
return $result['data'] ?? [];
}
return [];
}
}
11 changes: 8 additions & 3 deletions src/XinXiHua/SDK/Support/Sign/MakeSign.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public function sign($key, $params)
$params = $this->decodeParams($params);
// 字典排序
ksort($params);
// 生成查询字符串
$body = http_build_query($params);
return $this->generateResponseBodySignature($key, $body);
$unsignedData = "";
foreach ($params as $paramKey => $paramValue) {
if (!empty($paramValue)) {
$unsignedData .= $paramKey . '=' . trim($paramValue) . '&';
}
}
// 生成签名
return $this->generateResponseBodySignature($key, rtrim($unsignedData, '&'));
} catch (\Exception $exception) {
}
return false;
Expand Down
8 changes: 7 additions & 1 deletion src/XinXiHua/SDK/XXHServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace XinXiHua\SDK;

use Illuminate\Support\ServiceProvider;
use XinXiHua\SDK\Auth\OauthManager;
use XinXiHua\SDK\Auth\XXHManager;

class XXHServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -58,10 +59,15 @@ public function register()
'xxh-sdk'
);

// 启动信息化
// 注册信息化
$this->app->singleton('xxh', function () {
return new XXHManager($this->app);
});

// 注册 oauth
$this->app->singleton('oauth', function () {
return new OauthManager($this->app);
});

}
}

0 comments on commit c10a948

Please sign in to comment.