config change

This commit is contained in:
候学杰 2019-01-23 16:12:45 +08:00
parent 3ad2743907
commit b5bc9c2a5b
6 changed files with 39 additions and 17 deletions

View File

@ -1,9 +1,9 @@
{
"name": "paidian/php-auth-client",
"name": "paidian/auth-client",
"authors": [
{
"name": "候学杰",
"email": "houxuejie@hawumc.com.tv"
"email": "houxuejie@hawumc.com"
}
],
"autoload": {

View File

@ -1,14 +1,19 @@
<?php
return [
'code' => [
'unauthorized' => 400401,
],
/**
* 支持的应用配置
*/
'apps' => [
'erp' => [
'id' => '100002',
'secret' => '123456',
'id' => '100009',
'alias' => 'erp',
'secret' => env('AUTH_ERP_SECRET','123456'),
],
],
];

View File

@ -56,14 +56,17 @@ class Auth
}
}
/**
* 指定 app
* @param $key
*/
public function choose($key)
public function choose($id, $referer = null)
{
$this->id = $this->config['apps'][$key]['id'];
$this->secret = $this->config['apps'][$key]['secret'];
foreach ($this->config['apps'] as $app) {
if (!$id && $referer != null) {
$arr = parse_url($referer);
} elseif ($id && $app['id'] == $id) {
$this->id = $app['id'];
$this->secret = $app['secret'];
}
}
return $this;
}
/**
@ -97,7 +100,7 @@ class Auth
*/
public function getUserInfo($token)
{
$info = $this->rpc->call('user.info',[$token]);
$info = $this->rpc->call('user.info', [$token]);
return $info;
}

View File

@ -21,7 +21,6 @@ class PdAuthServiceProvider extends ServiceProvider
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('auth', function (Request $request) {
$token = $request->header('Authorization', $request->cookie(Authenticate::CookieName));
@ -39,18 +38,33 @@ class PdAuthServiceProvider extends ServiceProvider
return null;
});
//
$config = $this->app['config']['auth'];
if (!isset($config['guards']['auth'])) {
config(['auth.guards.auth' => ['driver' => 'auth']]);
$this->app['auth']->shouldUse('auth');
}
//添加获取token的路由
$this->app['router']->get('auth/token.json', function (Request $request) {
$code = $request->input('pd_code');
$id = $request->input('app_id');
$token = app('pd.auth')->choose($id)->getAccessToken($code);
return response()->json([
'code' => 0,
'message' => '',
'data' => $token,
]);
});
$this->app['router']->get('auth/logout', function (Request $request) {
app('pd.auth')->logout();
});
}
protected function setupConfig()
{
$source = realpath(__DIR__ . '/../config/pdauth.php');
$source = realpath(__DIR__ . '/../../config/auth.php');
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
$this->publishes([$source => config_path('pdauth.php')], 'pdauth');
@ -65,7 +79,7 @@ class PdAuthServiceProvider extends ServiceProvider
{
$this->setupConfig();
$this->app->singleton('pd.auth', function () {
return new OAuth(config('pdauth'));
return new Auth(config('pdauth'));
});
}