Add bearer_pwd to api auth

This commit is contained in:
Jérémy Munsch 2021-08-02 11:46:08 +02:00
parent 19e1a022a2
commit 39beb54b19
7 changed files with 82 additions and 17 deletions

View file

@ -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
]);
}
}

View file

@ -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'");
}
}

View file

@ -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;
});

View file

@ -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) {

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddBearerPwdToServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('services', function (Blueprint $table) {
$table->string('bearer_pwd')->after('bearer_token');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('services', function (Blueprint $table) {
$table->dropColumn('bearer_pwd');
});
}
}

View file

@ -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');
}
}

View file

@ -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)]
);