97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\AccessToken;
|
|
use App\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
//
|
|
$this->app->singleton('currentAccess', function() { return $this->getAccess(null); });
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
|
|
$this->app['auth']->viaRequest('api', function (Request $request) {
|
|
|
|
$accessToken = $this->getAccess($request);
|
|
if($accessToken == null) {
|
|
return null;
|
|
}
|
|
if(time() > strtotime($accessToken->expires_at)) {
|
|
return null;
|
|
}
|
|
|
|
if($accessToken->status =! "active") {
|
|
return null;
|
|
}
|
|
|
|
|
|
return $accessToken->getUser();
|
|
});
|
|
}
|
|
|
|
private function getAccess(?Request $request) {
|
|
$token = null;
|
|
if(isset($_SESSION["token"])) {
|
|
$token = $_SESSION["token"];
|
|
}
|
|
|
|
if(isset($_GET["access_token"])) {
|
|
$token = $_GET["access_token"];
|
|
}
|
|
|
|
if(isset($_GET["token"])) {
|
|
$token = $_GET["token"];
|
|
}
|
|
Log::debug("Request Header", getallheaders());
|
|
if(!is_null($request)) {
|
|
if($request->header("Authorization", false)) {
|
|
$token = trim($request->header("Authorization"));
|
|
$t = explode(" ", $token);
|
|
$token = last($t);
|
|
}
|
|
} else {
|
|
$headers = getallheaders();
|
|
if(isset($headers["Authorization"])) {
|
|
$token = trim($headers["Authorization"]);
|
|
$t = explode(" ", $token);
|
|
$token = last($t);
|
|
}
|
|
if(isset($headers["authorization"])) {
|
|
$token = trim($headers["authorization"]);
|
|
$t = explode(" ", $token);
|
|
$token = last($t);
|
|
}
|
|
}
|
|
|
|
|
|
if($token == null) {
|
|
return null;
|
|
}
|
|
|
|
return AccessToken::query()->where("token", "=", $token)->first();
|
|
}
|
|
}
|