VerifyCsrfToken   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 3
1
<?php
2
/**
3
 * This file verify nonce.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace WPB\App\Http\Middleware;
10
11
use Closure;
12
use Illuminate\Http\Request;
13
14
/**
15
 * The verify csrf token class for wp nonce.
16
 *
17
 * @since      1.0.0
18
 *
19
 * @author     Md Abu Ahsan basir <[email protected]>
20
 */
21
class VerifyCsrfToken
22
{
23
    /**
24
     * Handle an incoming request.
25
     *
26
     * @param \Illuminate\Http\Request $request The app http request.
27
     * @param \Closure                 $next    The next closure.
28
     *
29
     * @throws \Exception Throw the exception.
30
     *
31
     * @return mixed
32
     */
33
    public function handle(Request $request, Closure $next)
34
    {
35
        $token = $request->input('_token') ?? $request->header('X-CSRF-TOKEN');
36
        $action = $request->wpb_nonce ?? 'wpb_nonce';
37
38
        if (!wp_verify_nonce($token, $action)) {
0 ignored issues
show
Bug introduced by
The function wp_verify_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        if (!/** @scrutinizer ignore-call */ wp_verify_nonce($token, $action)) {
Loading history...
39
            if ($request->ajax()) {
40
                return wp_send_json(['message' => 'CSRF Token mitchmatch'], 403);
0 ignored issues
show
Bug introduced by
The function wp_send_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                return /** @scrutinizer ignore-call */ wp_send_json(['message' => 'CSRF Token mitchmatch'], 403);
Loading history...
41
            }
42
43
            throw new \Exception('CSRF Token mismatch');
44
        }
45
46
        return $next($request);
47
    }
48
}
49