39 lines
985 B
PHP
39 lines
985 B
PHP
<?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);
|
|
}
|
|
}
|