1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tymon\JWTAuth\Middleware; |
4
|
|
|
|
5
|
|
|
use Tymon\JWTAuth\JWTAuth; |
6
|
|
|
use Illuminate\Events\Dispatcher; |
7
|
|
|
use Illuminate\Routing\ResponseFactory; |
8
|
|
|
|
9
|
|
|
abstract class BaseMiddleware |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Illuminate\Routing\ResponseFactory |
13
|
|
|
*/ |
14
|
|
|
protected $response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Illuminate\Events\Dispatcher |
18
|
|
|
*/ |
19
|
|
|
protected $events; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Tymon\JWTAuth\JWTAuth |
23
|
|
|
*/ |
24
|
|
|
protected $auth; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new BaseMiddleware instance |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Routing\ResponseFactory $response |
30
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
31
|
|
|
* @param \Tymon\JWTAuth\JWTAuth $auth |
32
|
|
|
*/ |
33
|
15 |
|
public function __construct(ResponseFactory $response, Dispatcher $events, JWTAuth $auth) |
34
|
|
|
{ |
35
|
15 |
|
$this->response = $response; |
36
|
15 |
|
$this->events = $events; |
37
|
15 |
|
$this->auth = $auth; |
38
|
15 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Fire event and return the response |
42
|
|
|
* |
43
|
|
|
* @param string $event |
44
|
|
|
* @param string $error |
45
|
|
|
* @param integer $status |
46
|
|
|
* @param array $payload |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
12 |
|
protected function respond($event, $error, $status, $payload = []) |
50
|
|
|
{ |
51
|
12 |
|
$response = $this->events->fire($event, $payload, true); |
52
|
|
|
|
53
|
12 |
|
return $response ?: $this->response->json(['error' => $error], $status); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert to array a string passed as argument of a middleware in this way key1-ele1;key2-ele2 |
59
|
|
|
* @param String $str |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
15 |
|
protected function convertToArray($str){ |
63
|
15 |
|
$ret = []; |
64
|
15 |
|
$str = explode(';', $str); |
65
|
15 |
|
foreach($str as $value) { |
66
|
15 |
|
$tmp = explode('-', $value); |
67
|
15 |
|
if(count($tmp) != 2) |
68
|
15 |
|
return []; |
69
|
|
|
$ret[$tmp[0]] = $tmp[1]; |
70
|
|
|
} |
71
|
|
|
return $ret; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|