Passed
Push — master ( f7f26a...2fd308 )
by Atanas
02:20
created

Csrf::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Csrf;
4
5
use WPEmerge\Requests\Request;
6
7
/**
8
 * Provide CSRF protection utilities through WordPress nonces.
9
 */
10
class Csrf {
11
	/**
12
	 * Convenience header to check for the token.
13
	 *
14
	 * @var string
15
	 */
16
	protected $header = 'X-CSRF-TOKEN';
17
18
	/**
19
	 * GET/POST parameter key to check for the token.
20
	 *
21
	 * @var string
22
	 */
23
	protected $key = '';
24
25
	/**
26
	 * Maximum token lifetime.
27
	 *
28
	 * @see https://codex.wordpress.org/Function_Reference/wp_verify_nonce
29
	 * @var integer
30
	 */
31
	protected $maximum_lifetime = 2;
32
33
	/**
34
	 * Last generated token.
35
	 *
36
	 * @var string
37
	 */
38
	protected $token = '';
39
40
	/**
41
	 * Constructor.
42
	 *
43
	 * @codeCoverageIgnore
44
	 * @param string  $key
45
	 * @param integer $maximum_lifetime
46
	 */
47
	public function __construct( $key = '__wpemergeCsrfToken', $maximum_lifetime = 2 ) {
48
		$this->key = $key;
49
		$this->maximum_lifetime = $maximum_lifetime;
50
	}
51
52
	/**
53
	 * Get the last generated token.
54
	 *
55
	 * @return string
56
	 */
57
	public function getToken() {
58
		if ( ! $this->token ) {
59
			$this->generateToken();
60
		}
61
		return $this->token;
62
	}
63
64
	/**
65
	 * Get the csrf token from a request.
66
	 *
67
	 * @param  Request $request
68
	 * @return string
69
	 */
70
	public function getTokenFromRequest( Request $request ) {
71
		if ( $request->get( $this->key ) ) {
72
			return $request->get( $this->key );
73
		}
74
75
		if ( $request->post( $this->key ) ) {
76
			return $request->post( $this->key );
77
		}
78
79
		if ( $request->headers( $this->header ) ) {
80
			return $request->headers( $this->header );
81
		}
82
83
		return '';
84
	}
85
86
	/**
87
	 * Generate a new token.
88
	 *
89
	 * @param  int|string $action
90
	 * @return string
91
	 */
92
	public function generateToken( $action = -1 ) {
93
		$action = $action === -1 ? session_id() : $action;
94
		$this->token = wp_create_nonce( $action );
95
		return $this->getToken();
96
	}
97
98
	/**
99
	 * Check if a token is valid.
100
	 *
101
	 * @param  string     $token
102
	 * @param  int|string $action
103
	 * @return boolean
104
	 */
105
	public function isValidToken( $token, $action = -1 ) {
106
		$action = $action === -1 ? session_id() : $action;
107
		$lifetime = intval( wp_verify_nonce( $token, $action ) );
108
		return ( $lifetime > 0 && $lifetime <= $this->maximum_lifetime );
109
	}
110
111
	/**
112
	 * Add the token to a URL.
113
	 *
114
	 * @param  string $url
115
	 * @return string
116
	 */
117
	public function url( $url ) {
118
		return add_query_arg( $this->key, $this->getToken(), $url );
119
	}
120
121
	/**
122
	 * Return the markup for a hidden input which holds the current token.
123
	 *
124
	 * @return void
125
	 */
126
	public function field() {
127
		echo '<input type="hidden" name="' . esc_attr( $this->key ) . '" value="' . esc_attr( $this->getToken() ) . '" />';
128
	}
129
}
130