forked from cydrobolt/polr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ApiException to handle API errors and use ApiMiddleware to handle…
… API authentication
- Loading branch information
Showing
9 changed files
with
152 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace App\Exceptions\Api; | ||
|
||
class ApiException extends \Exception { | ||
/** | ||
* Catch an API exception. | ||
* | ||
* @param string $text_code | ||
* @param string $message | ||
* @param integer $status_code | ||
* @param string $response_type | ||
* @param \Exception $previous | ||
* | ||
* @return mixed | ||
*/ | ||
public function __construct($text_code='SERVER_ERROR', $message, $status_code = 0, $response_type='plain_text', Exception $previous = null) { | ||
// TODO special Polr error codes for JSON | ||
|
||
$this->response_type = $response_type; | ||
$this->text_code = $text_code; | ||
parent::__construct($message, $status_code, $previous); | ||
} | ||
|
||
private function encodeJsonResponse($status_code, $message, $text_code) { | ||
$response = [ | ||
'status_code' => $status_code, | ||
'error_code' => $text_code, | ||
'error' => $message | ||
]; | ||
|
||
return json_encode($response); | ||
} | ||
|
||
public function getEncodedErrorMessage() { | ||
if ($this->response_type == 'json') { | ||
return $this->encodeJsonResponse($this->code, $this->message, $this->text_code); | ||
} | ||
else { | ||
return $this->code . ' ' . $this->message; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use App\Models\User; | ||
use App\Helpers\ApiHelper; | ||
use App\Exceptions\Api\ApiException; | ||
|
||
class ApiMiddleware { | ||
protected static function getApiUserInfo(Request $request) { | ||
$api_key = $request->input('key'); | ||
$response_type = $request->input('response_type'); | ||
|
||
if (!$api_key) { | ||
// no API key provided; check whether anonymous API is enabled | ||
|
||
if (env('SETTING_ANON_API')) { | ||
$username = 'ANONIP-' . $request->ip(); | ||
} | ||
else { | ||
throw new ApiException('AUTH_ERROR', 'Authentication token required.', 401, $response_type); | ||
} | ||
$user = (object) [ | ||
'username' => $username | ||
]; | ||
} | ||
else { | ||
$user = User::where('active', 1) | ||
->where('api_key', $api_key) | ||
->where('api_active', 1) | ||
->first(); | ||
|
||
if (!$user) { | ||
abort(401, "Invalid authentication token."); | ||
} | ||
$username = $user->username; | ||
} | ||
|
||
$api_limit_reached = ApiHelper::checkUserApiQuota($username); | ||
|
||
if ($api_limit_reached) { | ||
throw new ApiException('QUOTA_EXCEEDED', 'Quota exceeded.', 429, $response_type); | ||
} | ||
return $user; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
|
||
public function handle($request, Closure $next) { | ||
$request->user = $this->getApiUserInfo($request); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters