app['config']['auth']; foreach ($this->app['config']['pdauth']['apps'] as $key => $app) { $this->app['auth']->viaRequest($key, function (Request $request) use ($key) { $token = $request->header('Authorization', $request->cookie(Authenticate::CookieName)); if ($token) { try { $user = app('pd.auth')->choose($key)->getUserInfo($token); if ($user) { return $user; } } catch (DecryptException $ex) { return null; } } return null; }); if (!isset($config['guards']['auth'])) { config(['auth.guards.' . $key => ['driver' => $key]]); $this->app['auth']->shouldUse('auth'); } } // $config = $this->app['config']['auth']; // // if (!isset($config['guards']['auth'])) { // config(['auth.guards.auth' => ['driver' => 'auth']]); // $this->app['auth']->shouldUse('auth'); // } $this->setupRouter(); } protected function setupConfig() { $source = realpath(__DIR__ . '/../config/auth.php'); if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { $this->publishes([$source => config_path('pdauth.php')], 'pdauth'); } elseif ($this->app instanceof LumenApplication) { $this->app->configure('pdauth'); } $this->mergeConfigFrom($source, 'pdauth'); } protected function setupRouter(){ //添加获取token的路由 $this->app['router']->get('auth/token.json', function (Request $request) { $code = $request->input('pd_code'); $token = app('pd.auth')->getAccessToken($code); $cookie = new Cookie(Authenticate::CookieName, $token['access_token'], strtotime($token['expired_at'])); return response()->json([ 'code' => 0, 'message' => '', 'data' => $token, ])->withCookie($cookie); }); $this->app['router']->get('auth/logout', function (Request $request) { app('pd.auth')->logout(); }); } public function register() { $this->setupConfig(); $this->app->singleton('pd.auth', function () { return new Auth(config('pdauth')); }); } }