71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Lib\Helper;
|
|
use App\Models\Api\ApiToken;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The policy mappings for the application.
|
|
*
|
|
* @var array<class-string, class-string>
|
|
*/
|
|
protected $policies = [];
|
|
|
|
/**
|
|
* Register any authentication / authorization services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->registerPolicies();
|
|
|
|
Auth::viaRequest('api-hmac', function (Request $request) {
|
|
|
|
/**
|
|
* Un token devrais être fourni pour chaque requete
|
|
*/
|
|
if (is_null($request->bearer_token)) {
|
|
return null;
|
|
}
|
|
|
|
// * Si la route actuelle n'as pas de nom alors on refuse de la traiter.
|
|
if (!(Helper::getRoute($request))) {
|
|
// @codeCoverageIgnoreStart
|
|
return null;
|
|
// @codeCoverageIgnoreEnd
|
|
}
|
|
|
|
$apiToken = null;
|
|
|
|
try {
|
|
$apiToken = ApiToken::getFromBearerToken($request->bearer_token ?? '');
|
|
} catch (ModelNotFoundException $e) {
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
** All Api routes must pass ApiToken model to controller.
|
|
** This hack is a custom binder instead of passing
|
|
** this to route as a parameter.
|
|
*/
|
|
app()->bind(ApiToken::class, function () use ($apiToken) {
|
|
return $apiToken;
|
|
});
|
|
|
|
// * On authenticate la request en vérifiant sa signature.
|
|
if ($apiToken->authenticateApi($request)) {
|
|
return $apiToken;
|
|
}
|
|
|
|
return null;
|
|
});
|
|
}
|
|
}
|