-
Notifications
You must be signed in to change notification settings - Fork 9
/
JWTSignatureBehavior.php
47 lines (36 loc) · 1.08 KB
/
JWTSignatureBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace AstrotechLabs\JWTTools;
use AstrotechLabs\JWTTools\JWTTools;
use Yii;
use yii\base\ActionFilter;
use yii\web\UnauthorizedHttpException;
class JWTSignatureBehavior extends ActionFilter
{
/**
* @var string
*/
public $headerName = 'Authorization';
/**
* @var string
*/
public $secretKey;
/**
* @inheritDoc
*/
public function beforeAction($action)
{
$authorizationHeader = Yii::$app->request->getHeaders()->get($this->headerName);
if (!$authorizationHeader) {
throw new UnauthorizedHttpException('Your request was made without an authorization token.');
}
$token = explode(' ', $authorizationHeader)[1];
$jwtTools = JWTTools::build($this->secretKey);
if ($jwtTools->tokenIsExpired($token)) {
throw new UnauthorizedHttpException('Authentication token is expired.');
}
if (!$jwtTools->signatureIsValid($token)) {
throw new UnauthorizedHttpException('The token signature is invalid.');
}
return true;
}
}