Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## [Unreleased][unreleased]

### Added
* Custom error message for token authorization

## 3.6.0 - 2026-10-27

### Security
Expand Down
8 changes: 7 additions & 1 deletion src/Authorization/TokenAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tomaj\NetteApi\Authorization;

use Tomaj\NetteApi\Exception\InvalidTokenException;
use Tomaj\NetteApi\Misc\IpDetectorInterface;
use Tomaj\NetteApi\Misc\TokenRepositoryInterface;

Expand Down Expand Up @@ -40,7 +41,12 @@ public function authorized(): bool
return false;
}

$result = $this->tokenRepository->validToken($token);
try {
$result = $this->tokenRepository->validToken($token);
} catch (InvalidTokenException $e) {
$this->errorMessage = $e->getMessage();
return false;
}
if (!$result) {
$this->errorMessage = 'Token doesn\'t exists or isn\'t active';
return false;
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Exception;

use Exception;

final class InvalidTokenException extends Exception
{

}
8 changes: 7 additions & 1 deletion src/Misc/TokenRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

namespace Tomaj\NetteApi\Misc;

use Tomaj\NetteApi\Exception\InvalidTokenException;

interface TokenRepositoryInterface
{
/**
* Return true if token is valid, otherwise return false
* Return true if token is valid, otherwise return false or throw InvalidTokenException with custom error message
*
* @param string $token
* @return bool
* @throws InvalidTokenException
*/
public function validToken(string $token): bool;

Expand Down
Loading