This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\Answer;
|
|
use App\Models\Brand;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerInfo;
|
|
use App\Models\Question;
|
|
use App\Models\Service;
|
|
use App\Models\SingleOptin;
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class RouteServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The path to the "home" route for your application.
|
|
*
|
|
* This is used by Laravel authentication to redirect users after login.
|
|
*
|
|
* @var string
|
|
*/
|
|
public const HOME = '/customers';
|
|
|
|
/**
|
|
* Define your route model bindings, pattern filters, etc.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->configureRateLimiting();
|
|
|
|
$this->routes(function () {
|
|
// @codeCoverageIgnoreStart
|
|
Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
|
|
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
|
|
// @codeCoverageIgnoreEnd
|
|
});
|
|
|
|
Route::model('brand', Brand::class);
|
|
Route::model('service', Service::class);
|
|
Route::model('single_optin', SingleOptin::class);
|
|
Route::model('question', Question::class);
|
|
Route::model('answer', Answer::class);
|
|
Route::model('customer', Customer::class);
|
|
|
|
// * Api parameters Bind.
|
|
Route::model('serviceQuestion', Question::class);
|
|
Route::bind('serviceCustomer', function ($value) {
|
|
// * Load brand
|
|
/** @var \App\Models\Brand $brand */
|
|
$brand = app(Service::class)->brand;
|
|
|
|
// ? Parameter is email
|
|
if (strpos($value, '@') !== false) {
|
|
$customer = Customer::whereHas('brands', function ($q) use ($brand) {
|
|
$q->where('brands.id', '=', $brand->id);
|
|
})->where('email', '=', $value)->firstOrFail();
|
|
}
|
|
|
|
try {
|
|
// ? Have we found a customer yet
|
|
if (!isset($customer) or \get_class($customer) !== Customer::class) {
|
|
// ? Parameter is hashcode
|
|
$customer = CustomerInfo::whereHas('brand', function ($q) use ($brand) {
|
|
$q->where('brands.id', '=', $brand->id);
|
|
})->where('hashcode', $value)
|
|
->with('customer')->firstOrFail()->customer;
|
|
}
|
|
|
|
// * Load Customer Login and Info for this brand.
|
|
/** @var \App\Models\Customer $customer */
|
|
|
|
return $customer->loadBrandData($brand);
|
|
} catch (ModelNotFoundException $e) {
|
|
// * Uniform Not found exception using model Customer.
|
|
throw (new ModelNotFoundException())->setModel(Customer::class);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Configure the rate limiters for the application.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function configureRateLimiting()
|
|
{
|
|
RateLimiter::for('web', function (Request $request) {
|
|
// @codeCoverageIgnoreStart
|
|
return Limit::perMinute(25)
|
|
->by(optional($request->user())->id ?: app('clientIp'));
|
|
// @codeCoverageIgnoreEnd
|
|
});
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
RateLimiter::for('api', function (Request $request) {
|
|
return Limit::perMinute(config('api.apiRequestRatePerMinute', 100))
|
|
->by(app('clientIp'));
|
|
});
|
|
}
|
|
}
|