Skip to content

Commit c8a73fa

Browse files
committed
Log the error output for API
1 parent af68dbe commit c8a73fa

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

app/Http/Middleware/APISecretToken.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public function getAuthorizationMethod()
6262
public function authenticate(Request $request, Route $route)
6363
{
6464
if (!$request->bearerToken() || empty($request->bearerToken())) {
65-
APILogService::log($request);
66-
throw new UnauthorizedHttpException('The authentication header was missing or malformed');
65+
APILogService::log($request, 'The authentication header was missing or malformed.');
66+
throw new UnauthorizedHttpException('The authentication header was missing or malformed.');
6767
}
6868

6969
list($public, $hashed) = explode('.', $request->bearerToken());
7070

7171
$key = APIKey::where('public', $public)->first();
7272
if (!$key) {
73-
APILogService::log($request);
73+
APILogService::log($request, 'Invalid API Key.');
7474
throw new AccessDeniedHttpException('Invalid API Key.');
7575
}
7676

@@ -85,7 +85,7 @@ public function authenticate(Request $request, Route $route)
8585
}
8686
}
8787
if (!$inRange) {
88-
APILogService::log($request);
88+
APILogService::log($request, 'This IP address <' . $request->ip() . '> does not have permission to use this API key.');
8989
throw new AccessDeniedHttpException('This IP address <' . $request->ip() . '> does not have permission to use this API key.');
9090
}
9191
}
@@ -98,26 +98,26 @@ public function authenticate(Request $request, Route $route)
9898
}
9999

100100
if (!$this->permissionAllowed) {
101-
APILogService::log($request);
101+
APILogService::log($request, 'You do not have permission to access this resource.');
102102
throw new AccessDeniedHttpException('You do not have permission to access this resource.');
103103
}
104104
}
105105

106106
try {
107107
$decrypted = Crypt::decrypt($key->secret);
108108
} catch (\Illuminate\Contracts\Encryption\DecryptException $ex) {
109-
APILogService::log($request);
109+
APILogService::log($request, 'There was an error while attempting to check your secret key.');
110110
throw new HttpException('There was an error while attempting to check your secret key.');
111111
}
112112

113113
$this->url = urldecode($request->fullUrl());
114114
if($this->_generateHMAC($request->getContent(), $decrypted) !== base64_decode($hashed)) {
115-
APILogService::log($request);
115+
APILogService::log($request, 'The hashed body was not valid. Potential modification of contents in route.');
116116
throw new BadRequestHttpException('The hashed body was not valid. Potential modification of contents in route.');
117117
}
118118

119119
// Log the Route Access
120-
APILogService::log($request, true);
120+
APILogService::log($request, null, true);
121121
return true;
122122

123123
}

app/Services/APILogService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __constructor()
3636
//
3737
}
3838

39-
public static function log(Request $request, $authorized = false)
39+
public static function log(Request $request, $error = null, $authorized = false)
4040
{
4141
if ($request->bearerToken() && !empty($request->bearerToken())) {
4242
list($public, $hashed) = explode('.', $request->bearerToken());
@@ -47,6 +47,7 @@ public static function log(Request $request, $authorized = false)
4747
try {
4848
$log = APILog::create([
4949
'authorized' => $authorized,
50+
'error' => $error,
5051
'key' => $public,
5152
'method' => $request->method(),
5253
'route' => $request->fullUrl(),

database/migrations/2016_10_07_152117_build_api_log_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
Schema::create('api_logs', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->boolean('authorized');
19+
$table->text('error')->nullable();
1920
$table->char('key', 16)->nullable();
2021
$table->char('method', 6);
2122
$table->text('route');

0 commit comments

Comments
 (0)