Add middleware for Bearer authorization
This commit is contained in:
parent
fbe2831551
commit
e7cb1d970b
38
app/Http/Middleware/ApiAuthorization.php
Normal file
38
app/Http/Middleware/ApiAuthorization.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use App\AuthToken;
|
||||||
|
use Closure;
|
||||||
|
|
||||||
|
class ApiAuthorization
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle($request, Closure $next)
|
||||||
|
{
|
||||||
|
// During debug mode disable this check
|
||||||
|
if (config('chaosapi.middleware-enabled')) return $next($request);
|
||||||
|
|
||||||
|
// Get the Authorization header
|
||||||
|
$authHeader = $request->header('Authorization');
|
||||||
|
|
||||||
|
// Check if header present or header malformed
|
||||||
|
if ($authHeader === null) return abort(403);
|
||||||
|
if (!str_starts_with($authHeader, 'Bearer ')) return abort(400);
|
||||||
|
|
||||||
|
$authToken = substr($authHeader, 7);
|
||||||
|
$dbAuthToken = AuthToken::where('token', $authToken)->first();
|
||||||
|
|
||||||
|
// Check token in DB
|
||||||
|
if ($dbAuthToken === null) return abort(401);
|
||||||
|
|
||||||
|
// Valid request
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'middleware-enabled' => env('APP_DEBUG', false),
|
||||||
|
|
||||||
'cachetime' => 60
|
'cachetime' => 60
|
||||||
];
|
];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user