1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Middleware/ApiCheck.php

114 lines
3.7 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-04-02 15:06:16 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Middleware;
use App\Models\AccountToken;
use Auth;
use Cache;
2015-04-02 15:06:16 +02:00
use Closure;
use Request;
use Response;
2017-01-30 20:40:43 +01:00
use Session;
use Utils;
2015-04-02 15:06:16 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class ApiCheck.
*/
2017-01-30 17:05:31 +01:00
class ApiCheck
{
2015-04-02 15:06:16 +02:00
/**
2017-01-30 20:40:43 +01:00
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
2015-04-02 15:06:16 +02:00
public function handle($request, Closure $next)
{
2016-10-06 14:46:27 +02:00
$loggingIn = $request->is('api/v1/login')
|| $request->is('api/v1/register')
|| $request->is('api/v1/oauth_login')
|| $request->is('api/v1/ios_subscription_status');
2015-04-02 15:06:16 +02:00
$headers = Utils::getApiHeaders();
2016-09-20 08:03:07 +02:00
$hasApiSecret = false;
2016-06-05 17:50:41 +02:00
if ($secret = env(API_SECRET)) {
2016-10-26 09:24:16 +02:00
$requestSecret = Request::header('X-Ninja-Secret') ?: ($request->api_secret ?: '');
$hasApiSecret = hash_equals($requestSecret, $secret);
2018-06-17 10:27:36 +02:00
} elseif (Utils::isSelfHost()) {
$hasApiSecret = true;
2016-06-05 17:50:41 +02:00
}
2015-04-02 15:06:16 +02:00
2015-11-02 19:43:22 +01:00
if ($loggingIn) {
2016-03-08 22:22:59 +01:00
// check API secret
2017-01-30 17:05:31 +01:00
if (! $hasApiSecret) {
2016-03-08 22:22:59 +01:00
sleep(ERROR_DELAY);
2017-01-30 20:40:43 +01:00
$error['error'] = ['message' => 'Invalid value for API_SECRET'];
return Response::json($error, 403, $headers);
2016-03-08 22:22:59 +01:00
}
2015-04-02 15:06:16 +02:00
} else {
2015-11-02 19:43:22 +01:00
// check for a valid token
$token = AccountToken::where('token', '=', Request::header('X-Ninja-Token'))->first(['id', 'user_id']);
2016-05-08 20:50:35 +02:00
// check if user is archived
if ($token && $token->user) {
2016-08-17 16:29:25 +02:00
Auth::onceUsingId($token->user_id);
2018-02-26 10:52:34 +01:00
session(['token_id' => $token->id]);
} elseif ($hasApiSecret && $request->is('api/v1/ping')) {
// do nothing: allow ping with api_secret or account token
2015-11-02 19:43:22 +01:00
} else {
2016-03-08 22:22:59 +01:00
sleep(ERROR_DELAY);
2017-01-30 20:40:43 +01:00
$error['error'] = ['message' => 'Invalid token'];
return Response::json($error, 403, $headers);
2015-11-02 19:43:22 +01:00
}
2015-04-02 15:06:16 +02:00
}
2017-01-30 20:40:43 +01:00
if (! Utils::isNinja() && ! $loggingIn) {
2015-05-13 17:32:59 +02:00
return $next($request);
2015-04-02 15:06:16 +02:00
}
2019-01-30 11:45:46 +01:00
$isMobileApp = strpos(array_get($_SERVER, 'HTTP_USER_AGENT'), '(dart:io)') !== false;
if (! Utils::hasFeature(FEATURE_API) && ! $hasApiSecret && ! $isMobileApp) {
2017-01-30 20:40:43 +01:00
$error['error'] = ['message' => 'API requires pro plan'];
return Response::json($error, 403, $headers);
2015-04-02 15:06:16 +02:00
} else {
2015-11-02 19:43:22 +01:00
$key = Auth::check() ? Auth::user()->account->id : $request->getClientIp();
2015-04-02 15:06:16 +02:00
// http://stackoverflow.com/questions/1375501/how-do-i-throttle-my-sites-api-users
$hour = 60 * 60;
2018-05-15 15:18:44 +02:00
$hour_limit = 1000;
2015-11-02 19:43:22 +01:00
$hour_throttle = Cache::get("hour_throttle:{$key}", null);
$last_api_request = Cache::get("last_api_request:{$key}", 0);
2015-04-02 15:06:16 +02:00
$last_api_diff = time() - $last_api_request;
2016-05-29 16:31:03 +02:00
2015-04-02 15:06:16 +02:00
if (is_null($hour_throttle)) {
$new_hour_throttle = 0;
} else {
$new_hour_throttle = $hour_throttle - $last_api_diff;
$new_hour_throttle = $new_hour_throttle < 0 ? 0 : $new_hour_throttle;
$new_hour_throttle += $hour / $hour_limit;
2017-01-30 17:05:31 +01:00
$hour_hits_remaining = floor(($hour - $new_hour_throttle) * $hour_limit / $hour);
2015-04-02 15:06:16 +02:00
$hour_hits_remaining = $hour_hits_remaining >= 0 ? $hour_hits_remaining : 0;
}
if ($new_hour_throttle > $hour) {
$wait = ceil($new_hour_throttle - $hour);
sleep(1);
2017-01-30 20:40:43 +01:00
2015-11-08 21:34:26 +01:00
return Response::json("Please wait {$wait} second(s)", 403, $headers);
2015-04-02 15:06:16 +02:00
}
2018-02-04 10:24:43 +01:00
Cache::put("hour_throttle:{$key}", $new_hour_throttle, 60);
Cache::put("last_api_request:{$key}", time(), 60);
2015-04-02 15:06:16 +02:00
}
return $next($request);
}
2016-05-29 16:31:03 +02:00
}