-
Notifications
You must be signed in to change notification settings - Fork 0
/
JwtMiddleware.php
30 lines (26 loc) · 1006 Bytes
/
JwtMiddleware.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
<?php
namespace go1\jwt_middleware;
use Firebase\JWT\JWT;
use Silex\Api\BootableProviderInterface;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class JwtMiddleware implements BootableProviderInterface
{
public function boot(Application $app)
{
$app->before(function (Request $req) {
if ($auth = $req->headers->get('Authorization') ?: $req->headers->get('authorization')) {
if (0 === strpos($auth, 'Bearer ')) {
$token = substr($auth, 7);
}
}
$token = $req->query->get('jwt', isset($token) ? $token : null);
$token = $token ?: $req->cookies->get('jwt');
if ($token && (2 === substr_count($token, '.'))) {
$chunks = explode('.', $token);
$req->attributes->set('jwt.payload', JWT::jsonDecode(JWT::urlsafeB64Decode($chunks[1])));
$req->attributes->set('jwt.raw', $token);
}
});
}
}