2017-12-12 12:05:22 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handle the authentication for the API layer
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Contracts\Middleware;
|
2021-03-09 00:24:09 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\User;
|
2017-12-12 12:05:22 +08:00
|
|
|
use Closure;
|
2020-02-09 02:29:34 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-12-12 12:05:22 +08:00
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
class ApiAuth implements Middleware
|
2017-12-12 12:05:22 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
2017-12-12 12:05:22 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-02-09 02:29:34 +08:00
|
|
|
public function handle(Request $request, Closure $next)
|
2017-12-12 12:05:22 +08:00
|
|
|
{
|
|
|
|
// Check if Authorization header is in place
|
2017-12-31 03:31:11 +08:00
|
|
|
$api_key = $request->header('x-api-key', null);
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($api_key === null) {
|
2017-12-31 03:31:11 +08:00
|
|
|
$api_key = $request->header('Authorization', null);
|
|
|
|
if ($api_key === null) {
|
|
|
|
return $this->unauthorized('X-API-KEY header missing');
|
|
|
|
}
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the user via API key. Cache this lookup
|
2017-12-13 06:58:27 +08:00
|
|
|
$user = User::where('api_key', $api_key)->first();
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($user === null) {
|
2017-12-31 01:58:34 +08:00
|
|
|
return $this->unauthorized('User not found with key "'.$api_key.'"');
|
2017-12-12 12:05:22 +08:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:24:09 +08:00
|
|
|
if ($user->state !== UserState::ACTIVE && $user->state !== UserState::ON_LEAVE) {
|
|
|
|
return $this->unauthorized('User is not ACTIVE, please contact an administrator');
|
|
|
|
}
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
// Set the user to the request
|
|
|
|
Auth::setUser($user);
|
2018-01-03 04:37:52 +08:00
|
|
|
$request->merge(['user' => $user]);
|
2017-12-12 12:05:22 +08:00
|
|
|
$request->setUserResolver(function () use ($user) {
|
|
|
|
return $user;
|
|
|
|
});
|
|
|
|
|
2021-06-09 23:20:25 +08:00
|
|
|
// Force english locale for API
|
|
|
|
app()->setLocale('en');
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an unauthorized message
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param mixed $details
|
|
|
|
*
|
2017-12-12 12:05:22 +08:00
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
private function unauthorized($details = '')
|
2017-12-12 12:05:22 +08:00
|
|
|
{
|
|
|
|
return response([
|
|
|
|
'error' => [
|
2018-03-20 09:50:40 +08:00
|
|
|
'code' => '401',
|
2017-12-12 12:05:22 +08:00
|
|
|
'http_code' => 'Unauthorized',
|
2018-03-20 09:50:40 +08:00
|
|
|
'message' => 'Invalid or missing API key ('.$details.')',
|
2017-12-12 12:05:22 +08:00
|
|
|
],
|
|
|
|
], 401);
|
|
|
|
}
|
|
|
|
}
|