1 | <?php |
||
2 | |||
3 | namespace ElfSundae\Laravel\Api\Middleware; |
||
4 | |||
5 | use Closure; |
||
6 | use Illuminate\Http\Request; |
||
7 | use ElfSundae\Laravel\Api\Token; |
||
8 | use ElfSundae\Laravel\Api\Helper; |
||
9 | use ElfSundae\Laravel\Api\Exceptions\InvalidApiTokenException; |
||
10 | |||
11 | class VerifyApiToken |
||
12 | { |
||
13 | /** |
||
14 | * The Token instance. |
||
15 | * |
||
16 | * @var \ElfSundae\Laravel\Api\Token |
||
17 | */ |
||
18 | protected $token; |
||
19 | |||
20 | /** |
||
21 | * The URIs that should be excluded from token verification. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $except = []; |
||
26 | |||
27 | /** |
||
28 | * Create the middleware. |
||
29 | * |
||
30 | * @param \ElfSundae\Laravel\Api\Token $token |
||
31 | */ |
||
32 | public function __construct(Token $token) |
||
33 | { |
||
34 | $this->token = $token; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Handle an incoming request. |
||
39 | * |
||
40 | * @param \Illuminate\Http\Request $request |
||
41 | * @param \Closure $next |
||
42 | * @return mixed |
||
43 | * |
||
44 | * @throws \ElfSundae\Laravel\Api\Exceptions\InvalidApiTokenException |
||
45 | */ |
||
46 | public function handle($request, Closure $next) |
||
47 | { |
||
48 | if ($this->inExceptArray($request) || $this->verifyToken($request)) { |
||
49 | return $next( |
||
50 | Helper::setCurrentAppKeyForRequest($this->getKeyFromRequest($request), $request) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | throw new InvalidApiTokenException; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Determine if the request has a URI that should be passed through verification. |
||
59 | * |
||
60 | * @param \Illuminate\Http\Request $request |
||
61 | * @return bool |
||
62 | */ |
||
63 | protected function inExceptArray(Request $request) |
||
64 | { |
||
65 | foreach ($this->except as $except) { |
||
66 | if ($except !== '/') { |
||
67 | $except = trim($except, '/'); |
||
68 | } |
||
69 | |||
70 | if ($request->is($except)) { |
||
71 | return true; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Verify the api token from request. |
||
80 | * |
||
81 | * @param \Illuminate\Http\Request $request |
||
82 | * @return bool |
||
83 | */ |
||
84 | protected function verifyToken(Request $request) |
||
85 | { |
||
86 | $time = (int) ($request->input('_time') ?: $request->header('X-API-TIME')); |
||
87 | $token = $request->input('_token') ?: $request->header('X-API-TOKEN'); |
||
88 | |||
89 | return abs(time() - $time) < (int) config('api.token_duration') && |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
90 | $this->token->verify($token, $this->getKeyFromRequest($request), $time); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the app key from the request. |
||
95 | * |
||
96 | * @param \Illuminate\Http\Request $request |
||
97 | * @return string |
||
98 | */ |
||
99 | protected function getKeyFromRequest(Request $request) |
||
100 | { |
||
101 | return $request->input('_key') ?: $request->header('X-API-KEY'); |
||
102 | } |
||
103 | } |
||
104 |