diff --git a/composer.json b/composer.json index eaab9b5..28e22d5 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/config/auth.php b/config/auth.php index c09e8b1..9541773 100644 --- a/config/auth.php +++ b/config/auth.php @@ -1,14 +1,19 @@ [ + 'unauthorized' => 400401, + ], + /** * 支持的应用配置 */ 'apps' => [ 'erp' => [ - 'id' => '100002', - 'secret' => '123456', + 'id' => '100009', + 'alias' => 'erp', + 'secret' => env('AUTH_ERP_SECRET','123456'), ], - ], ]; \ No newline at end of file diff --git a/src/Auth.php b/src/Auth.php index 58fe2ad..a58b00b 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -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; } diff --git a/src/PdAuth/Middleware/Authenticate.php b/src/Middleware/Authenticate.php similarity index 100% rename from src/PdAuth/Middleware/Authenticate.php rename to src/Middleware/Authenticate.php diff --git a/src/PdAuth/Middleware/CheckRole.php b/src/Middleware/CheckRole.php similarity index 100% rename from src/PdAuth/Middleware/CheckRole.php rename to src/Middleware/CheckRole.php diff --git a/src/PdAuth/PdAuthServiceProvider.php b/src/PdAuthServiceProvider.php similarity index 74% rename from src/PdAuth/PdAuthServiceProvider.php rename to src/PdAuthServiceProvider.php index 4f8c59c..aa824d9 100644 --- a/src/PdAuth/PdAuthServiceProvider.php +++ b/src/PdAuthServiceProvider.php @@ -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')); }); }