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
9 changes: 9 additions & 0 deletions src/Exception/InvalidAlgException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Webdevcave\Jwt\Exception;

use Exception;

class InvalidAlgException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Webdevcave\Jwt\Exception;

use Exception;

class InvalidTokenException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/TokenNotPresentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Webdevcave\Jwt\Exception;

use Exception;

class TokenNotPresentException extends Exception
{
}
6 changes: 3 additions & 3 deletions src/SignerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webdevcave\Jwt;

use Exception;
use Webdevcave\Jwt\Exception\InvalidAlgException;
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
use Webdevcave\Jwt\Signer\Hs\Hs384Signer;
use Webdevcave\Jwt\Signer\Hs\Hs512Signer;
Expand Down Expand Up @@ -39,14 +39,14 @@ public static function assign(string $algorithm, string $className): void
/**
* @param string $algorithm
*
* @throws Exception
* @throws InvalidAlgException
*
* @return Signer
*/
public static function build(string $algorithm): Signer
{
if (!isset(self::$algorithmMap[$algorithm])) {
throw new Exception("Algorithm not assigned: $algorithm. See SignerFactory::assign()");
throw new InvalidAlgException("Algorithm not assigned: $algorithm. See SignerFactory::assign()");
}

$className = self::$algorithmMap[$algorithm];
Expand Down
31 changes: 20 additions & 11 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Webdevcave\Jwt;

use Exception;
use Webdevcave\Jwt\Exception\InvalidAlgException;
use Webdevcave\Jwt\Exception\InvalidTokenException;
use Webdevcave\Jwt\Exception\TokenNotPresentException;
use Webdevcave\Jwt\Secrets\Secret;
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
use Webdevcave\Jwt\Signer\Signer;
Expand Down Expand Up @@ -87,7 +89,9 @@ private static function getDefaultSigner(): Signer
}

/**
* @throws Exception
* @throws InvalidAlgException
* @throws InvalidTokenException
* @throws TokenNotPresentException
*
* @return Token
*/
Expand Down Expand Up @@ -117,12 +121,12 @@ public static function fromAuthorizationBearer(): Token
}

if (!$authorizationHeader) {
throw new Exception('Authorization header is not set');
throw new TokenNotPresentException('Authorization header is not set');
}

$matches = [];
if (!preg_match('/Bearer\s((.*)\.(.*)\.(.*))/', $authorizationHeader, $matches)) {
throw new Exception('Invalid "Authorization" header value');
throw new InvalidTokenException('Invalid "Authorization" header value');
}

return static::fromString($matches[1]);
Expand All @@ -131,7 +135,8 @@ public static function fromAuthorizationBearer(): Token
/**
* @param string $token
*
* @throws Exception
* @throws InvalidAlgException
* @throws InvalidTokenException
*
* @return Token
*/
Expand All @@ -140,15 +145,15 @@ public static function fromString(string $token): Token
$parts = explode('.', $token);

if (count($parts) != 3) {
throw new Exception('Invalid token: JWT token must have 3 sections separated by "."');
throw new InvalidTokenException('Invalid token: JWT token must have 3 sections separated by "."');
}

$headers = json_decode(self::decodeSection($parts[0]), true) ?: [];
$payload = json_decode(self::decodeSection($parts[1]), true) ?: [];
$signature = self::decodeSection($parts[2]) ?: '';

if (empty($headers['alg'])) {
throw new Exception('"alg" JWT header must not be empty');
throw new InvalidTokenException('"alg" JWT header must not be empty');
}

return static::create()
Expand Down Expand Up @@ -214,15 +219,19 @@ public static function create(): Token
}

/**
* @param $param
* @param string $param
*
* @throws Exception
* @throws TokenNotPresentException
*
* @return Token
*/
public static function fromQueryString($param = 'token'): Token
public static function fromQueryString(string $param = 'token'): Token
{
$token = isset($_GET[$param]) ? $_GET[$param] : null;
$token = $_GET[$param] ?? null;

if (is_null($token)) {
throw new TokenNotPresentException('Token parameter is not set');
}

return static::fromString($token);
}
Expand Down
Loading