Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Authenticated_Nonce
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 verify
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Every Data Sync endpoint should be protected by nonce that belongs to an authenticated user.
4 * and is generated and verified for that specific endpoint.
5 *
6 * @package automattic/jetpack-wp-js-data-sync
7 */
8
9namespace Automattic\Jetpack\WP_JS_Data_Sync\Endpoints;
10
11use Automattic\Jetpack\WP_JS_Data_Sync\DS_Utils;
12
13class Authenticated_Nonce {
14
15    /**
16     * @var string Which nonce action to verify?
17     */
18    private $action;
19
20    public function __construct( $name ) {
21        $this->action = $name;
22    }
23
24    public function create() {
25
26        if ( DS_Utils::is_debug() && ! did_action( 'set_current_user' ) ) {
27            throw new \RuntimeException( "Debug: Attempting to create {$this->action} nonce before the user is set." );
28        }
29        return wp_create_nonce( $this->action );
30    }
31
32    public function verify( $nonce ) {
33        if ( DS_Utils::is_debug() && ! did_action( 'set_current_user' ) ) {
34            throw new \RuntimeException( "Debug: Attempting to validate {$this->action} nonce before the user is set." );
35        }
36
37        return wp_verify_nonce( $nonce, $this->action );
38    }
39}