php-auth-client/src/PdAuthServiceProvider.php

100 lines
3.2 KiB
PHP
Raw Normal View History

2018-01-02 03:15:51 +00:00
<?php
namespace PdAuth;
2018-01-05 07:42:41 +00:00
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
2018-01-02 03:15:51 +00:00
use Illuminate\Support\ServiceProvider;
2018-01-05 07:42:41 +00:00
use PdAuth\Middleware\Authenticate;
2019-01-24 05:40:23 +00:00
use Symfony\Component\HttpFoundation\Cookie;
2018-01-02 03:15:51 +00:00
class PdAuthServiceProvider extends ServiceProvider
{
2018-01-05 07:42:41 +00:00
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// 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.
2019-01-24 05:40:23 +00:00
$config = $this->app['config']['auth'];
foreach ($this->app['config']['pdauth']['apps'] as $key => $app) {
$this->app['auth']->viaRequest($key, function (Request $request) use ($key) {
2018-01-05 07:42:41 +00:00
2019-01-24 05:40:23 +00:00
$token = $request->header('Authorization', $request->cookie(Authenticate::CookieName));
2018-01-05 07:42:41 +00:00
2019-01-24 05:40:23 +00:00
if ($token) {
try {
$user = app('pd.auth')->choose($key)->getUserInfo($token);
if ($user) {
return $user;
}
} catch (DecryptException $ex) {
return null;
2018-01-05 07:42:41 +00:00
}
}
2019-01-24 05:40:23 +00:00
return null;
});
if (!isset($config['guards']['auth'])) {
config(['auth.guards.' . $key => ['driver' => $key]]);
$this->app['auth']->shouldUse('auth');
2018-01-05 07:42:41 +00:00
}
2019-01-24 05:40:23 +00:00
}
2018-05-02 08:37:30 +00:00
2019-01-24 05:40:23 +00:00
// $config = $this->app['config']['auth'];
//
// if (!isset($config['guards']['auth'])) {
// config(['auth.guards.auth' => ['driver' => 'auth']]);
// $this->app['auth']->shouldUse('auth');
// }
$this->setupRouter();
}
2018-05-02 02:55:13 +00:00
2019-01-24 05:40:23 +00:00
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');
2018-05-02 02:55:13 +00:00
}
2019-01-23 08:12:45 +00:00
2019-01-24 05:40:23 +00:00
$this->mergeConfigFrom($source, 'pdauth');
}
protected function setupRouter(){
2019-01-23 08:12:45 +00:00
//添加获取token的路由
$this->app['router']->get('auth/token.json', function (Request $request) {
$code = $request->input('pd_code');
2019-01-24 05:40:23 +00:00
$token = app('pd.auth')->getAccessToken($code);
$cookie = new Cookie(Authenticate::CookieName, $token['access_token'], strtotime($token['expired_at']));
2019-01-23 08:12:45 +00:00
return response()->json([
'code' => 0,
'message' => '',
'data' => $token,
2019-01-24 05:40:23 +00:00
])->withCookie($cookie);
2019-01-23 08:12:45 +00:00
});
$this->app['router']->get('auth/logout', function (Request $request) {
app('pd.auth')->logout();
});
2018-01-05 07:42:41 +00:00
}
2018-01-02 03:15:51 +00:00
public function register()
{
2018-03-19 03:53:36 +00:00
$this->setupConfig();
2018-01-02 03:15:51 +00:00
$this->app->singleton('pd.auth', function () {
2019-01-23 08:12:45 +00:00
return new Auth(config('pdauth'));
2018-01-02 03:15:51 +00:00
});
}
}