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) {
|
|
|
|
$token = $request->header('Authorization', $request->cookie(Authenticate::CookieName));
|
|
|
|
if ($token) {
|
2019-01-31 08:15:40 +00:00
|
|
|
return app('pd.auth')->choose($key)->getUserInfo($token);
|
2018-01-05 07:42:41 +00:00
|
|
|
}
|
2019-01-24 05:40:23 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2019-02-11 07:27:20 +00:00
|
|
|
if (!isset($config['guards'][$key])) {
|
2019-01-24 05:40:23 +00:00
|
|
|
config(['auth.guards.' . $key => ['driver' => $key]]);
|
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
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:15:40 +00:00
|
|
|
protected function setupRouter()
|
|
|
|
{
|
2019-01-23 08:12:45 +00:00
|
|
|
//添加获取token的路由
|
2019-02-14 11:07:17 +00:00
|
|
|
$this->app['router']->get('api/auth/token.json', function (Request $request) {
|
2019-01-23 08:12:45 +00:00
|
|
|
$code = $request->input('pd_code');
|
2019-01-31 08:27:56 +00:00
|
|
|
$id = $request->input('app_id');
|
|
|
|
$token = app('pd.auth')->choose(null, $id)->getAccessToken($code);
|
2019-01-24 05:40:23 +00:00
|
|
|
$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
|
|
|
});
|
|
|
|
|
2019-02-14 11:07:17 +00:00
|
|
|
$this->app['router']->get('api/auth/logout', function (Request $request) {
|
2019-01-23 08:12:45 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|