Skip to content

Commit

Permalink
Add Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonis committed Jun 20, 2019
1 parent 04e1bdf commit 2b9ec10
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/ApiKeyGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Http\Request;
use Leonis\ApiKeyAuth\Exceptions\ApiKeyAuthException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class ApiKeyGuard implements Guard
Expand All @@ -31,7 +32,7 @@ class ApiKeyGuard implements Guard
public function __construct(UserProvider $provider, Request $request)
{
$this->provider = $provider;
$this->request = $request;
$this->request = $request;
}

/**
Expand All @@ -47,30 +48,31 @@ public function user()

$apiKey = $this->getApiKeyInstance();
if (!$apiKey) {
return response()->json([
'message' => 'The api key is not exist.',
], 400);
throw new ApiKeyAuthException('The api key is not exist.', 400);
}

$payloads = $this->getPayloads();

$signature = $this->getSignature();

if (!$this->checkSignature($payloads, $apiKey->secret, $signature)) {
return response()->json([
'message' => 'The signature is invalid.',
], 401);
throw new ApiKeyAuthException('The signature is invalid.', 401);
}

$this->user = $this->provider->retrieveById($apiKey->user_id);

if (!$this->user) {
throw new ApiKeyAuthException('The api key is not exist.', 400);
}

return $this->user;
}

/**
* Validate a user's credentials.
*
* @param array $credentials
*
* @return bool
*/
public function validate(array $credentials = [])
Expand Down Expand Up @@ -128,6 +130,7 @@ public function getSignature()
* @param string $payloads
* @param string $secret
* @param string $signature
*
* @return bool
*/
public function checkSignature($payloads, $secret, $signature)
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/ApiKeyAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Leonis\ApiKeyAuth\Exceptions;

use Exception;

class ApiKeyAuthException extends Exception
{

}
14 changes: 6 additions & 8 deletions src/Middleware/TimeDeviationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Closure;
use Illuminate\Support\Facades\Validator;
use Leonis\ApiKeyAuth\Exceptions\ApiKeyAuthException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class TimeDeviationMiddleware
Expand All @@ -21,24 +22,21 @@ class TimeDeviationMiddleware
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$timestamp = $request->input('timestamp');
$timestamp = $request->input('timestamp');
$timeoffset = $request->input('timeoffset');

if ($timestamp === null || $timeoffset === null) {
return response()->json([
'message' => 'Timestamp and timeoffset is required.',
], 400);
throw new ApiKeyAuthException('Timestamp and timeoffset is required.', 400);
}

if (abs(time() - $timestamp) > $timeoffset) {
return response()->json([
'message' => 'Request has expired.',
], 400);
throw new ApiKeyAuthException('Request has expired.', 400);
}

return $next($request);
Expand Down

0 comments on commit 2b9ec10

Please sign in to comment.