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;
|
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.
|
|
|
|
|
2018-05-02 02:55:13 +00:00
|
|
|
$this->app['auth']->viaRequest('auth', function (Request $request) {
|
2018-01-05 07:42:41 +00:00
|
|
|
|
2018-05-29 13:21:20 +00:00
|
|
|
$token = $request->header('Authorization', $request->cookie(Authenticate::CookieName));
|
2018-01-05 07:42:41 +00:00
|
|
|
|
|
|
|
if ($token) {
|
|
|
|
try {
|
|
|
|
$user = app('pd.auth')->getUserInfo($token);
|
|
|
|
if ($user) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
} catch (DecryptException $ex) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2018-05-02 08:37:30 +00:00
|
|
|
|
2018-05-02 02:55:13 +00:00
|
|
|
//
|
|
|
|
$config = $this->app['config']['auth'];
|
|
|
|
|
|
|
|
if (!isset($config['guards']['auth'])) {
|
|
|
|
config(['auth.guards.auth' => ['driver' => 'auth']]);
|
2018-05-02 08:37:30 +00:00
|
|
|
$this->app['auth']->shouldUse('auth');
|
2018-05-02 02:55:13 +00:00
|
|
|
}
|
2018-01-05 07:42:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:15:37 +00:00
|
|
|
protected function setupConfig()
|
|
|
|
{
|
2018-03-19 03:53:36 +00:00
|
|
|
$source = realpath(__DIR__ . '/../config/pdauth.php');
|
2018-03-19 03:15:37 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
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 () {
|
2018-01-05 07:42:41 +00:00
|
|
|
return new OAuth(config('pdauth'));
|
2018-01-02 03:15:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|