Passed
Push — master ( 5cfb80...5d8a7b )
by CodexShaper
13:02
created

VerifyCsrfToken   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 16 5
1
<?php
2
3
namespace WPB\App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class VerifyCsrfToken
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return mixed
16
     */
17
    public function handle(Request $request, Closure $next)
18
    {
19
        $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');
20
        $action = $request->wpb_nonce ?: 'wpb_nonce';
21
22
        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

22
        if ( !/** @scrutinizer ignore-call */ wp_verify_nonce( $token, $action ) ) {
Loading history...
23
            if ($request->ajax()) {
24
                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

24
                return /** @scrutinizer ignore-call */ wp_send_json(["message" => "CSRF Token mitchmatch"], 403 );
Loading history...
25
            }
26
27
            throw new \Exception("CSRF Token mismatch");
28
            
29
            
30
        }
31
32
        return $next($request);
33
    }
34
}
35