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

VerifyCsrfToken::handle()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 2
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
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