2
2
3
3
namespace Webdevcave \Jwt ;
4
4
5
- use Exception ;
5
+ use Webdevcave \Jwt \Exception \InvalidAlgException ;
6
+ use Webdevcave \Jwt \Exception \InvalidTokenException ;
7
+ use Webdevcave \Jwt \Exception \TokenNotPresentException ;
6
8
use Webdevcave \Jwt \Secrets \Secret ;
7
9
use Webdevcave \Jwt \Signer \Hs \Hs256Signer ;
8
10
use Webdevcave \Jwt \Signer \Signer ;
@@ -87,7 +89,9 @@ private static function getDefaultSigner(): Signer
87
89
}
88
90
89
91
/**
90
- * @throws Exception
92
+ * @throws InvalidAlgException
93
+ * @throws InvalidTokenException
94
+ * @throws TokenNotPresentException
91
95
*
92
96
* @return Token
93
97
*/
@@ -117,12 +121,12 @@ public static function fromAuthorizationBearer(): Token
117
121
}
118
122
119
123
if (!$ authorizationHeader ) {
120
- throw new Exception ('Authorization header is not set ' );
124
+ throw new TokenNotPresentException ('Authorization header is not set ' );
121
125
}
122
126
123
127
$ matches = [];
124
128
if (!preg_match ('/Bearer\s((.*)\.(.*)\.(.*))/ ' , $ authorizationHeader , $ matches )) {
125
- throw new Exception ('Invalid "Authorization" header value ' );
129
+ throw new InvalidTokenException ('Invalid "Authorization" header value ' );
126
130
}
127
131
128
132
return static ::fromString ($ matches [1 ]);
@@ -131,7 +135,8 @@ public static function fromAuthorizationBearer(): Token
131
135
/**
132
136
* @param string $token
133
137
*
134
- * @throws Exception
138
+ * @throws InvalidAlgException
139
+ * @throws InvalidTokenException
135
140
*
136
141
* @return Token
137
142
*/
@@ -140,15 +145,15 @@ public static function fromString(string $token): Token
140
145
$ parts = explode ('. ' , $ token );
141
146
142
147
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 "." ' );
144
149
}
145
150
146
151
$ headers = json_decode (self ::decodeSection ($ parts [0 ]), true ) ?: [];
147
152
$ payload = json_decode (self ::decodeSection ($ parts [1 ]), true ) ?: [];
148
153
$ signature = self ::decodeSection ($ parts [2 ]) ?: '' ;
149
154
150
155
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 ' );
152
157
}
153
158
154
159
return static ::create ()
@@ -214,15 +219,19 @@ public static function create(): Token
214
219
}
215
220
216
221
/**
217
- * @param $param
222
+ * @param string $param
218
223
*
219
- * @throws Exception
224
+ * @throws TokenNotPresentException
220
225
*
221
226
* @return Token
222
227
*/
223
- public static function fromQueryString ($ param = 'token ' ): Token
228
+ public static function fromQueryString (string $ param = 'token ' ): Token
224
229
{
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
+ }
226
235
227
236
return static ::fromString ($ token );
228
237
}
0 commit comments