No longer need our own token exception

This commit is contained in:
Jonny Barnes 2020-11-28 19:02:37 +00:00
parent 6942fc1d32
commit f73a5587dc
5 changed files with 17 additions and 31 deletions

View file

@ -1,13 +0,0 @@
<?php
namespace App\Exceptions;
use Exception;
class InvalidTokenException extends Exception
{
public function __construct($message, $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Exceptions\InvalidTokenException;
use App\Http\Responses\MicropubResponses; use App\Http\Responses\MicropubResponses;
use App\Models\Place; use App\Models\Place;
use App\Services\Micropub\{HCardService, HEntryService, UpdateService}; use App\Services\Micropub\{HCardService, HEntryService, UpdateService};
@ -39,7 +38,6 @@ class MicropubController extends Controller
* then passes over the info to the relevant Service class. * then passes over the info to the relevant Service class.
* *
* @return JsonResponse * @return JsonResponse
* @throws InvalidTokenException
*/ */
public function post(): JsonResponse public function post(): JsonResponse
{ {

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Exceptions\InvalidTokenException;
use App\Http\Responses\MicropubResponses; use App\Http\Responses\MicropubResponses;
use App\Jobs\ProcessMedia; use App\Jobs\ProcessMedia;
use App\Models\Media; use App\Models\Media;

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Services; namespace App\Services;
use App\Exceptions\InvalidTokenException;
use App\Jobs\AddClientToDatabase; use App\Jobs\AddClientToDatabase;
use DateTimeImmutable; use DateTimeImmutable;
use Lcobucci\JWT\{Configuration, Token}; use Lcobucci\JWT\{Configuration, Token};

View file

@ -2,11 +2,12 @@
namespace Tests\Feature; namespace Tests\Feature;
use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use Tests\TestCase; use Tests\TestCase;
use Lcobucci\JWT\Builder;
use App\Services\TokenService; use App\Services\TokenService;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use App\Exceptions\InvalidTokenException;
class TokenServiceTest extends TestCase class TokenServiceTest extends TestCase
{ {
@ -36,24 +37,26 @@ class TokenServiceTest extends TestCase
public function test_token_with_different_signing_key_throws_exception() public function test_token_with_different_signing_key_throws_exception()
{ {
$this->expectException(InvalidTokenException::class); $this->expectException(RequiredConstraintsViolated::class);
$this->expectExceptionMessage('Token failed validation');
$data = [ $data = [
'me' => 'https://example.org', 'me' => 'https://example.org',
'client_id' => 'https://quill.p3k.io', 'client_id' => 'https://quill.p3k.io',
'scope' => 'post' 'scope' => 'post'
]; ];
$signer = new Sha256();
$token = (new Builder())->set('me', $data['me']) $config = resolve(Configuration::class);
->set('client_id', $data['client_id'])
->set('scope', $data['scope']) $token = $config->builder()
->set('date_issued', time()) ->issuedAt(new DateTimeImmutable())
->set('nonce', bin2hex(random_bytes(8))) ->withClaim('client_id', $data['client_id'])
->sign($signer, 'r4ndomk3y') ->withClaim('me', $data['me'])
->getToken(); ->withClaim('scope', $data['scope'])
->withClaim('nonce', bin2hex(random_bytes(8)))
->getToken($config->signer(), InMemory::plainText('r4andomk3y'))
->toString();
$service = new TokenService(); $service = new TokenService();
$token = $service->validateToken($token); $service->validateToken($token);
} }
} }