No longer need our own token exception
This commit is contained in:
parent
6942fc1d32
commit
f73a5587dc
5 changed files with 17 additions and 31 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Exceptions\InvalidTokenException;
|
||||
use App\Http\Responses\MicropubResponses;
|
||||
use App\Models\Place;
|
||||
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.
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws InvalidTokenException
|
||||
*/
|
||||
public function post(): JsonResponse
|
||||
{
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Exceptions\InvalidTokenException;
|
||||
use App\Http\Responses\MicropubResponses;
|
||||
use App\Jobs\ProcessMedia;
|
||||
use App\Models\Media;
|
||||
|
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\InvalidTokenException;
|
||||
use App\Jobs\AddClientToDatabase;
|
||||
use DateTimeImmutable;
|
||||
use Lcobucci\JWT\{Configuration, Token};
|
||||
|
|
|
@ -2,11 +2,12 @@
|
|||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Lcobucci\JWT\Configuration;
|
||||
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
|
||||
use Tests\TestCase;
|
||||
use Lcobucci\JWT\Builder;
|
||||
use App\Services\TokenService;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||
use App\Exceptions\InvalidTokenException;
|
||||
|
||||
class TokenServiceTest extends TestCase
|
||||
{
|
||||
|
@ -36,24 +37,26 @@ class TokenServiceTest extends TestCase
|
|||
|
||||
public function test_token_with_different_signing_key_throws_exception()
|
||||
{
|
||||
$this->expectException(InvalidTokenException::class);
|
||||
$this->expectExceptionMessage('Token failed validation');
|
||||
$this->expectException(RequiredConstraintsViolated::class);
|
||||
|
||||
$data = [
|
||||
'me' => 'https://example.org',
|
||||
'client_id' => 'https://quill.p3k.io',
|
||||
'scope' => 'post'
|
||||
];
|
||||
$signer = new Sha256();
|
||||
$token = (new Builder())->set('me', $data['me'])
|
||||
->set('client_id', $data['client_id'])
|
||||
->set('scope', $data['scope'])
|
||||
->set('date_issued', time())
|
||||
->set('nonce', bin2hex(random_bytes(8)))
|
||||
->sign($signer, 'r4ndomk3y')
|
||||
->getToken();
|
||||
|
||||
$config = resolve(Configuration::class);
|
||||
|
||||
$token = $config->builder()
|
||||
->issuedAt(new DateTimeImmutable())
|
||||
->withClaim('client_id', $data['client_id'])
|
||||
->withClaim('me', $data['me'])
|
||||
->withClaim('scope', $data['scope'])
|
||||
->withClaim('nonce', bin2hex(random_bytes(8)))
|
||||
->getToken($config->signer(), InMemory::plainText('r4andomk3y'))
|
||||
->toString();
|
||||
|
||||
$service = new TokenService();
|
||||
$token = $service->validateToken($token);
|
||||
$service->validateToken($token);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue