diff --git a/app/Http/Controllers/Api/V2/ApiController.php b/app/Http/Controllers/Api/V2/ApiController.php index 009d927..ee0c622 100644 --- a/app/Http/Controllers/Api/V2/ApiController.php +++ b/app/Http/Controllers/Api/V2/ApiController.php @@ -8,6 +8,7 @@ use App\Http\Controllers\Controller; use App\Models\Service; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; class ApiController extends Controller { @@ -21,13 +22,17 @@ class ApiController extends Controller try { /** @var \App\Models\Service */ $service = Service::getFromBearerToken($request->bearerToken()); - $service->generateApiToken(); - $service->saveOrFail(); - return response()->json([ - 'api_token' => $service->api_token - ]); } catch (ModelNotFoundException $e) { throw new ApiErrorException(ApiError::authFailed()); } + if (!Hash::check($request->bearer_pwd, $service->bearer_pwd)) { + throw new ApiErrorException(ApiError::authFailed()); + } + + $service->generateApiToken(); + $service->saveOrFail(); + return response()->json([ + 'api_token' => $service->api_token + ]); } } diff --git a/app/Http/Controllers/ServiceController.php b/app/Http/Controllers/ServiceController.php index 14f5675..11a4eab 100644 --- a/app/Http/Controllers/ServiceController.php +++ b/app/Http/Controllers/ServiceController.php @@ -30,7 +30,7 @@ class ServiceController extends Controller $service->save(); return redirect()->route('services')->with('success', $edit ? 'Modifications enregistrĂ©es !' : sprintf( - 'Token du service : %s', + 'Token du service : \'%s\'', $service->bearer_token )); } @@ -41,7 +41,9 @@ class ServiceController extends Controller public function generateApiToken(Service $service) { $service->generateBearerToken(); - $service->save(); - return back()->with('success', "Nouveau token d'API de " . $service->name . " : " . $service->bearer_token); + $bearerPwd = $service->bearer_pwd; + $service->saveOrFail(); + return back() + ->with('success', "Nouveau token d'API de $service->name : '$service->bearer_token' et mot de passe '$bearerPwd'"); } } diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 7dc9bd1..a04d10d 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -31,7 +31,6 @@ class Authenticate extends Middleware $isApiV2Request = $request->isJson() && Helper::isRequestForApiV2($request); if ($isApiV2Request) { - //! Is Bearer Token needed ? // https://stackoverflow.com/questions/25838183/what-is-the-oauth-2-0-bearer-token-exactly/25843058 /** * Un token devrais ĂȘtre fourni pour chaque requete apres authentification @@ -55,7 +54,7 @@ class Authenticate extends Middleware try { $service = Service::getFromApiToken($apiToken); - $service->authenticate($guards, $apiToken); + $service->authenticateApi($guards, $apiToken); app()->bind(Service::class, function () use ($service) { return $service; }); diff --git a/app/Models/Service.php b/app/Models/Service.php index b8c740d..01b8ee2 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -6,6 +6,7 @@ use Illuminate\Auth\AuthenticationException; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; class Service extends Model @@ -20,20 +21,39 @@ class Service extends Model { static::creating(function ($service) { $service->generateBearerToken(); + if (Hash::needsRehash($service->bearer_pwd)) { + $service->bearer_pwd = Hash::make($service->bearer_pwd); + } + }); + static::updating(function ($service) { + if (Hash::needsRehash($service->bearer_pwd)) { + $service->bearer_pwd = Hash::make($service->bearer_pwd); + } }); } - public function generateApiToken() + /** + * Generates a new Api Token + * + * @return string + */ + public function generateApiToken(): string { - $this->api_token = hash('sha256', Str::random(80)); + $this->api_token = hash('sha256', Str::uuid()); $this->api_token_date = now(); return $this->api_token; } - public function generateBearerToken() + /** + * Generates new bearer credentials + * + * @return string + */ + public function generateBearerToken(): string { - $this->bearer_token = hash('sha256', Str::random(80)); + $this->bearer_token = hash('sha256', Str::uuid()); $this->bearer_token_date = now(); + $this->bearer_pwd = hash('ripemd160', Str::uuid()); return $this->bearer_token; } @@ -83,13 +103,14 @@ class Service extends Model } /** - * Authenticate from a token string + * Authenticate from the api token string * * @param array $guards * @param string $apiToken * @return void + * @throws \Illuminate\Auth\AuthenticationException */ - public function authenticate(array $guards, string $apiToken) + public function authenticateApi(array $guards, string $apiToken) { if ($this->api_token !== $apiToken) { diff --git a/database/migrations/2021_08_02_103828_add_bearer_pwd_to_services_table.php b/database/migrations/2021_08_02_103828_add_bearer_pwd_to_services_table.php new file mode 100644 index 0000000..70ff424 --- /dev/null +++ b/database/migrations/2021_08_02_103828_add_bearer_pwd_to_services_table.php @@ -0,0 +1,32 @@ +string('bearer_pwd')->after('bearer_token'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('services', function (Blueprint $table) { + $table->dropColumn('bearer_pwd'); + }); + } +} diff --git a/tests/Feature/ApiV2/ApiBase.php b/tests/Feature/ApiV2/ApiBase.php index ed0d559..3e3ec26 100644 --- a/tests/Feature/ApiV2/ApiBase.php +++ b/tests/Feature/ApiV2/ApiBase.php @@ -79,6 +79,12 @@ class ApiBase extends BaseTestCase /** @var \App\Models\Service $service */ $service = $brand->services()->firstOrFail(); + // regen bearer token to get the pwd in clear + $service->generateBearerToken(); + $bearer_pwd = $service->bearer_pwd; + $service->saveOrFail(); + $service->bearer_pwd = $bearer_pwd; + return compact('customer', 'service', 'brand'); } } diff --git a/tests/Feature/ApiV2/ApiTest.php b/tests/Feature/ApiV2/ApiTest.php index 5b286f2..0e79ad8 100644 --- a/tests/Feature/ApiV2/ApiTest.php +++ b/tests/Feature/ApiV2/ApiTest.php @@ -39,7 +39,7 @@ class ApiTest extends ApiBase $response = $this->callApi( 'post', route('apiV2.authenticate'), - [], + ['bearer_pwd' => $service->bearer_pwd], ['Authorization' => sprintf('Bearer %s', $service->bearer_token)] );