This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Providers/AuthServiceProvider.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2019-04-24 18:46:41 +00:00
<?php
namespace App\Providers;
use App\Models\AccessToken;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
2019-07-17 12:52:22 +00:00
use Illuminate\Support\Facades\Log;
2019-04-24 18:46:41 +00:00
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
2019-06-20 14:46:50 +00:00
$this->app->singleton('currentAccess', function() { return $this->getAccess(null); });
2019-04-24 18:46: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.
$this->app['auth']->viaRequest('api', function (Request $request) {
2019-06-20 14:46:50 +00:00
$accessToken = $this->getAccess($request);
if($accessToken == null) {
return null;
2019-04-24 18:46:41 +00:00
}
2019-06-20 14:46:50 +00:00
if(time() > strtotime($accessToken->expires_at)) {
return null;
2019-04-24 18:46:41 +00:00
}
2019-06-20 14:46:50 +00:00
if($accessToken->status =! "active") {
return null;
2019-04-24 18:46:41 +00:00
}
2019-06-20 14:46:50 +00:00
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"];
}
2019-04-24 18:46:41 +00:00
2019-06-20 14:46:50 +00:00
if(isset($_GET["token"])) {
$token = $_GET["token"];
}
2019-07-17 12:52:22 +00:00
Log::debug("Request Header", getallheaders());
2019-06-20 14:46:50 +00:00
if(!is_null($request)) {
2019-04-24 18:46:41 +00:00
if($request->header("Authorization", false)) {
$token = trim($request->header("Authorization"));
$t = explode(" ", $token);
$token = last($t);
}
2019-06-20 14:46:50 +00:00
} else {
$headers = getallheaders();
2019-07-17 12:52:22 +00:00
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);
}
2019-06-20 14:46:50 +00:00
}
2019-04-24 18:46:41 +00:00
2019-06-20 14:46:50 +00:00
if($token == null) {
return null;
}
2019-04-24 18:46:41 +00:00
2019-06-20 14:46:50 +00:00
return AccessToken::query()->where("token", "=", $token)->first();
2019-04-24 18:46:41 +00:00
}
}