Skip to content

Commit 0a8449f

Browse files
Merge pull request #3 from WebdevCave/feature/exceptions
Replacing generic exceptions
2 parents 0e1ebb3 + 87bad7e commit 0a8449f

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Webdevcave\Jwt\Exception;
4+
5+
use Exception;
6+
7+
class InvalidAlgException extends Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Webdevcave\Jwt\Exception;
4+
5+
use Exception;
6+
7+
class InvalidTokenException extends Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Webdevcave\Jwt\Exception;
4+
5+
use Exception;
6+
7+
class TokenNotPresentException extends Exception
8+
{
9+
}

src/SignerFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Webdevcave\Jwt;
44

5-
use Exception;
5+
use Webdevcave\Jwt\Exception\InvalidAlgException;
66
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
77
use Webdevcave\Jwt\Signer\Hs\Hs384Signer;
88
use Webdevcave\Jwt\Signer\Hs\Hs512Signer;
@@ -39,14 +39,14 @@ public static function assign(string $algorithm, string $className): void
3939
/**
4040
* @param string $algorithm
4141
*
42-
* @throws Exception
42+
* @throws InvalidAlgException
4343
*
4444
* @return Signer
4545
*/
4646
public static function build(string $algorithm): Signer
4747
{
4848
if (!isset(self::$algorithmMap[$algorithm])) {
49-
throw new Exception("Algorithm not assigned: $algorithm. See SignerFactory::assign()");
49+
throw new InvalidAlgException("Algorithm not assigned: $algorithm. See SignerFactory::assign()");
5050
}
5151

5252
$className = self::$algorithmMap[$algorithm];

src/Token.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Webdevcave\Jwt;
44

5-
use Exception;
5+
use Webdevcave\Jwt\Exception\InvalidAlgException;
6+
use Webdevcave\Jwt\Exception\InvalidTokenException;
7+
use Webdevcave\Jwt\Exception\TokenNotPresentException;
68
use Webdevcave\Jwt\Secrets\Secret;
79
use Webdevcave\Jwt\Signer\Hs\Hs256Signer;
810
use Webdevcave\Jwt\Signer\Signer;
@@ -87,7 +89,9 @@ private static function getDefaultSigner(): Signer
8789
}
8890

8991
/**
90-
* @throws Exception
92+
* @throws InvalidAlgException
93+
* @throws InvalidTokenException
94+
* @throws TokenNotPresentException
9195
*
9296
* @return Token
9397
*/
@@ -117,12 +121,12 @@ public static function fromAuthorizationBearer(): Token
117121
}
118122

119123
if (!$authorizationHeader) {
120-
throw new Exception('Authorization header is not set');
124+
throw new TokenNotPresentException('Authorization header is not set');
121125
}
122126

123127
$matches = [];
124128
if (!preg_match('/Bearer\s((.*)\.(.*)\.(.*))/', $authorizationHeader, $matches)) {
125-
throw new Exception('Invalid "Authorization" header value');
129+
throw new InvalidTokenException('Invalid "Authorization" header value');
126130
}
127131

128132
return static::fromString($matches[1]);
@@ -131,7 +135,8 @@ public static function fromAuthorizationBearer(): Token
131135
/**
132136
* @param string $token
133137
*
134-
* @throws Exception
138+
* @throws InvalidAlgException
139+
* @throws InvalidTokenException
135140
*
136141
* @return Token
137142
*/
@@ -140,15 +145,15 @@ public static function fromString(string $token): Token
140145
$parts = explode('.', $token);
141146

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

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

150155
if (empty($headers['alg'])) {
151-
throw new Exception('"alg" JWT header must not be empty');
156+
throw new InvalidTokenException('"alg" JWT header must not be empty');
152157
}
153158

154159
return static::create()
@@ -214,15 +219,19 @@ public static function create(): Token
214219
}
215220

216221
/**
217-
* @param $param
222+
* @param string $param
218223
*
219-
* @throws Exception
224+
* @throws TokenNotPresentException
220225
*
221226
* @return Token
222227
*/
223-
public static function fromQueryString($param = 'token'): Token
228+
public static function fromQueryString(string $param = 'token'): Token
224229
{
225-
$token = isset($_GET[$param]) ? $_GET[$param] : null;
230+
$token = $_GET[$param] ?? null;
231+
232+
if (is_null($token)) {
233+
throw new TokenNotPresentException('Token parameter is not set');
234+
}
226235

227236
return static::fromString($token);
228237
}

0 commit comments

Comments
 (0)